javaScript/jQuery_spPageContextInfo Variable in SharePoint 2013

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       

  1. $.ajax({  
  2.          beforeSend: function (request) {  
  3.             request.setRequestHeader("Accept""application/json;odata=verbose;charset=utf-8");  
  4.       },  
  5.       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'",  
  6.                 dataType: "json",  
  7.                 success: function (data) {  
  8.                     response($.map(data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results, function (item) {  
  9.                         return {  
  10.                             fields: getFields(item.Cells.results)  
  11.                         }  
  12.                     }));  
  13.                 },  
  14.                 error: function (data) {  
  15.                     alert('search error');  
  16.                 }  
  17.             });  
  18. _spPageContextInfo Object Properties found for SharePoint 2013 application:  
  19. alertsEnabled: false  
  20. allowSilverlightPrompt :"True"  
  21. clientServerTimeDelta :-8980  
  22. crossDomainPhotosEnabled: false  
  23. currentCultureName: "en-US"  
  24. currentLanguage: 1033  
  25. currentUICultureName: "en-US"  
  26. layoutsUrl: "_layouts/15"  
  27. pageListId:  "{df5e2d45-ad0b-4608-a813-ded6a97511db}"  
  28. pagePersonalizationScope:  1  
  29. serverRequestPath:  "/sites/ SharepointSite /Lists/ custom/ custom.aspx"  
  30. siteAbsoluteUrl: "http://svr:2525/sites/ SharepointSite "  
  31. siteClientTag: "0$$15.0.4433.150615.0.4433.1506"  
  32. siteServerRelativeUrl; "/sites/ SharepointSite "  
  33. systemUserKey: "i:0).w|s-1-5-21-2086049...90378103-3194450012-500"  
  34. tenantAppVersion: "0"  
  35. userId: 1  
  36. webAbsoluteUrl: "http:// svr:2525/sites /sites/SharepointSite"  
  37. webLanguage: 1033  
  38. webLogoUrl:"_layouts/15/images/siteicon.png"  
  39. webPermMasks: Object { High=2147483647, Low=4294967295}  
  40. webServerRelativeUrl: "/sites/ SharepointSite "  
  41. webTemplate: "1"  
  42. webTitle : " SharepointSite " 
Ebook Download
View all
Learn
View all