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

  • 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. – Ganesh Sanap 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? – Ganesh Sanap 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

Try using below solutions:

Solution 1:

Use legacyPageContext to access the pageContext info:

// This is how you can access the page context info

// Retreive current user display name 

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 = (<any>window)._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.
| improve this answer | |

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.