SOLVED

_spPageContextInfo is undefined

%3CLINGO-SUB%20id%3D%22lingo-sub-153892%22%20slang%3D%22en-US%22%3E_spPageContextInfo%20is%20undefined%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-153892%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20very%20simple%20REST%20call%20to%20get%20all%20of%20the%20items%20in%20a%20list(Announcements)%20but%20when%20I%20run%20this%20Mikael's%20script%20editor%20web%20part%20I%20get%20this%20error%26nbsp%3B%3CSPAN%3E_spPageContextInfo'%20is%20not%20defined%3C%2FSPAN%3E.%20where%20_spPageContextInfo%20is%20the%20current%20url%20of%20the%20page%20I%20am%20in).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhat%20am%20I%20doing%20wrong%20(I%20am%20using%20modern%20pages)%20%3F%3CBR%20%2F%3E%3CBR%20%2F%3E%24.ajax(%7B%3CBR%20%2F%3E%26nbsp%3B%20url%3A%20_spPageContextInfo.webAbsoluteUrl%20%2B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22%2F_api%2Fweb%2Flists%2Fgetbytitle('Announcements')%2Fitems%22%2C%3CBR%20%2F%3E%26nbsp%3B%20method%3A%20%22GET%22%2C%3CBR%20%2F%3E%26nbsp%3B%20headers%3A%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22Accept%22%3A%20%22application%2Fjson%3B%20odata%3Dverbose%22%3CBR%20%2F%3E%26nbsp%3B%20%7D%2C%3CBR%20%2F%3E%26nbsp%3B%20success%3A%20function(data)%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20console.log(data.d.results)%3B%3CBR%20%2F%3E%26nbsp%3B%20%7D%2C%3CBR%20%2F%3E%26nbsp%3B%20error%3A%20function(data)%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20alert(JSON.stringify(error))%3B%3CBR%20%2F%3E%26nbsp%3B%20%7D%3CBR%20%2F%3E%7D)%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-153892%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EREST%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-306995%22%20slang%3D%22en-US%22%3ERe%3A%20_spPageContextInfo%20is%20undefined%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-306995%22%20slang%3D%22en-US%22%3E%3CP%3EWe%20have%203%20cloud%20tenant%20with%203%20different%20domains.%20For%20one%20cloud%20tenant%20_spPageContextInfo%20is%20completely%20blank.%20Will%20this%20be%20due%20to%20improper%20AD%20synch%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-154197%22%20slang%3D%22en-US%22%3ERe%3A%20_spPageContextInfo%20is%20undefined%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-154197%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%40NigelWitherdin%20Thanks%20for%20explaining%20why%20_spPageContextInfo%20does%20not%20wok%20in%20modern%20pages.%26nbsp%3B%20I%20missed%20that.%26nbsp%3B%20I%20agree%20with%20you%20the%20way%20forward%20is%20to%20use%20the%20SPFx%20webpart.%26nbsp%3B%20But%20for%20a%20quick%20Proof%20of%20Concept%20Mikael's%20webpart%20is%20fine.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERegards%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENigel%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-154092%22%20slang%3D%22en-US%22%3ERe%3A%20_spPageContextInfo%20is%20undefined%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-154092%22%20slang%3D%22en-US%22%3E%3CP%3EDoes%20it%20work%26nbsp%3Bon%20a%20classic%20page%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20thread%20(%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMicrosoft%2Ftypes-publisher%2Fissues%2F363%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FMicrosoft%2Ftypes-publisher%2Fissues%2F363%3C%2FA%3E)%20it%20is%20mentioned%20that%20the%20SharePoint%20constants%20(like%20%3CSPAN%3E_spPageContextInfo%3C%2FSPAN%3E)%20are%20not%20supported%20in%20modern%20pages.%20I%20think%26nbsp%3Bthat%20(as%20useful%20as%20it%20may%20be)%20Mikael%20Svensson's%20Spfx%20web%20part%20that%20allows%20you%20to%20host%20JavaScript%20on%20a%20modern%20page%20should%20probably%20be%20thought%20of%20as%20a%20hack%2C%20and%20wouldn't%20be%20considered%20supported%20by%20MS.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20supported%20solution%20would%20be%20to%20create%20an%20Spfx%20web%20part%20for%20your%20customization%2C%20and%20use%20the%20REST%20call%20from%20within%20it.%20Within%20the%20Spfx%20web%20part%20you%20can%20get%20the%20current%20page's%20context%26nbsp%3Busing%20%22%3CSPAN%3Ethis%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3Econtext%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3EpageContext%22%20-%20so%20you%20can%20use%3C%2FSPAN%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%0A%3CDIV%3E%3CSPAN%3E%22this%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3Econtext%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3EpageContext%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3Eweb%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3EabsoluteUrl%22%20to%20get%20the%20URL%20of%20the%20current%20web%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%3EThanks%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%3ENigel%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E
Highlighted
Regular Contributor

I have a very simple REST call to get all of the items in a list(Announcements) but when I run this Mikael's script editor web part I get this error _spPageContextInfo' is not defined. where _spPageContextInfo is the current url of the page I am in).

 

What am I doing wrong (I am using modern pages) ?

$.ajax({
  url: _spPageContextInfo.webAbsoluteUrl +
    "/_api/web/lists/getbytitle('Announcements')/items",
  method: "GET",
  headers: {
    "Accept": "application/json; odata=verbose"
  },
  success: function(data) {
    console.log(data.d.results);
  },
  error: function(data) {
    alert(JSON.stringify(error));
  }
});

3 Replies
Highlighted
Solution

Does it work on a classic page?

 

In this thread (https://github.com/Microsoft/types-publisher/issues/363) it is mentioned that the SharePoint constants (like _spPageContextInfo) are not supported in modern pages. I think that (as useful as it may be) Mikael Svensson's Spfx web part that allows you to host JavaScript on a modern page should probably be thought of as a hack, and wouldn't be considered supported by MS.

 

The supported solution would be to create an Spfx web part for your customization, and use the REST call from within it. Within the Spfx web part you can get the current page's context using "this.context.pageContext" - so you can use 

"this.context.pageContext.web.absoluteUrl" to get the URL of the current web
 
Thanks
 
Nigel
Highlighted

Hi @NigelWitherdin Thanks for explaining why _spPageContextInfo does not wok in modern pages.  I missed that.  I agree with you the way forward is to use the SPFx webpart.  But for a quick Proof of Concept Mikael's webpart is fine.

 

Regards

 

Nigel

Highlighted

We have 3 cloud tenant with 3 different domains. For one cloud tenant _spPageContextInfo is completely blank. Will this be due to improper AD synch