Ribbons for Non-Web Parts

Update 2011-01-20: Added note about controlling visibility.

What are Non-Web Parts?? I just invented that term to pool everything that is not supervised by a WebPartManager instance–most notably static Web Parts and user controls. But the more interesting question is: how do you make those things work with a ribbon?

The Server Side

While the XML defining your tabs makes it to the client as long as you installed your Non-Web Part as a feature it’s tabs will never show up. Initiating that would have been the WebPartManager’s job server side. Calling MakeTabAvailable() and MakeContextualGroupInitiallyVisible() will take care of this. Just remember that you need a reference to Web.Command.UI.dll as these methods and IsTabAvailable() are defined in SPRibbon’s base class Ribbon.

But now the tabs or rather the ContextualGroup shows up as soon as the page loads–the lack of supervision by a WePartManager making itself felt again. To prevent this you have to associate your ContextualGroup with a VisibilityContext and disable it.

Summing everything up the server side code will look like this:

protected void Page_Load(object sender, EventArgs e) {
 SPRibbon ribbon = SPRibbon.GetCurrent(this.Page);
 if (ribbon != null) {
  if (ribbon.IsTabAvailable("My.Tab") == false) {
   // Don't specify VisibilityContext here or Tab will never show!
   ribbon.MakeTabAvailable("My.Tab");
  }
  // Adding disabled VisibilityContext to ContextualGroup makes it initially invisible
  ribbon.MakeContextualGroupInitiallyVisible("My.ContextualGroup", "My.VisibilityContext");
  ribbon.DisableVisibilityContext("My.VisibilityContext");
 }
} // Page_Load()

The Client Side

While there is no way of enabling a VisibilityContext on the client side everything will be taken care of automatically when you call SelectRibbonTab(). This lonely line is the necessary client side JavaScript to show the ContextualGroup you just managed to hide:

SelectRibbonTab('My.Tab', true);

SelectRibbonTab() is defined in Init.debug.js and will also take care of loading the ribbon initially.

The last step is to decide when your ContextualGroup should be visible. Hiding it when another Web Part is selected seems to come as close as possible. Of course this isn’t a perfect solution as you don’t notice when an element outside a Web Part is selected.

Implementing this solution in your Ribbon XML to control visibility of a ContextualGroup would look like this:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
 ...
  <CommandUIHandlers>
   <CommandUIHandler
    Command="My.ContextualGroup.Command"
    CommandAction=""
    EnabledScript="javascript:function isContextualGroupEnabled() {
      return SP.Ribbon.WebPartComponent.get_pageComponentIdOfActiveWebPart() === null;
     }
     isContextualGroupEnabled();"
    />
  </CommandUIHandlers>
 ...
</Elements>

SP.Ribbon.WebPartComponent is definied in SP.Ribbon.debug.js. A ContextualGroup is visible if it’s EnabledScript returns true. You would use similar code to control a tab’s visibility using the Ribbon XML or to control visibility using a page component.

Advertisements
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

One Response to Ribbons for Non-Web Parts

  1. Erwin says:

    Hi can you explain me more the process of rendering a contextual tab for standard controls because I’m currently trying to implement this approach and it doesn’t seems to work. When I apply your method the contextual tab is still showing when page loads even if I declare a visibilitycontext and disable it.

    Thanks for your help

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