How to use _spPageContextInfo JavaScript variable in SharePoint

In this SharePoint Online tutorial, I will explain how to use _spPageContextInfo JavaScript variable in SharePoint Online or SharePoint 2013/2016.

_spPageContextInfo variable provides a few properties that will be useful in JavaScript and client object model code. We can use _spPageContextInfo variable in SharePoint 2013 or SharePoint online to retrieve various properties.

Also, I will show an example of how to use _spPageContextInfo JavaScript variable in SharePoint 2013/2016 or SharePoint Online to display the current site URL.

What is _spPageContextInfo JavaScript variable in SharePoint Online?

_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 to use _spPageContextInfo in SharePoint

  • 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 how to use _spPageContextInfo  in SharePoint 2013/Online

Here I will show you a few examples of how to use _spPageContextInfo JavaScript variable in SharePoint 2013/2016 or SharePoint Online.

Example-1: Retrieve Current Site URL using _spPageContextInfo in SharePoint Online

We will see here how we can retrieve the current site URL using _spPageContextInfo JavaScript variable. You can write the below inside a script editor web part in a web part page in SharePoint.

On a button click, I am retrieving the SharePoint site URL using _spPageContextInfo.

<input type='button' value='Retrieve Site URL' onclick="getSiteURL();"/>
<br />
<script language="javascript" type="text/javascript">
function getSiteURL() {
alert(_spPageContextInfo.webAbsoluteUrl);
}
</script>

The output will appear like below:

_sppagecontextinfo sharepoint 2013
_sppagecontextinfo sharepoint 2013

Example-2:

$.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 Properties in SharePoint 2013

_spPageContextInfo has various properties which you can use in SharePoint 2013/2016/Online.

  • 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”
  • 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 “

You may like following _spPageContextInfo SharePoint tutorials:

I hope this will be useful to learn how to use  _spPageContextInfo JavaScript variable in SharePoint 2013/2016 or SharePoint Online. Also, I have shown you a few examples on _spPageContextInfo in SharePoint Online.

>