Hello SharePointers,
Today in this article we will see about how to get current context from SharePoint Modern site Page.

In classic pages, and anything other than Modern site Pages,, we can get the current context of web and user using “_spPageContextInfo” variable attribute.

The thing is that, when we try to do the same thing, like access “_spPageContextInfo” variable in Modern Pages, we will get Undefined error.

send approval request to a group in MS Flow.

As a developer, If we change the page mode to “View Source”, we will see that the “_SPPageContextInfo” variable is disabled or commented. Because of this behavior we won’t able to access Current web context information.

The Good news is we have a workaround available and that also comes from SharePoint only. To achieve this we need to use the full URL of a Modern Page to send a request with “as=JSON” query string.

Below is the Format of Request URL

  • https://<yourtenant>.SharePoint.com/sites/<siteName>/SitePages/ModernPage.aspx
  • https://<yourtenant>.sharepoint.com/sites/<siteName> — This is only used when your home page is a Modern Page

Now we need to test this behavior. Please Navigate to the Modern page of your site.

Retrieve all Communication Sites using PnP PowerShell

Open Developer Tools console.

We need to open a developer Console from Browser like Google Chrome or Internet Explorer.

To do this Please follow below steps.

  1. Go to your SharePoint site Modern Page
  2. Press “F12″ button from keyboard
  3. Select “Console”
Get Current Context from SharePoint Modern Site Page
Developer Console Image

Now as you navigate to the console, then just copy and Paste below code snippet in the Console window.

function getRequest(url) {    
    var request = new XMLHttpRequest();    
    return new Promise(function(resolve, reject) {    
        request.onreadystatechange = function() {    
            if (request.readyState !== 4) return;    
            if (request.status >= 200 && request.status < 300) {    
                resolve(request);    
            } else {    
                reject({    
                    status: request.status,    
                    statusText: request.statusText    
                });    
            }    
        };    
    
        request.open('GET', url, true);    
        request.setRequestHeader("Content-Type", "application/json;charset=utf-8");    
        request.setRequestHeader("ACCEPT", "application/json; odata.metadata=minimal");    
        request.setRequestHeader("ODATA-VERSION", "4.0");    
        request.send();    
    
    });    
}    
    
//Get the Request location from the browser URL    
var path = location.href.replace(location.search, "") + "?as=json";    
    
//Returns the current user, item, page and context information    
getRequest(path).then(function(response) {    
    console.log(JSON.parse(response.response));    
});

That’s It!! This will return the current user context information, properties of current page, current web properties and current Item Properties.

I hope now you should learn how to Get Current Context from SharePoint Modern Site Page.

Follow SharePoint Gems for such wonderful articles.

find more details on this at c-sharp corner.

Please comment.
One thought on “Get Current Context from SharePoint Modern Site Page”

Leave a Reply