0

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 '19 at 9:17
  • _spPageContextInfo.currentLanguage, _spPageContextInfo.webAbsoluteUrl, _spPageContextInfo.webServerRelativeUrl, _spPageContextInfo.listUrl, _spPageContextInfo.siteAbsoluteUrl, _spPageContextInfo.listID, are few objects
    – user4205
    Dec 11 '19 at 9:23
  • Are you not able to use legacyPageContext in your code? Dec 11 '19 at 9:25
  • You mean this.context.pageContext.legacyPageContext ? No, get undefined error for pageContext in javascript
    – user4205
    Dec 11 '19 at 9:26
4

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.
2
  • 1
    Solution 2 worked for me, thanks!!
    – Alberto S.
    Jun 5 '20 at 9:47
  • Hi, does this answers your question? If yes, Please Upvote(^) and accept as an Answer so that it will help others with same question to find the correct answer easily. It also removes the question from "Unanswered questions" list. Feb 14 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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