.NET Tips and Tricks

Blog archive

Guru Tip: How to Activate a SharePoint Ribbon Tab in Sandboxed Solutions

I went back to Andrei Smolin, a team leader at Add-In Express, for some more SharePoint help. I wanted to activate a Ribbon Tab using, I thought, code like this:

SPRibbon  currentRibbon = SPRibbon.GetCurrent(this.Page);
currentRibbon.MakeTabAvailable("<Tab  Id>");
currentRibbon.InitialTabId  = "<Tab Id>";

Unfortunately, I wanted to use this code in a Sandboxed solution -- something that's specifically forbidden. Andrie pointed out that my code wouldn't work very well in a farm solution, either: the tab would get selected but it wouldn't get activated.

Andrie gave me the following script, which not only bypasses the Sandbox limitation but actually activates the tab. This code works with a feature called SPRibbon and a tab with the ID AdxspRibbonTab1, so you'll need to change those two names in your code:

var ribbonReady = false;

function onBodyLoaded() {
  if (!ribbonReady) {
    if ((typeof (_ribbonReadyForInit) == 'function') &&
         _ribbonReadyForInit()) {
      ribbonReady = true;
      var control = get_SPRibbon().FindControl("AdxspRibbonTab1");
      if (control != null) {
        var tabElem = document.getElementById(control.getFullId() +
                                              "-title");
        if (tabElem && tabElem.firstChild) {
                    tabElem.firstChild.click();	
        }
      }
    }
  }
};

if (_spBodyOnLoadFunctionNames != 'undefined' && 
    _spBodyOnLoadFunctionNames != null) {
    _spBodyOnLoadFunctionNames.push("onBodyLoaded");
}
onBodyLoaded();

Posted by Peter Vogel on 05/17/2012


comments powered by Disqus

Featured

Subscribe on YouTube