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.
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);
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);
To get the web title, which is a string, use the following JavaScript code:
var webTitle = _spPageContextInfo.webTitle;
console.log("Web Title: " + webTitle);
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);
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);
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);
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);
To get the web template, which is a string, use the following JavaScript code:
var webTemplate = _spPageContextInfo.webTemplate;
console.log("Web Template: " + webTemplate);
If you have questions about accessing the logged in user details from the _spPageContextInfo
object, please refer to this article for more information.