SOLVED

PnP JavaScript Core - Only for Web Parts?

%3CLINGO-SUB%20id%3D%22lingo-sub-17663%22%20slang%3D%22en-US%22%3EPnP%20JavaScript%20Core%20-%20Only%20for%20Web%20Parts%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-17663%22%20slang%3D%22en-US%22%3E%3CP%3EI'd%20like%20to%20use%20the%20PnP%20JS%20Core%20library%20just%20to%20speed%20up%20my%20development%20time%20versus%20something%20like%20jQuery.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20got%20my%20app%20working%20within%20a%20web%20part%20as%20shown%20in%20samples%2C%20but%20this%20application%20needs%20to%20load%20on%20every%20single%20page.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAfter%20adding%20my%20app.js%20file%20to%20a%20User%20Custom%20Action%20(ScriptLink)%2C%20I%20now%20receive%20404's%20on%20the%20GET%20and%20POST%20requests.%20It%20looks%20like%20pnp.js%20isn't%20parsing%20down%20to%20the%20root%20of%20the%20current%20site%20properly.%20Is%20it%20possible%20to%20manually%20set%20the%20root%20url%3F%20Should%20I%20be%20doing%20this%20differently%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EExample%20in%20Web%20Part%3A%3C%2FP%3E%3CP%3Ehttps%3A%2F%2Fsharepoint%2Fsites%2Fsomesite%2F_api%2Fsite%2Frootweb%2Flists%2FgetByTitle('somelist')%3CSPAN%3E%2Fgetitems%3F%24expand%3D%3C%2FSPAN%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EExample%20in%26nbsp%3BUser%20Custom%20Action%20on%20home%20page%3A%3C%2FP%3E%3CP%3Ehttps%3A%2F%2Fsharepoint%2Fsites%2Fsomesite%3CSTRONG%3E%2FSitePages%2F%3C%2FSTRONG%3E_api%2Fsite%2Frootweb%2Flists%2FgetByTitle('somelist')%2Fgetitems%3F%24expand%3D%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks!!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-23138%22%20slang%3D%22en-US%22%3ERe%3A%20PnP%20JavaScript%20Core%20-%20Only%20for%20Web%20Parts%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-23138%22%20slang%3D%22en-US%22%3EWhoops%2C%20I%20bet%20Github%20would've%20been%20a%20much%20better%20spot%20for%20this!%3CBR%20%2F%3E%3CBR%20%2F%3EThanks%20Patrick!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-21975%22%20slang%3D%22en-US%22%3ERe%3A%20PnP%20JavaScript%20Core%20-%20Only%20for%20Web%20Parts%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-21975%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Bradley%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESorry%20to%20hear%20you%20had%20some%20trouble%20with%20the%20library%20-%20I%20didn't%20see%20this%20until%20someone%20linked%20it%20in%20the%20issues%20list.%20I%20took%20a%20swing%20at%20responding%20there%2C%20please%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FOfficeDev%2FPnP-JS-Core%2Fissues%2F222%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Ehave%20a%20look%3C%2FA%3E%20-%20but%20have%20also%20copied%20my%20response%20here%20for%20folks.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20have%20a%20guess%20at%20what%20is%20happening%20here.%20The%20usercustomaction%20code%20is%20executed%20before%20the%20_spPageContextInfo%20is%20defined%20on%20the%20page.%20If%20that%20happens%20there%20isn't%20much%20we%20can%20do.%20We%20don't%20%22parse%22%20the%20url%20at%20all%2C%20we%20just%20look%20for%20that%20global%20var.%3C%2FP%3E%0A%3CP%3EStarting%20with%201.0.5%20(just%20released%20this%20week)%20you%20can%20export%20the%20Web%20object%20and%20set%20it%20to%20any%20url%20you%20like%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3Eimport%20%7B%20Web%20%7D%20from%20%22sp-pnp-js%22%3B%0Alet%20w%20%3D%20new%20Web(%22https%3A%2F%2Fsharepoint%2Fsites%2Fsomesite%22)%3B%0Aw.get().then(...)%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EWhich%20would%20allow%20you%20to%20determine%20the%20url%20to%20use%20based%20on%20a%20given%20scenario.%20The%20reason%20we%20don't%20try%20and%20guess%20at%20the%20url%20is%20that%20method%20is%20bound%20to%20fail%20too%20often.%20So%20if%20we%20can't%20find%20that%20global%20var%20mentioned%20above%20we%20just%20make%20the%20requests%20to%20%2F_api%2F...%20which%20then%20in%20the%20case%20you%20linked%20resolves%20to%20the%20SitePages%20folder.%3C%2FP%3E%0A%3CP%3ESo%20two%20solutions%20to%20try%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EWait%20for%20the%20entire%20page%20to%20load%20before%20making%20the%20requests%20(i.e.%20using%20%24(function()%20%7B%7D)%20or%20similar%3C%2FLI%3E%0A%3CLI%3ETry%20using%20the%20Web%20object%20directly%20and%20setting%20the%20url%20you%20want%20explicitly.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EPlease%20let%20us%20know%20if%20either%20of%20those%20are%20helpful.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-21226%22%20slang%3D%22en-US%22%3ERe%3A%20PnP%20JavaScript%20Core%20-%20Only%20for%20Web%20Parts%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-21226%22%20slang%3D%22en-US%22%3E%3CP%3ENope%2C%20I%20ended%20up%20just%20rewriting%20the%20app%20in%20jQuery.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-21036%22%20slang%3D%22en-US%22%3ERe%3A%20PnP%20JavaScript%20Core%20-%20Only%20for%20Web%20Parts%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-21036%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20Bradley%20!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20the%20same%20problem%20here.%20Did%20you%20find%20a%20solution%20to%20this%20problem%20%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBest%20regards%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAur%C3%A9lien%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-17964%22%20slang%3D%22en-US%22%3ERe%3A%20PnP%20JavaScript%20Core%20-%20Only%20for%20Web%20Parts%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-17964%22%20slang%3D%22en-US%22%3E%3CP%3EIs%20there%20a%20method%20to%20set%20the%20site%20URL%20via%20something%20like%20%24pnp.config%2C%20%24pnp.setup%2C%20%24pnp.storage%3F%20I%20explored%20the%20DOM%20a%20bit%20but%20I%20couldn't%20find%20something%20that%20sets%20the%20site%20URL.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20a%20bit%20of%20clarification%20on%20what%20I'm%20doing.%20I'm%20using%20the%20same%20app.js%20file%20in%20both%20scenarios.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20JS%20looks%20like%20this%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24pnp.sp.site.rootWeb.lists.getByTitle(%22somelist%22).getItemsByCAMLQuery(%7B%20ViewXml%3A%20camlQuery%20%7D).then(function(result)%20%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%2F%2Fmore%20stuff%20here%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%7D)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CUL%3E%3CLI%3ELoad%20app.js%20via%20web%20part%3COL%3E%3CLI%3ECreate%20site%20page%3C%2FLI%3E%3CLI%3ECreate%20app.html%20with%20%3CSCRIPT%3E%20tags%20pointing%20at%20jquery.js%2C%20pnp.js%2C%20and%20app.js%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FLI%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BLI%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3BAdd%20Content%20Editor%20Web%20Part%20and%20point%20it%20add%20app.html%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FLI%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BLI%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3BLoad%20page%20and%20app.js%20makes%20calls%20to%20the%20correct%20REST%20endpoints%20-%26amp%3Bamp%3Bamp%3Bamp%3Bnbsp%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BSPAN%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3Bhttps%3A%2F%2Fsharepoint%2Fsites%2Fsomesite%2F_api%2Fsite%2Frootwe%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FSPAN%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BSPAN%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3Bb%2Flists%2FgetByTitle(%26amp%3Bamp%3Bamp%3Bamp%3Bamp%3B%2339%3Bsomelist%26amp%3Bamp%3Bamp%3Bamp%3Bamp%3B%2339%3B)%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FSPAN%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BSPAN%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%2Fgetitems%3F%24expand%3D%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FSPAN%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Bnbsp%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FLI%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FOL%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FLI%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BLI%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3BLoad%20app.js%20via%20User%20Custom%20Action%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BOL%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BLI%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3BAdd%20Custom%20Action%20for%20pnp.js%2C%20sequence%2010100%2C%20scoped%20to%20site%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FLI%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BLI%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3BAdd%20Custom%20Action%20for%20jquery.js%2C%20sequence%2010101%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BSPAN%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%2C%20scoped%20to%20site%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FSPAN%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FLI%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BLI%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3BAdd%20Custom%20Action%20for%20app.js%2C%20sequence%2010102%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BSPAN%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%2C%20scoped%20to%20site%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FSPAN%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FLI%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BLI%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3BLoad%20home%20page%20and%20app.js%20makes%20calls%20to%20the%20incorrect%20REST%20endpoints%20-%26amp%3Bamp%3Bamp%3Bamp%3Bnbsp%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BSPAN%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3Bhttps%3A%2F%2Fsharepoint%2Fsites%2Fsomesite%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FSPAN%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BSTRONG%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%2FSitePages%2F%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FSTRONG%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BSPAN%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B_api%2Fsite%2Frootweb%2Flists%2FgetByTitle(%26amp%3Bamp%3Bamp%3Bamp%3Bamp%3B%2339%3Bsomelist%26amp%3Bamp%3Bamp%3Bamp%3Bamp%3B%2339%3B)%2Fget%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FSPAN%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BSPAN%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3Bitems%3F%24expand%3D%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FSPAN%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Bnbsp%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FLI%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FOL%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FLI%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FUL%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2Flingo-body%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3Blingo-sub%20id%3D%22lingo-sub-17770%22%20slang%3D%22en-US%22%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3BRe%3A%20PnP%20JavaScript%20Core%20-%20Only%20for%20Web%20Parts%3F%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2Flingo-sub%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3Blingo-body%20id%3D%22lingo-body-17770%22%20slang%3D%22en-US%22%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3BP%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3BCould%20you%20remove%20the%20%2FSitepages%2F%20as%20this%20is%20a%20url%20which%20does%20not%20work%20with%20the%20_api%20as%20far%20as%20i%20know.%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2FP%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%26amp%3Bamp%3Bamp%3Bamp%3Blt%3B%2Flingo-body%26amp%3Bamp%3Bamp%3Bamp%3Bgt%3B%3C%2FSCRIPT%3E%3C%2FLI%3E%3C%2FOL%3E%3C%2FLI%3E%3C%2FUL%3E%3C%2FLINGO-BODY%3E
New Contributor

I'd like to use the PnP JS Core library just to speed up my development time versus something like jQuery.

 

I got my app working within a web part as shown in samples, but this application needs to load on every single page.

 

After adding my app.js file to a User Custom Action (ScriptLink), I now receive 404's on the GET and POST requests. It looks like pnp.js isn't parsing down to the root of the current site properly. Is it possible to manually set the root url? Should I be doing this differently?

 

Example in Web Part:

https://sharepoint/sites/somesite/_api/site/rootweb/lists/getByTitle('somelist')/getitems?$expand= 

 

Example in User Custom Action on home page:

https://sharepoint/sites/somesite/SitePages/_api/site/rootweb/lists/getByTitle('somelist')/getitems?$expand= 

 

Thanks!!

6 Replies

Could you remove the /Sitepages/ as this is a url which does not work with the _api as far as i know.

Is there a method to set the site URL via something like $pnp.config, $pnp.setup, $pnp.storage? I explored the DOM a bit but I couldn't find something that sets the site URL.

 

For a bit of clarification on what I'm doing. I'm using the same app.js file in both scenarios.

 

The JS looks like this: 

 

$pnp.sp.site.rootWeb.lists.getByTitle("somelist").getItemsByCAMLQuery({ ViewXml: camlQuery }).then(function(result) {

 

//more stuff here

 

});

 

 

  • Load app.js via web part
    1. Create site page
    2. Create app.html with <script> tags pointing at jquery.js, pnp.js, and app.js
    3. Add Content Editor Web Part and point it add app.html
    4. Load page and app.js makes calls to the correct REST endpoints - https://sharepoint/sites/somesite/_api/site/rootweb/lists/getByTitle('somelist')/getitems?$expand= 
  • Load app.js via User Custom Action
    1. Add Custom Action for pnp.js, sequence 10100, scoped to site
    2. Add Custom Action for jquery.js, sequence 10101, scoped to site
    3. Add Custom Action for app.js, sequence 10102, scoped to site
    4. Load home page and app.js makes calls to the incorrect REST endpoints - https://sharepoint/sites/somesite/SitePages/_api/site/rootweb/lists/getByTitle('somelist')/getitems?$expand= 

Hello Bradley !

 

I have the same problem here. Did you find a solution to this problem ?

 

Best regards,

 

Aurélien

 

Nope, I ended up just rewriting the app in jQuery.

best response confirmed by Bradley Griffin (New Contributor)
Solution

Hi Bradley,

 

Sorry to hear you had some trouble with the library - I didn't see this until someone linked it in the issues list. I took a swing at responding there, please have a look - but have also copied my response here for folks.

 

I have a guess at what is happening here. The usercustomaction code is executed before the _spPageContextInfo is defined on the page. If that happens there isn't much we can do. We don't "parse" the url at all, we just look for that global var.

Starting with 1.0.5 (just released this week) you can export the Web object and set it to any url you like:

import { Web } from "sp-pnp-js";
let w = new Web("https://sharepoint/sites/somesite");
w.get().then(...)

Which would allow you to determine the url to use based on a given scenario. The reason we don't try and guess at the url is that method is bound to fail too often. So if we can't find that global var mentioned above we just make the requests to /_api/... which then in the case you linked resolves to the SitePages folder.

So two solutions to try:

  1. Wait for the entire page to load before making the requests (i.e. using $(function() {}) or similar
  2. Try using the Web object directly and setting the url you want explicitly.

Please let us know if either of those are helpful.

Whoops, I bet Github would've been a much better spot for this!

Thanks Patrick!