Using _spPageContextInfo to Determine the Current SharePoint Context in Script

Sahil Malik had an extremely useful little post about a month back that I emailed to myself and then promptly forgot. Today, Mikael Svenson pointed back to Sahil’s in a post of his own that is also very useful. I’m going to steal a little from both posts for this one. I hope Sahil and Mikael don’t mind. I’ll ask for forgiveness later.

Mikael’s trick shows you An easy way to accomplish Home navigation links regardless of the path of your site collection just by looking at a JavaScript variable that SharePoint provides us. Knowing the current context is something I struggled with when I first was working on SPServices, and I came up with some monkey business with the WebUrlFromPageUrl operation in the Webs Web Service and some other URL hackery to figure it out. Since then, I’ve layered in checks for some of the variables that SharePoint 2010 provides to avoid Web Service calls, where possible. In the upcoming 2013.01 release, I’ll add in a few more, and even some that take advantage of some of the additional variables in SharePoint 2013. Of course, there will be fallbacks for anything that isn’t available, so the library continues to be version-resistant.

Mikael linked back to Sahil’s post _spPageContextInfo is your new best friend. In Sahil’s post, he lists the values which are available in SharePoint 2010 and 2013 in every page. Here’s the table that Sahil provides, but I’ve sorted the data alphabetically and changed the example URL to make some of the values more clear. It’s worth noting that there isn’t an SP2007 column in Sahil’s table. That’s because the _spPageContextInfo variable wasn’t introduced until SharePoint 2010. For SharePoint 2007, it’s still monkey business and URL hackery. After all, no one uses script with SharePoint 2007, right?

Assuming we are on the page https://sharepoint.com/sites/demos/SitePages/Home.aspx

[important]2013-03-27 – Added L_Menu_BaseUrl for SharePoint 2007 based on Christophe’s helpful comment below.[/important]

Name SP2007 SP2010 SP2013 Example Value
alertsEnabled X X false
allowSilverlightPrompt X X true
clientServerTimeDelta X 17856
crossDomainPhotosEnabled X true
currentCultureName X en-US
currentLanguage X X 1033
currentUICultureName X en-US
L_Menu_BaseUrl X  X X /Sites/Demos
layoutsUrl X _layouts/15
pageItemId X X 1
pageListId X X GUID
pagePersonalizationScope X 1
serverRequestPath X /Sites/Demos/SitePages/Home.aspx
siteAbsoluteUrl X http://sharepoint.com
siteClientTag X 21$$15.0.4454.1026
siteServerRelativeUrl X X /sites/Demos
systemUserKey X 1:0).w|<sid>
tenantAppVersion X 0
userId X X 30
webAbsoluteUrl X http://sharepoint.com/sites/demos
webLanguage X X 1033
webLogoUrl X _layouts/15/images/siteicon.png
webPermMasks X Object with properties High & Low
webServerRelativeUrl X X /Sites/Demos
webTemplate X 1
webTitle X Demos
webUIVersion X X 4 (2010) or 15 (2013)

Similar Posts

25 Comments

  1. Hi Marc,

    Need your valuable suggestions over here.

    I have configured extranet zone through AAM(Alternate Access mappings) and trying to access Intranet home page. It has few cutsom webparts which loads the data from pages library and displays on the home page.

    When I try to access the extranet zone url , The webparts arent loading. After further investigation I found that spcontext object is pointing to the default zone url(Which is not accessible from Extranet) and as a result web parts are not loading.

    1. @Shravan:

      That’s really not my thing (nor does it connect to this post much). I’d suggest you post your question to a public forum where you’re more likely to get a good answer.

      M.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.