Friday
Feb032012
SharePoint - JavaScript current page context info
Friday, February 3, 2012 at 5:38PM 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.
Figure: Version History showing in a modal popup.
in
SharePoint,
code
SharePoint,
code 





Reader Comments (2)
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!
Picking the right search terms for each blog title remains the most difficult part of blogging. Seriously.