Tags
« Pretty up SharePoint 2010 mysite with showModalDialog (updated) | Main | Australian SharePoint Conference 2012, March 20-21, Melbourne »
Friday
Feb032012

SharePoint - JavaScript current page context info

Quick blog. On every SharePoint page there's a javascript context variable:

 _spPageContextInfo 
{
webServerRelativeUrl : "/ProjectWeb",
webLanguage : 1033,
currentLanguage : 1033,
webUIVersion : 4,
pageListId : "{c1d7b89f-f07b-4e2e-b89c-76c315831d59}",
pageItemId : 5,
userId : 68,
alertsEnabled : true,
siteServerRelativeUrl : "/",
allowSilverlightPrompt : "True"
}
So if you are on /ProjectWeb/Pages/default.aspx
_spPageContextInfo.pageListId;   // list guid for Pages
_spPageContextInfo.pageItemId; // ID for listitem

 

Special Note

siteServerRelativeUrl will tell you if your site collection is sitting at the root level, or mapped further down.  Fairly useful information for all sorts of mischief.

No idea why alertsEnabled is a useful information.

 

An Example

Say if you want to show the versions page:

var options = {
tite: "Version History",
url: _spPageContextInfo.webServerRelativeUrl + "/_layouts/Versions.aspx?list="+ _spPageContextInfo.pageListId +"&ID="+ _spPageContextInfo.pageItemId,
width: 800,
height: 500,
};
SP.UI.ModalDialog.showModalDialog(options);

 

This will get you the Version History page in a modal popup, without running any additional client context async query.

image

Figure: Version History showing in a modal popup.

Reader Comments (5)

Oh... my... god... thank you so much John. I was searching for quite a while for the following search terms (maybe this will help future users find this): sharepoint 2010 javascript page ID return pageID

Anyway, this is extremely helpful; I can finally finish out an if statement that has been eluding me for days for lack of the current page ID!

December 3, 2012 | Unregistered CommenterWill Lanni

Picking the right search terms for each blog title remains the most difficult part of blogging. Seriously.

December 4, 2012 | Registered CommenterJohnLiu.NET

Thank you so much for this helpful info

January 21, 2014 | Unregistered CommenterEdisson Gomez

Does _spPageContextInfo variable on HTML also ?

I have the SharePoint Hosted App which contain some HTML pages, and there are _spPageContextInfo variable is undefined.

Would you please give me some idea about that issue ?

August 1, 2014 | Unregistered CommenterVu Pham

Not available in HTML pages. The SPPageContextInfo is generated by the control in an ASPX page.

September 5, 2014 | Registered CommenterJohnLiu.NET

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>