SharePoint Page Context Details in Modern Script Editor

I've used Copilot to compile how to retrieve various page context details in SharePoint Online using the _spPageContextInfo variable. This is useful for SharePoint admins and web developers who want to access page-specific information on their SharePoint pages.

Reminder: Ensure that the "Enable classic _spPageContextInfo" setting is enabled in the Modern Script Editor web part to access the _spPageContextInfo object.

1. Web Server Relative URL

To get the web server relative URL, which is a string, use the following JavaScript code:

var webServerRelativeUrl = _spPageContextInfo.webServerRelativeUrl; console.log("Web Server Relative URL: " + webServerRelativeUrl);

2. Site Absolute URL

To get the site absolute URL, which is a string, use the following JavaScript code:

var siteAbsoluteUrl = _spPageContextInfo.siteAbsoluteUrl; console.log("Site Absolute URL: " + siteAbsoluteUrl);

3. Web Title

To get the web title, which is a string, use the following JavaScript code:

var webTitle = _spPageContextInfo.webTitle; console.log("Web Title: " + webTitle);

4. Web Logo URL

To get the web logo URL, which is a string representing the URL of the web's logo, use the following JavaScript code:

var webLogoUrl = _spPageContextInfo.webLogoUrl; console.log("Web Logo URL: " + webLogoUrl);

5. Current Language

To get the current language, which is a number representing the language code, use the following JavaScript code:

var currentLanguage = _spPageContextInfo.currentLanguage; console.log("Current Language: " + currentLanguage);

6. Page List ID

To get the page list ID, which is a string, use the following JavaScript code:

var pageListId = _spPageContextInfo.pageListId; console.log("Page List ID: " + pageListId);

7. Page Item ID

To get the page item ID, which is a number, use the following JavaScript code:

var pageItemId = _spPageContextInfo.pageItemId; console.log("Page Item ID: " + pageItemId);

8. Web Template

To get the web template, which is a string, use the following JavaScript code:

var webTemplate = _spPageContextInfo.webTemplate; console.log("Web Template: " + webTemplate);

Further Reading

If you have questions about accessing the logged in user details from the _spPageContextInfo object, please refer to this article for more information.