Pnpjs: @pnp/nodejs and @pnp/sp + SPO add-in list items always empty

Created on 23 Jul 2018  路  7Comments  路  Source: pnp/pnpjs

Category

  • [ ] Enhancement
  • [ ] Bug
  • [ x ] Question
  • [ ] Documentation gap/issue

Version

Please specify what version of the library you are using: [ 1.1.2 ]

Observed Behavior

Trying to use @pnp/nodejs and @pnp/sp and unable to retrive list tems

I have followed the instructions provided here:
https://pnp.github.io/pnpjs/nodejs/sp-fetch-client.html

And here:
https://pnp.github.io/pnpjs/sp/items.html

I have setup the add-in and provided the relevant permissions in the SharePoint Online site collection.

When executing the code below - the "list1" object is returned correctly but when querying the "items" it's always returning an empty array.

Also, the "Add Item" is returning a 503 Forbidden.

Is it not possible to have a standalone Node.js (Angular 6) app querying a SharePoint Online site?

What am I doing wrong?

Thanks for any help you can provide, I'm stuck...

import { sp } from "@pnp/sp";
import { SPFetchClient } from "@pnp/nodejs";

var settings = {
sp: {
id: "XXX",
secret: "XXX",
url: "https://some_site_collection_root_url",
}
}

sp.setup({
sp: {
fetchClientFactory: () => {
return new SPFetchClient(settings.sp.url, settings.sp.id, settings.sp.secret);
},
},
});

// WORKS AND RETURNS list1 valid object
var list1 = sp.web.lists.getByTitle("List Name");
console.log(JSON.stringify(list1, null, 4));

// DOES NOT WORK => Always returns Array(0) [] even though there are list items in the list
sp.web.lists.getByTitle("List Name").items.get().then((response) => {
console.log(response);
});

// DOES NOT WORK => Returns a 503 Forbidden error or I have also seen a 400 bad request...
sp.web.lists.getByTitle("List Name").items.add({
Title: "Test1"
}).then((result) => {
console.log(result);
}).catch(console.log);

documentation details needed question

Most helpful comment

If this may help at all I believe the root of the issue was that my AppPermissions were not well defined. I had put the URL of a site collection or list in the following (for instance):

<AppPermissionRequests> <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web/list" Right="Write"/> </AppPermissionRequests>

The "Scope" attribute must be exactly what is described in this article:
https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/add-in-permissions-in-sharepoint

NOT the URL of an existing site, web or list.

All is working for me now and this PNP SPO library is very powerful, nice work guys :)

Hope this helps

All 7 comments

Hi @lambiuk - yes it is definitely possible to run the code from nodejs. Can you create (or link existing) a repo with your code so we can easily reproduce the issue and see what is wrong? The most likely issue is the permissions aren't correct for the id and secret.

Going to close this as we didn't get enough detail to investigate. If you still need assistance please _reopen_ this issue with the code sample reproducing the issue and we'll have a look. Thanks!

I've got the exact same issue, and going crazy for the third day in a row. Any solution?

Hi @nda1974 - please open a new issue with all your details and we'll do our best to help out. As you can see we never really got enough details here to debug this one. Thanks!

If this may help at all I believe the root of the issue was that my AppPermissions were not well defined. I had put the URL of a site collection or list in the following (for instance):

<AppPermissionRequests> <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web/list" Right="Write"/> </AppPermissionRequests>

The "Scope" attribute must be exactly what is described in this article:
https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/add-in-permissions-in-sharepoint

NOT the URL of an existing site, web or list.

All is working for me now and this PNP SPO library is very powerful, nice work guys :)

Hope this helps

@lambiuk thank you VERY much, now its working.
And by the way I totally agree PNP Rocks thanks guys

Thanks @lambiuk for updating with your findings.

Was this page helpful?
0 / 5 - 0 ratings