Dynamically adding items to ribbon menu

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.
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

            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;
                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);

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.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s