Tags

If you try to dig a little bit into Microsoft Office development, is inevitable to face the problem I am going to discuss about. And this is related to adding control to a ribbon menu. It seems the control is read only after ribbon is loaded, so suggested solution was to implement the changes in the constructor logic. This solution might work for most of the situation. However, there are situations when you want to add or remove menu items dynamically after ribbon is loaded. And this can be done very simple by changing the “Dynamic” property to true in Visual Studio.
RibbonMenuDynamicProperty
Once you have modified this property, you are allowed to modify items for the menu. Below is an example how you can do this, more exactly I have added buttons as items for each list inside a SharePoint.

            // Clear items
            this.mnSharePointLists.Items.Clear();

            using(ClientContext ctx = new ClientContext("Url"))
            {
                // Connection details, implement a string to secure string transformation to have a working example
                string username = "username";        
                SecureString password = new SecureString();
        
                // Set connection details to SharePoint Online
                ctx.Credentials = new SharePointOnlineCredentials(username, password);

                Web oWeb = ctx.Site.RootWeb;
                ListCollection oLists = oWeb.Lists;
                ctx.Load(oLists);
                ctx.ExecuteQuery();
                for(int i = 0; i < oLists.Count(); i++)
                {                   
                   RibbonButton btn = Factory.CreateRibbonButton();
                   btn.ShowImage = true;
                   btn.Image = HP.Properties.Resources.Img;   // Image is stored as resource
                   btn.Label = oLists[i].Title;
                   btn.Description = Convert.ToString(oLists[i].Description);
                   this.mnSharePointLists.Items.Add(btn);                  
                }  
            }

I removed lines and adapt the code because this is only suppose to give you an idea (a full example will come later). However, I hope you have a clear image about how to work with ribbon menu to obtain a result similar with one from the screenshot below.
RibbonMenuExample

Advertisements