Search code examples
c#javascriptsharepointribbon

Sharepoint Ribbon: Circumventing Context sensitivity


I'm new to sharepoint development and I'm trying to modify the behaviour of the Sharepoint ribbon. As you all know, the ribbon is such that when something else gains focus(e.g a list item), the ribbon automatically switches to an appropriate tab or tab group(e.g the List tools tab group).

I'd like to disable this constant switching of tabs and make the browse tab to always be the active tab, unless the user explicitly clicks on another tab.

I've tried doing the following in the Page_Load() of a Usercontrol, but it only works once, when the page is initially loaded. What am I doing wrong? More importantly, how could I do it right, if at all?

Basically, I'm hoping someone could point me to the event that's fired when the context changes and the ribbon switches, and how I could hook up to this event and force the ribbon to switch back to the browse tab.

protected void Page_Load()
        {
            string showBrowseTabScript = string.Empty;
            showBrowseTabScript = @"
                                function ShowBrowseTab() {

                                         var ribbon = SP.Ribbon.PageManager.get_instance().get_ribbon();
                                         SelectRibbonTab(""Ribbon.Read"", true);
                                      }

                                      SP.SOD.executeOrDelayUntilScriptLoaded(function() {

                                         var pm = SP.Ribbon.PageManager.get_instance();
                                         pm.add_ribbonInited(function() {

                                            ShowBrowseTab();
                                         });

                                         var ribbon = null;
                                         try
                                         {
                                            ribbon = pm.get_ribbon();

                                         }

                                         catch (e) { }

                                         if (!ribbon) {

                                            if (typeof(_ribbonStartInit) == ""function"")

                                               _ribbonStartInit(_ribbon.initialTabId, false, null);
                                         }
                                         else {

                                            ShowBrowseTab();
                                         }

                                      },

                                      ""sp.ribbon.js"");
                                ";
            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "BrowseTabScript", showBrowseTabScript, true);
        }

Solution

  • Here is my solution to the problem, in case anyone is interested.

    Taking Ken Henderson's suggestion into consideration, I was able to achieve what I've been trying to do, although I achieved this by modifying the code of the SP.Ribbon.js and SP.Ribbon.debug.js files. I'm using the SP.Ribbon.debug.js to show my solution below, since it is not as cryptic as the SP.Ribbon.js.

    Basically, I use the code below to trick the ribbon into thinking that the User is on a different tab and has clicked on the "Browse" tab. You will notice that I set the old tab information in the code. It will still work without me doing this, but I did it just in case the ribbon needs that information for something else I'm not aware of. This code, in combination with the Page_Load() function I posted in the first post, cause the ribbon to behave just like I needed it to.

    SP.Ribbon.PageManager.prototype = {
    
        executeRootCommand: function (commandId, properties, commandInfo, root) {
            ULSMg8: ;
            var $v_0;
            if (!SP.ScriptUtility.isNullOrUndefined(commandInfo) && commandId !== 'RibbonEvent' && (commandId !== 'CommandContextChanged' || (!SP.ScriptUtility.isNullOrUndefined(properties) && properties['ChangedByUser']))) {
    // My changes to SP.Ribbon
    
    if (properties["ChangedByUser"] === false) {
                properties["ChangedByUser"] = true;
                var $NewContextId = properties["NewContextId"];
                var $NewContextCommand = properties["NewContextCommand"];
                properties["OldContextId"] = $NewContextId;
                properties["OldContextCommand"] = $NewContextCommand;
                properties["NewContextId"] = "Ribbon.Read";
                properties["NewContextCommand"] = "ReadTab";
                SelectRibbonTab("Ribbon.Read", true);
            }
    
    // End of changes to SP.Ribbon
    
                // the rest of the code has been ommitted for clarity
            return $v_0;
        }
    }