Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

First of all, I've asked this same question on StackOverflow. I don't know if questions asked there are visited by users here and vice-versa, so in hopes that my question gets the most exposure, I'm asking here again. If this is generally frowned on here, then my sincere appologies.

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.

After a lot of reading on the net, 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);
        }
share|improve this question

2 Answers 2

I believe what you are trying to achieve is something not possible.

SharePoint Ribbon switches the contextual tab group automatically, when user selects different web parts or controls in the page. This default behavior cannot be controlled.

What you have achieved though is the default selection of the ribbon tab when the page is loaded and the results you get are correct. You can select any ribbon tab as the default one, but persisting the selected tab group would not be possible.

share|improve this answer
    
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 using code similar to what I have in the ShowBrowseTab() method. I've edited my post to reflect this. –  FonTak Apr 17 '12 at 7:48
    
I solved my problem and have posted the solution on StackOverflow. You can find the link in my original post. –  FonTak Apr 18 '12 at 12:51
up vote 0 down vote accepted

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

Taking Ken Henderson's(from StackOverflow) 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;
    }
}
share|improve this answer
1  
It's highly recommended do not modify any out of the box sharepoint files, because this may cause an unexpected behavior on some pages under different circumstances or after sharepoint cumulative updates. –  Kai Apr 18 '12 at 13:11
    
@Kai I was/am very skeptical about modifying those files as well. I am doing it this way for now. Do you have any suggestion for adding this behaviour to sharepoint without modifying those files? –  FonTak Apr 18 '12 at 14:24
    
About suggestions...playing with javascript, but this is really complicated task. –  Kai Apr 18 '12 at 14:47
1  
um... i'm impressed by the code that FonTak managed to find/edit in SP.Ribbon.JS... but I agree 1000% that nobody should be modifying any of the default files... that said... I tested just the last line (SelectRibbonTab("Ribbon.Read", true))... which SEEMS to do what he's asking (selects the 'Browse' tab)... was FonTak really that close to a very simple answer? –  Scott Brickey Aug 20 '12 at 20:39
    
Thus far, it works for me :) –  FonTak Aug 22 '12 at 8:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.