How To Activate SharePoint Ribbon Tab by JavaScript Code

In the last weeks I’ve extended the sample project that I’ve created for “Ribbon Customization”. You find the project source code on Codeplex:

The related articles on my blog are:


There are lots of excellent articles out there about SharePoint 2010 Ribbon customization.

Some articles that helped me:

Especially the first blog post in the above list leaves a question open: "Using client-side code to show ribbon items".

I had the same problem: How to activate a custom SharePoint Ribbon tab by JavaScript code?

After some reading in SharePoint standard JavaScript files I found the solution. I used some jQuery too:

1: $(document).ready(function  () {

 

2:     ExecuteOrDelayUntilScriptLoaded(salesappActiveRibbonTab, "sp.ribbon.js" );

 

3:

 

4:     function  salesappActiveRibbonTab() {

 

5:         try  {

 

6:             _ribbonStartInit("MyApp.SharePoint.Ribbon.CustomTab" , false , null );

 

7:         } catch  (e) {

 

8:         };

 

9:     };

 

10: });

This allows me to activate my custom Ribbon tab with ID "MyApp.SharePoint.Ribbon.CustomTab" by JavaScript code. It works pretty fine!

Update 1

Meanwhile I created an test project for adding a custom ribbon tab while runtime. There I have had the problem, that the ribbon was created correctly BUT was not activated thru the code above. After some debugging of MS Ribbon JavaScript I created the following code:

var ribbontestintervall = null;

 

function RibbonTestActiveRibbonTab2() {     try {         window.clearInterval(ribbontestintervall);

 

        if (typeof (_ribbonStartInit) == "function")             _ribbonStartInit('RibbonTest.SharePoint.Ribbon.CustomTab', false, null);

 

        if (true && typeof (_ribbonReadyForInit) == 'function' && !_ribbonReadyForInit()) {             ribbontestintervall = window.setInterval("RibbonTestActiveRibbonTab2()", 1000);         }     } catch (e2) {     };
};

So the Browser retries the activation of the tab as long as it works.

Update 2 (2011/01/14)

See my new post and sample project on CodePlex :

Sample Visual Studio 2010 project for creating a custom SharePoint 2010 Ribbon tab on runtime (!)

Update 3 (2011/03/03)

Next part posted today. See the part list at the top of this blog post.

SharePoint 2010 Application Page Code Behind Throws Exception “Trying to use an SPWeb object that has been closed or disposed and is no longer valid.”

This was the situation:

I have a SharePoint Foundation 2010 project in VS2010. There I deploy some SharePoint lists with custom views an custom form pages.

In one case I wanted to override the RaisePostBackEvent method of the customized form page. In the method I wanted to do some actions before the Save button code is executed. (Its the standard Save button of a SharePoint list form: <SharePoint:SaveButton...>.

The overridden RaisePostBackEvent locked like this:

1: protected  override  void  RaisePostBackEvent(System.Web.UI.IPostBackEventHandler  sourceControl, string  eventArgument)
2:         {
3:             if  (sourceControl is  System.Web.UI.WebControls.Button )
4:             {
5:                 System.Web.UI.WebControls.Button  button = ((System.Web.UI.WebControls.Button )sourceControl);
6:                 if  (button.ClientID.StartsWith(savebutton1.ClientID) || button.ClientID.StartsWith(savebutton2.ClientID))
7:                 {
8:                     using  (SPWeb  web = SPContext .Current.Web)
9:                     {
10:                         //some code 
11:                     }
12:                 }
13:             };
14:
15:             base .RaisePostBackEvent(sourceControl, eventArgument);
16:         }
17:
18:

When executing the custom list form page there was no errors when I filled all field input boxes with data and press the Save button. But as I tried the “required field” functionality – leaving some fields blank – the SharePoint system throws the following error message in the SharePoint log:

06/15/2010 18:11:19.94 	w3wp.exe (0x1384)                       	0x0E5C	SharePoint Foundation         	Runtime                       	tkau	Unexpected	Microsoft.SharePoint.SPException: Trying to use an SPWeb object that has been closed or disposed and is no longer valid.    at Microsoft.SharePoint.WebPartPages.SPWebPartManager.ThrowIfManagerIsInvalid()     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.get_LimitedWebPartManager()     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.LoadConnectionsState()     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.CreateWebPartsFromRowSetData(Boolean onlyInitializeClosedWebParts)     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.LoadWebParts()     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.OnPageInitComplete(Object sender, EventArgs e)     at System.EventHandler.Invoke(Object sender, EventArgs e)     at System.Web.UI.Page.OnInitComplete(EventArgs e)     at Sal...	b196095c-b664-4f3c-b764-f9b3ef8ac5db
06/15/2010 18:11:19.94*	w3wp.exe (0x1384)                       	0x0E5C	SharePoint Foundation         	Runtime                       	tkau	Unexpected	...esAppLists.Layouts.SalesApp.AddDecisionMakerForm.OnInitComplete(EventArgs e)     at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)	b196095c-b664-4f3c-b764-f9b3ef8ac5db
0

After some debugging and thinking about it I realized that it only happens when the I press the Save button and this action will not close the dialog frame of the list form page. This means: The postback action does more than “store data and leave the dialog frame”.

So I figured out that there is a overridden RaisePostBackEvent method on the page.

I removed the method for testing purpose… and the problem was away!

It seems to me that it is not possible to use using (SPWeb web = SPContext.Current.Web) in such a method.