Secrets Behind SharePoint Online – Day 2

Unbind the secret behind to get context information for the modern site page.

Today we will see how to get the current context information from the Modern Page. In Classical pages and other than modern site pages, we can get the current web and user context information using the _spPageContextInfo variable.

But when we try to access the same variable in Modern site pages, we will get an undefined error. If we change the page to view source mode, we will see this _spPageContextInfo javascript variable is commented. Due to that we won’t access the current context information.

There is a workaround is available and that too has come from SharePoint. We have to use the full Modern Page URL to send the request along with “as=json” as a query string.

Format of the Request URL:

  • http://domaoin.sharepoint.com/sites/name/sitepages/modernpage.aspx
  • http://domaoin.sharepoint.com/sites/name – If the modern page is the home page of the site

To test this, navigate to the modern site page. Then open the browser console and paste the below code snippet,

Shantha Kumar
Shantha Kumar
Articles: 278