Monday, July 5, 2010

Enable or Disable buttons in SharePoint Ribbon

A few days ago I was doing some reading on the web and I ran across Chris O'Brien post on the ribbon:
http://www.sharepointnutsandbolts.com/2010/01/customizing-ribbon-part-1-creating-tabs.html

(very good post, recommended to everyone if you want to get to know the ribbon better!)

In the first part of this post there is a sample ribbon tab with 4 buttons dependent on one another.

Meaning: clicking one button should enable or disable other buttons on the ribbon (Same tab, different groups).



This was supposed to be easy to achieve using the "EnabledScript" attribute on the CommandUIHandler associated with the buttons needed to be changed.

In my RTM machine, clicking one button did not change the status of the other buttons. After a little research I learned that other examples of enabling a button based on selected list items do work.

I then figured out that were was some update or refresh code running when selecting a list item that was not running during a click on my ribbon button.
This led me to find that in Chris's example, if I select an item after changing the flag - the buttons get enabled as they should!

To keep it short, there is a Javascript method called "RefreshCommandUI" that gets called after certain events in SharePoint (Like selecting an item in a list view) which is responsible to handle all updates of the ribbon.
These updates include:
1. Showing or hiding a contextual tab based on context
2, Enabling or Disabling buttons in the current active tab

Note, that this command does not run on buttons that are not active, but as soon as your tab will become active it will trigger the "EnabledScript" so not to worry!

Anyway, Thanks Chris for the great post, I hope this little fix here will make your ribbonizing experience even more smooth and worth while!

Shai Petel.

2 comments:

Chris O'Brien said...

Thanks Shai - replied in the comments over on my post, but in summary I'm sure the sample worked in pre-RTM builds without explicitly calling RefreshCommandUI(). However, looks like this is now necessary in RTM for these situations.

Great work, thanks for the correction!

Chris.

Chris O'Brien said...

Thanks Shai. I replied in the comments over on my post, but in summary I'm sure my sample worked without explicitly calling RefreshCommandUI() in pre-RTM builds. Seems like it's now necessary to make this call in RTM however, so great work and thanks for the correction!

Chris.