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) |
For the record, in SP 2007 the global variable L_Menu_BaseUrl will give you the current URL.
Thanks, Christophe! I didn’t used to trust any of the JavaScript variables that SharePoint used, but at this point I do.
M.
Yeah, global variables are usually considered a bad practice, but they are the building blocks of SharePoint pages… Even more (worse?) in SP 2013!
Marc, I see that you have updated your grid, for the record L_Menu_BaseUrl is also available in SP 2010.
And 2013, as it turns out. Updated!
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
Jason:
Good question. I just Bingled and found this helpful post:
http://sadomovalex.blogspot.com/2010/01/how-sharepoint-sets-currentthreadcurren.html
Quite a bit less helpful, in that it is typically obtuse, is the MSDN doc here:
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo_properties.aspx
M.
Small suggestion as “siteServerRelativeUrl” is available with SP2010.
Could be SP/CU issues, but we see this prop everywhere on SP2010 farms.
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.
G8 article… Can you please share list of all variable or redirect me to the source where I can get list of all such variables/objects?
Ravi:
I had to glue this together from multiple sources, so I’m pretty sure there’s no “list”. This all varies by version, update level, etc.
M.
Thanks Marc…
BTW… You are so quick… :)
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?
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.
I see the user ID which looks like the id from the internal site collection sharePoint user list. I am trying to get the SharePoint user name, email, and display name during client side rendering and it doesn’t seem to be working. Any ideas?
http://whoisidaho.com
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.
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
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