SharePoint 2013: javaScript/jQuery_spPageContextInfo Variable
Introduction
_spPageContextInfo variable provides few properties which will be useful in JavaScript and client object model code.
_spPageContextInfo is a JavaScript/jquery context variable, which will rendered for each SharePoint page. _spPageContextInfo holds few proprieties which will be useful in JavaScript/jquery 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
$.ajax({
beforeSend: function (request) {
request.setRequestHeader("Accept", "application/json;odata=verbose;charset=utf-8");
},
url: _spPageContextInfo.siteAbsoluteUrl +"/_api/search/query?querytext='" + request.term + "*'&sourceid='b09a7990-05ea-4af9-81ef-edfab16c4e31'&rowlimit='20'&selectproperties='FirstName,AccountName,PictureURL,SipAddress,PreferredName,Country,City'",
dataType: "json",
success: function (data) {
response($.map(data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results, function (item) {
return {
fields: getFields(item.Cells.results)
}
}));
},
error: function (data) {
alert('search error');
}
});
_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/ SharepointSite /Lists/ custom/ custom.aspx"
siteAbsoluteUrl: "http://svr:2525/sites/ SharepointSite "
siteClientTag: "0$$15.0.4433.1506 15.0.4433.1506"
siteServerRelativeUrl; "/sites/ SharepointSite "
systemUserKey: "i:0).w|s-1-5-21-2086049...90378103-3194450012-500"
tenantAppVersion: "0"
userId: 1
webAbsoluteUrl: "http:// svr:2525/sites /sites/SharepointSite"
webLanguage: 1033
webLogoUrl:"_layouts/15/images/siteicon.png"
webPermMasks: Object { High=2147483647, Low=4294967295}
webServerRelativeUrl: "/sites/ SharepointSite "
webTemplate: "1"
webTitle : " SharepointSite "