Introduction
_spPageContextInfo is a JavaScript context variable, which will rendered for each SharePoint page. _spPageContextInfo holds few proprieties which will be useful in JavaScript and client object model code. If you want to know the properties of _spPageContextInfo, go to view page source and find text with "_spPageContextInfo".
When It will be useful:
- When we are making any ajax calls, we might need the server URL dynamically. In this case we can use _spPageContextInfo.siteAbsoluteUrl property.
- If you want to know the current SharePoint List GUID, use _spPageContextInfo.pageListId.
- Even it has more properties also(which are self explanatory). We can use them according to the requirements.
Sample Code:
if (_spPageContextInfo != null) {
if (_spPageContextInfo.pageListId != null) {
var pageListId = _spPageContextInfo.pageListId;
}
if (_spPageContextInfo.siteAbsoluteUrl && _spPageContextInfo.siteAbsoluteUrl != '') {
var siteAbsoluteUrl = _spPageContextInfo.siteAbsoluteUrl
}
}
$.ajax({
type: "GET",
url: siteAbsoluteUrl + "/_layouts/15/Test.aspx?listid=" + pageListId + "",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert('success');
},
error: function () {
alert('failed ');
}
});
_spPageContextInfo Object Properties found for SharePoint 2013 application:
- alertsEnabled: false
- allowSilverlightPrompt :"True"
- clientServerTimeDelta :-8980
- crossDomainPhotosEnabled: false
- currentCultureName: "en-US"
- currentLanguage: 1033
- currentUICultureName: "en-US"
- layoutsUrl: "_layouts/15"
- pageListId: "{df5e2d45-ad0b-4608-a813-ded6a97511db}"
- pagePersonalizationScope: 1
- serverRequestPath: "/sites/dotnetmirror/Lists/Calender custom/calendar.aspx"
- siteAbsoluteUrl: "http://computerName:5000/sites/dotnetmirror"
- siteClientTag: "0$$15.0.4433.1506"
- siteServerRelativeUrl; "/sites/dotnetmirror"
- systemUserKey: "i:0).w|s-1-5-21-2086049...90378103-3194450012-500"
- tenantAppVersion: "0"
- userId: 1
- webAbsoluteUrl: "http://computerName:5000/sites/dotnetmirror"
- webLanguage: 1033
- webLogoUrl:"_layouts/15/images/siteicon.png"
- webPermMasks: Object { High=2147483647, Low=4294967295}
- webServerRelativeUrl: "/sites/dotnetmirror"
- webTemplate: "1"
- webTitle : "dotnetmirror"