Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Dynamic populate ribbon button edit XML error

(0) ShareShare
ReportReport
Posted on by


Hello,

I'm creating dynamic ribbon flyout buttons with ribbon workbench, which to be clicked to trigger actions. Refer the blog:

Dynamic Flyout Menu in Unified Interface (UCI) (www.kashanahmed.com/.../dynamic-flyout-menu-in-unified_15.html).

The main point of my code:
1. Retrieved Template Names and IDs (NOT GUID) from external endpoint.
2. Using the Names and IDs to edit the ribbon XML with JavaScript.

Here is my code:

function DynamicTemplateMenu(commandProperties) {

    debugger;

    var templates = [; //global templates variable

    const requestOptions = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ get_templates: 'Yes' })
    };

    fetch('prod-16.canadaeast.logic.azure.com:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'requestOptions)
        .then(response => response.json())
        .then(data => {

            alert(JSON.stringify(data));
            data = data['all-template-data';
            for (let i = 0i < data.lengthi ) {
                templates.push(data[i);
            }

            alert(JSON.stringify(templates));

            var entityName = Xrm.Page.data.entity.getEntityName();

            var isUCI = Xrm.Internal.isUci();

            var page = "Form";

            var commandId = "mcu.contact.Command.onTemplateClicked";

            if (isUCI) {

                commandId = entityName   "|NoRelationship|"   page   "|"   commandId;
            }

            var menuXml = '<Menu Id="mcu.contact.Template.Menu">';
            menuXml  = '<MenuSection Id="mcu.contact.Templates.MenuSection" Sequence="10" DisplayMode="Menu16">'
            menuXml  = '<Controls Id="mcu.contact.Templates.Control">'

            var Name = "";

            var Value = "";

            if (templates != null) {

                for (var i = 0i < templates.lengthi ) {
                    var Name = templates[i.templateName;
                    var Value = templates[i.templateId;

                    menuXml  = '<Button Alt="'   Name   '" Command="'   commandId   '" Id="'   Value   '" Sequence="'   ((i   1) * 10)   '" LabelText="'   Name   '" TemplateAlias="o1" ToolTipTitle= "'   Name   '" ToolTipDescription="'   Name   '" />'
                }

                menuXml  = '</Controls>';
                menuXml  = '</MenuSection>';
                menuXml  = '</Menu>';

                commandProperties["PopulationXML" = menuXml;
            }
        });
}

//=========================================================================

function onTemplateClicked(commandProperties) {
    alert("template with id "   commandProperties.SourceControlId   " selected.");
}

//=========================================================================
 
Run the code, there is a error as below
pastedimage1669674877053v1.png
The Line 78 menuXml format of value as below:
<Menu Id="mcu.contact.Template.Menu">
  <MenuSection Id="mcu.contact.Templates.MenuSection" Sequence="10" DisplayMode="Menu16">
    <Controls Id="mcu.contact.Templates.Control">
      <Button Alt="Temp1111.docx" Command="contact|NoRelationship|Form|mcu.contact.Command.TemplateClicked" Id="MAX0001" Sequence="10" LabelText="Temp1111.docx" TemplateAlias="o1" ToolTipTitle"Temp1111.docx" ToolTipDescription="Temp1111.docx" />
      <Button Alt="Temp2222.docx" Command="contact|NoRelationship|Form|mcu.contact.Command.TemplateClicked" Id="MAX0002" Sequence="20" LabelText="Temp2222.docx" TemplateAlias="o1" ToolTipTitle"Temp2222.docx" ToolTipDescription="Temp2222.docx" />
      <Button Alt="Temp3333.docx" Command="contact|NoRelationship|Form|mcu.contact.Command.TemplateClicked" Id="MAX0003" Sequence="30" LabelText="Temp3333.docx" TemplateAlias="o1" ToolTipTitle"Temp3333.docx" ToolTipDescription="Temp3333.docx" />
      <Button Alt="Temp4444.docx" Command="contact|NoRelationship|Form|mcu.contact.Command.TemplateClicked" Id="MAX0004" Sequence="40" LabelText="Temp4444.docx" TemplateAlias="o1" ToolTipTitle"Temp4444.docx" ToolTipDescription="Temp4444.docx" />
    </Controls>
  </MenuSection>
</Menu>

  
    
      
Could you pls help me to check? Thanks.
  • ALAN365 Profile Picture
    on at
    RE: Dynamic populate ribbon button edit XML error

    Thank you Andrew, it works.

    Only different is I have used sessionStorage to store the data when form load without using global variable.

  • Verified answer
    a33ik Profile Picture
    84,325 Most Valuable Professional on at
    RE: Dynamic populate ribbon button edit XML error

    As far as I understood you have no need to pass anything.

  • ALAN365 Profile Picture
    on at
    RE: Dynamic populate ribbon button edit XML error

    Hi Andrew,

    Thank you so much for your response.

    I'm rewriting the code following your suggestion. Have a question now, using Enable Rule to trigger function asyncDataLoad, when I set up Custom Rule, which Crm Parameter I need to select?  PrimaryControl or CommendProperties, or just leave it doing nothing?

    Thank you.

  • Verified answer
    a33ik Profile Picture
    84,325 Most Valuable Professional on at
    RE: Dynamic populate ribbon button edit XML error

    Hello,

    I believe that the main issue is that your request is executed in the Async way when this command should be generated in a Sync one.

    I had a similar scenario and here is the sudo code that I used:

    var alltemplates = [];
    
    function asyncDataLoad() {
    //async call to load the data in async way
    //and save the data in alltemplates
    
    //this is the stub that always returns true
    return true;
    }
    
    function DynamicTemplateMenu(commandProperties) {
    //use the data from alltemplates to populate PopulationXML of commandProperties
    }

    So the idea is the following - I use asyncDataLoad to load the data. The trigger for it - enabled rule.

    Once the form is loaded - asyncDataLoad is called, this is triggered before the dropdown is populated.

    Then... when the dropdown is opened - templates data is ready.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,884 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,760 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans