1

Using SPFx extension (application customizer) added JS files to all SharePoint pages (including modern pages), something like mentioned in below blog:

Step by Step Procedure to Add a JS file to all pages in a SPFx Modern Pages using SharePoint Framework Extension.

I am using some custom JavaScript logic which runs in almost all pages, I am using _spPageContextInfo object extensively, it works fine in classic pages.

In modern page I get _spPageContextInfo is undefined.

Are there any alternative ways I can get _spPageContextInfo object value using normal JavaScript in modern pages.

In SPFx we can use this.context.pageContext.legacyPageContext but not in JavaScript

4
  • Can you please tell us what exactly you are fetching from _spPageContextInfo in your code? Maybe we can suggest you another way to get the same in SPFx. Dec 11, 2019 at 9:17
  • 1
    _spPageContextInfo.currentLanguage, _spPageContextInfo.webAbsoluteUrl, _spPageContextInfo.webServerRelativeUrl, _spPageContextInfo.listUrl, _spPageContextInfo.siteAbsoluteUrl, _spPageContextInfo.listID, are few objects
    – user4205
    Dec 11, 2019 at 9:23
  • Are you not able to use legacyPageContext in your code? Dec 11, 2019 at 9:25
  • You mean this.context.pageContext.legacyPageContext ? No, get undefined error for pageContext in javascript
    – user4205
    Dec 11, 2019 at 9:26

1 Answer 1

7

Try using below solutions:

Solution 1:

Use legacyPageContext to access the pageContext information:

// This is how you can access the page context info
this.context.pageContext.legacyPageContext;

// Retreive current user display name 
this.context.pageContext.legacyPageContext["userDisplayName"]);

Source: Access _spPageContextInfo details in SharePoint Framework - Check the list of properties you can access using legacyPageContext.

Solution 2:

Use _spPageContextInfo under window object:

const pageContext: any = (window as any)._spPageContextInfo;

Source: SPFx Extensions - Multi-Lingual Support.

Solution 3:

  1. Get the pageContext from this.context.pageContext.legacyPageContext;.
  2. Push the whole object under window or document object.
  3. Access it from window or document.
1
  • 2
    Solution 2 worked for me, thanks!!
    – Alberto S.
    Jun 5, 2020 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.