In some of the recent work I’ve been doing around the SharePoint ribbon, specifically when diving into the way how the commanding works, I noticed the SharePoint SDK talks about two types of commands: global and focused commands.
This was a bit confusing as I didn’t see a way to qualify the difference between the two when working with command UI handlers, only with commands implemented with a page component. Naturally I had to dive in and figure this out. The SharePoint SDK doesn’t provide a lot of context around the difference between the two. Hopefully this post will shed some additional light and clarification on the differences between the two and provide some guidance on when you should use one over the other.
Global commands are easy to understand; they are available everywhere. There’s no time a global command it’s available (provided his
canHandleCommand() handler says he is available).
So what are focused commands? The ribbon contains this thing called the FocusManager. His job is to keep track of which page component has focus. A page component can acquire or yield focus. When would this make sense? Consider a Web Part that shows the contents of a list, and you get to specify which list. So consider if I put two of these on a page and configured one to show the contents of the Announcements list while the other shows the contents of the Contacts list. My Web Part has a Panic button in the ribbon that purges the contents of the list in the Web Part. But if I have multiple Web Parts on the page, it isn’t like I get multiple buttons in the ribbon… I get just one. So what happens if I click it? If it was a global command, it would fire on both Web Parts. Yikes, maybe not what I wanted! Instead what I could do is select one Web Part which would tell the FocusManager it wants focus. Now, when I click the button, the command will run on in the focused instance.
Generally speaking you’re going to want to use global commands in the majority of the ribbon customization work you do. Focused commands add a lot of complexity and extra plumbing work that you likely don’t want to deal with so make your life easy and stick with global commands.
When does it make sense to use focused commands? As the above example leads you to see, if you have the same component on the page multiple times (such as a Web Part) that leverages specific things in one Web Part.