Actually, It’s Already In The SharePoint PageContext!

What is SharePoint PageContext ?

SharePoint PageContext provides standard definitions for SharePoint objects shared between the client-side applications, web parts, and other components. Typically the data is fetched via REST queries when navigating to a new page. Additionally, it can be preloaded by the web server or filled from a custom application cache.

How to access SharePoint PageContext?

In classic SharePoint pages, we can access it directly in the SharePoint page via _spPageContextInfo window variable.

In modern SharePoint, we needed to scaffold our projects to create custom WebParts and customizations via SharePoint generator. The object is passed down from the BaseClientSideWebPart inherited from @microsoft/sp-component-base

 const element: React.ReactElement<ITestWebPartProps > = React.createElement(
      TestWebPart,
      {
        description: this.properties.description,
        context: this.context,
        title: this.properties.title,
        displayMode: this.displayMode,
        updateProperty: (value: string) => {
          this.properties.title = value;
        }
      }
    );

Below is the screenshot of the PageContext accessible from your WebPart scaffolds we just passed in the code above.

SharePoint pageContext

Seeing those properties, you can get the data right away even without doing an extra query! You can actually use this if you need an ID reference to what you are looking for.

SharePoint PageContext in Table

PropertiesWhat’s inside ?
aadInfoContextual information for communicating with Azure Active Directory. If the current page doesn’t have an associated Azure Active Directory tenant, this property will be undefined.
cultureInfoIt provides culture info for the current user of the application. This class is primarily used with the PageContext class.
isInitializedReturns whether the PageContext has been initialized.
legacyPageContextAn object providing classic SharePoint properties that may be required by certain legacy scripts.
listContextual information for the SharePoint list that is hosting the page. If there is no list associated to the current page, this property will be undefined.
listItemContextual information for the SharePoint list item that is hosting the page. If there is no list item associated to the current page, this property will be undefined.
serviceKeyThe service key for PageContext.
siteContextual information for the SharePoint site collection (“SPSite”) that is hosting the page.
userIt provides contextual information for the SharePoint user that is accessing the page. This class is primarily used with the PageContext class.
webContextual information for the SharePoint site (“SPWeb”) that is hosting the page.
How useful is the SharePoint PageContext ?

Very useful! You can do all sorts of validations and checks without doing a lot of queries and awaits!

  1. Below is a list of common usage on how you can optimize the SharePoint Page context. The list is of course based on my experiences.
    • Check if the current user is external user.
    • Get the users basic information, email, ID, time Zone, and Display Name.
    • Get the Azure Active Directory Tenant and User ID.
    • Identify which site page a current user is accessing.
    • The current permission a user has.
    • Access the SharePoint page color theme!
    • Get the current list you are in and the users permissions.
    • Check if the current user is an Administrator, is an Owner or has actually a permission to edit and more.
    • Get the associated Hub Site ID.
    • Produce the current users photo URL.
    • Produce a URL for the current users Delve site / Personal Site.
    • Get the current users preferred language.

Now that is one useful tip! For questions and clarifications, please write it as a comment below. Have a nice day! 

References : PageContext class


Accounting.js Automation Collaboration Competitors Connect Content Type Create List Item Design Flows Hillbilly Tabs Issues Javascript Limitation Limitations Microsoft Teams ModernScriptEditor NodeJs Numeral.js O365 Office 365 OneDrive People PnP Power Automate PowerShell Pwermissions Rest API Rest Endpoint ScriptEditor Send an HTTP Request Send an HTTP Request to SharePoint SharePoint SharePoint Architecture SharePoint Designs SharePoint Modern SharePoint Online SharePoint Tabs ShellScript SPFX SPO Sync Teams TypeScript Versioning Workflows

9 thoughts on “Actually, It’s Already In The SharePoint PageContext!”

  1. That’s quite a load of information! I actually know of the pageContext but never thought there is actually information for the Site Theme!

  2. Pingback: How to check if current user is external in SharePoint - tipsbybits.com

  3. Pingback: How to Enable Saving Templates in SharePoint - tipsbybits.com

  4. Pingback: Expand Person metadata column in SharePoint with PNP Spfx - tipsbybits.com

  5. Very useful for sure. Have been using for a while, but one thing you mention I am not able to get is if the current user is a site admin. That used to work, but can’t seem to find the location for where this is.

Leave a Comment

Your email address will not be published. Required fields are marked *