Latest Posts

_spPageContextInfo Object In SharePoint

When programming in SharePoint using JavaScript - Client Side Object Model, we often need some properties related to the site to use in our code. By using _spPageContextInfo in SharePoint, you can avoid too many calls to SharePoint objects to get basic properties like Site Title, Logged-In user name, Site URL, etc….

How to use _spPageContextInfo

NOTE:

Every SharePoint version (SharePoint 2013, 2016, 2019, SharePoint Online, etc…) has different set of properties given by _spPageContextInfo. In this tutorial, _spPageContextInfo properties are listed that are exposed in SharePoint Online with Site Collection Administrator access. You can explore more on the SharePoint version you are using.

_spPageContextInfo variable is used when you are working with JavaScript Object Model.

  • Create a WebPart page and add a Script Editor Web part in it.
  • You can use below snippet in your code to access _spPageContextInfo properties. More properties are listed in section below.

<script type="text/javascript" >

   var WebAbsoluteURL = _spPageContextInfo.webabsoluteurl;

   var CurrentUserId = _spPageContextInfo.userid;

   var CurrentUserLoginName = _spPageContextInfo.userloginname;

</script>

If you want to see what properties are available on the SharePoint version that you are using,

  • Go to your site
  • Open Developer tools (F12)
  • Go to Console Tab (See the Screen shot below of chrome browser)
  • Write command _spPageContextInfo and press Enter.
  • All the properties will be listed.
  • _spPageContextInfo
  • You can also go to any site page >> view source (Right click on the page and click on view page source).
  • You will find variable _spPageContextInfo and all properties

_spPageContextInfo properties:

Site URL used: https://share11.sharepoint.com/sites/test/SitePages/home.aspx

SharePoint Version: SharePoint Online

Access of the logged-in User: Site Collection Administrator

Property

Property Value on execution on the above site

Description of the Key

User information

userDisplayName

Display Name

Gives the current user Display Name

userEmail

[email protected]

Gives the current user Email address

userLoginName

[email protected]

Gives the current user Login Name

SignOutUrl

https://share11.sharepoint.com/sites/test/_layouts/15/SignOut.aspx

This URL can be used to log out the existing/Logged-in user. You can put a Hyperlink on your SharePoint Site and on click, you can pass this URL. The logged-in user will be logged out after clicking on the link.

ProfileUrl

https://share11-my.sharepoint.com/person.aspx

This property will give profile URL of the current user

isSiteAdmin

TRUE

Checks if the current user is Site Admin or not

isSiteOwner

false

Checks if the current user is Site Owner or not

 

List Information

listTitle

Site Pages

Gives current List Title where _spPageContextInfo. listTitle is executed.

listUrl

/sites/test/SitePages

Gives current List URL where _spPageContextInfo. listTitle is executed.

  

Site Information

siteAbsoluteUrl

https://share11.sharepoint.com/sites/test

Gives absolute URL of the current Site

siteId

{98d343bf-7371-3eb4-8d02-69773987f869}

Give Site Id of the current Site.

siteServerRelativeUrl

/sites/test

Gives Server Relative URL of the current Site

 

Web Information

webAbsoluteUrl

https://share11.sharepoint.com/sites/test

Gives Absolute URL of the current Web

webDescription

Site Description

Gives a description of the current Web

webLanguage

1033

Gives the default language of the current web. 1033 = English

webLogoUrl

https://share11.sharepoint.com/sites/test/SiteAssets/untitled3.png

Gives Full path of the current site logo

webServerRelativeUrl

/sites/test

Gives current web server relative URL

webTemplateConfiguration

STS#0

Gives the name of the template which was used to create the current site. STS#0 = Team Site

webTitle

Site Title

Gives the Title of the current Web

 

Other 

PublishingFeatureOn

false

This property shows whether the site is Publishing site or if the publishing feature is activated on the site or not

RecycleBinItemCount

1

This property displays the total number of items in Recycle Bin

 

Navigation

navigationInfo.quickLaunch

 

This is an array object of all the links in Quick Launch Menu. You can get below information by exploring the array

 

AudienceIds: null

 

 

Children: []

Array Object of submenu under the quick launch menu

 

Id: 1031

Id of the quick launch link

 

IsDocLib: false

A boolean value indicating whether the link is of the document library or not

 

IsExternal: false

A boolean value indicating whether the link is external or not

 

ListTemplateType: 0

Template of the list if the link is any SharePoint list

 

ParentId: 1025

The ID of the parent link

 

Title: "Home"

Title of the link

 

Url: "/sites/test"

URL of the link

 

navigationInfo.topNav

 

This is an array object of all the links in Quick Launch Menu. You can get below information by exploring the array

 

AudienceIds: null

 

 

Children: []

Array Object of submenu under the top navigation menu

 

Id: 2005

Id of the top navigation link

 

IsDocLib: false

A boolean value indicating whether the link is of the document library or not

 

IsExternal: false

A boolean value indicating whether the link is external or not

 

ListTemplateType: 0

Template of the list if the link is any SharePoint list

 

ParentId: 1002

The ID of the parent link

 

Title: "Home"

Title of the link

 

Url: "/sites/test/"

URL of the link

 

Current Culture and Language Information

currentCultureLCID

1033

1033 = English - United States

currentCultureName

en-US

 

currentLanguage

1033

1033 = English - United States

_spPageContextInfo is undefined

Sometimes you may face an issue like, _spPageContextInfo is not defined. Below are some of the things you may look to find out the root cause.

  • Make sure you are not calling _spPageContextInfo properties before DOM is loaded. To ensure proper execution of the command, try to run it after document is ready ( $(document).ready(function) ) or document is loaded ( $(window).load(function() )
  • If SP.JS file is not loaded that can also cause issue while running _spPageContextInfo command. You can execute command as mentioned below.

<script type="text/javascript" >

   $( document ).ready(function() {

     SP.SOD.executeFunc('sp.js', 'SP.ClientContext', executeCommand );

   });

   function executeCommand()

   {

     var WebAbsoluteURL = _spPageContextInfo.webabsoluteurl;

     var CurrentUserId = _spPageContextInfo.userid;

     var CurrentUserLoginName = _spPageContextInfo.userloginname;

   }

</script>

  • SP.SOD.executeFunc ensures that the specified file (sp.js) that contains the specified function (SP.ClientContext) is loaded and then runs the specified callback function (executeCommand). Details about SP.SOD.executeFunc

Conclusion

You learned how to use _spPageContextInfo command in JavaScript Programming in SharePoint and some useful tips to troubleshoot in case of error while executing the command.


We value your Feedback:

Page URL:

Name:

Email:


Suggestion:

© 2024 Code SharePoint