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. What’s the difference between currentCultureName and currentUICultureName? for me, I’m getting different values, and I’m not sure which one represents what the user sees in their browser

    1. Anton:

      I know that I went through the entries in the chart in detail when I first posted it, but things definitely do change over time, as you’ve said. You’ve probably got a valid correction, though, so I’ve updated the post.

      Thanks,
      M.

  2. Both articles you mentioned and this one as well just list the available variables. None of them explain how to get it into your app. Do I need to reference a specific .js file in the Default.aspx or can I just start using it in app.js?

    1. Jonathan:

      These posts are more focused on the on premises version of SharePoint. Keep in mind that before the launch of Office365, that was really the main story. I haven’t been building apps, so I can’t be sure of the exact answer to your question. On premises these variables are simply available in SharePoint pages.

      M.

    1. Yes, the userID is the item ID in the Site Collection’s User Information List. I have a function in SPServices you can call to get the other values. You can also call the REST APIs on Office365.

      M.

  3. Hey Marc,

    I realize this is an old post, but for anyone who comes across it, be aware that _spPageContextInfo isn’t available until fairly late in the page load process, so you need to test for its existence before using it. I haven’t (yet) figured out exactly where it is created/populated, but I’m currently struggling with it *not* being available when my CSR code (loaded via a Custom Action) is loaded.

    It is available in the OnPreRender handler of the View page override, but by that point, it’s too late to call RegisterTemplateOverrides to register additional overrides, so I need to figure out a different way to determine the current list (one that is not dependent upon the URL as that won’t work if a LVWP is added to a different page.

    God, I love SharePoint! ;-)

    Dave

    1. Dave, _spPageContextInfo is loaded with the page body, so it should be available on document ready. What property are you using in your code? Most of the info should also be available in the list view context.

      Christophe

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.