Learn about SharePoint 2013 development on Ted Pattison's Blog > Posts > Understanding the _spPageContextInfo variable in SharePoint 2010
July 26
Understanding the _spPageContextInfo variable in SharePoint 2010

For a while now I have been using the _spPageContextInfo variable in the JavaScript code I write for SharePoint 2010. I first learned about this variable when I saw in sample JavaScript code that others had blogged about. For example, what should you do when you need to parse together an URL to a page or a Web service that is relative to the root of the hosting Web application. You can easily discover the relative path between the current site collection and the root of the hosting Web application using a property named siteServerRelativeUrl which is available through the _spPageContextInfo variable.

var siteCollectionUrl = _spPageContextInfo.siteServerRelativeUrl;

While I have certainly appreciated the functionality of the _spPageContextInfo variable, my curiosity began to grow wondering why it works the way it does. I searched for information on Bing and Google trying to find an official Microsoft reference or any more information on this variable. However, all I was able to find was more examples of using the _spPageContextInfo variable in code samples but real insight into when and how this variable gets created and what information you can expect to find inside it. That led me to doing a little digging to find out how it really works behind the scenes.

As it turns out, the _spPageContextInfo variable is added to pages in SharePoint 2010 by the SPWebPartManager control. What's nice is that Microsoft has added the SPWebPartManager control to every Master Page that they deploy inside the Master Page Gallery including v4.master, minimal.master, default.master as well as all the master pages that are included as part of the SharePoint Server 2010 publishing features such as nightandday.master and BlueBand.master. That means you can expect the _spPageContextInfo variable to be included on any SharePoint 2010 page except in rare cases such as when the page is hardcoded to a legacy master page in the _layouts directory or when the page is using a custom master page that has not been implemented correctly because it does not include the SPWebPartManager control.

The SPWebPartManager control adds a client-side script block to each page with code which creates a JavaScript object and initializes it with a specific set of property values. This object is assigned to a global variable named _spPageContextInfo as shown in the following code fragment.

var _spPageContextInfo = {
   webServerRelativeUrl: "\u002fsites\u002fTestSite",
   webLanguage: 1033,
   currentLanguage: 1033,
   webUIVersion: 4,
   userId: 24,
   alertsEnabled: false,
   siteServerRelativeUrl: "\u002fsites\u002fTestSite",
   allowSilverlightPrompt: 'True' };

A key point is that the SPWebPartManager control uses the current SPSite and SPWeb objects on the Web server to query important property values and to make them available to client-side JavaScript code. This greatly simplifies writing JavaScript code when you need to create relative URLs or when you need to discover the current user's ID in the user information list for the current site collection. In general the use of the _spPageContextInfo variable is important to anyone writing JavaScript code that needs to access entry point functions exposed the SharePoint 2010 JavaScript libraries.

You will find that some page requests result in a JavaScript object behind the _spPageContextInfo variable that has more properties than other requests. For example, if the incoming request is processed inside the scope of a list such as the case when accessing AllItem.aspx, then the object behind the _spPageContextInfo variable will also provide a pageListId property containing the GUID-based ID for that list. If the incoming request targets a page stored inside a document library such as a Wiki page or a Publishing page, then the object behind the _spPageContextInfo variable will provide an additional property named pageItemId which provides the integer-based ID used to track the document for the current page in the containing document library.

Of course, if you really want to know the naked underlying truth about the _spPageContetInfo variable, you can simply inspect the implementation of a private method named SharePointClientJs_Register which the SPWebPartManager class uses to generate the client-side script block. Here's a screenshot of that method implementation as seen when inspecting the Microsoft.SharePoint.dll assembly with .NET Reflector.

SPPageContextInfo

Did you really just read through that entire block of code? What are you, some kind of inverted coding geek? Or maybe just someone that wants to learn SharePoint 2010 inside and out.

Comments

There are no comments for this post.

© Copyright 2012 Ted Pattison.  |  All Rights Reserved
LOGIN