Forum Discussion

Bradley Griffin's avatar
Bradley Griffin
Copper Contributor
Oct 03, 2016

PnP JavaScript Core - Only for Web Parts?

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 t...
  • Patrick Rodgers's avatar
    Patrick Rodgers
    Oct 13, 2016

    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.

Resources