0

Hey SPFx devs I am encountering some issues with security tokens in a small SPA I have developed on top of the SPFx framework which interacts with data in SharePoint online.

If the app is left idle or only GET requests are performed it seems like the security token goes stale and POST requests to update or insert records fail (either a 400 or a 403 error). If I refresh the page then try the same action it succeeds.

I'm using the SPFx framework with React and sp-pnp-js library to perform the REST actions.

Here's a code snippet from my TS service:

public async add(newProject){
    try{
      let res = await pnp.sp.web.lists.getByTitle(this.listName).items.add(newProject);
      return({message: "Success", data: res.data});
    }
    catch(e){
      console.log(e);
      return({message: "Error", data: e.data.responseBody['odata.error'].message.value});
    }
  }

As you can see I'm just using native sp-pnp-js functions, I can rewrite them with the HTTP client manually if it makes a difference...

Any help would be appreciated :) Thanks in advance!

Chris Drappier
  • 5,280
  • 10
  • 40
  • 64
Andrew
  • 1
  • 1

2 Answers2

0

Are you setting the Pnp Js context?

sp.setup({
  spfxContext: this.props.context
});
Joao Livio
  • 89
  • 1
  • 1
  • 13
0

There is a known issue with SharePoint Framework ServiceScope object and access tokens as outlined in this github issue: https://github.com/pnp/pnpjs/issues/2379

The workaround if you are using latest PnP and spfi object is to use the RequestDigest() behavior when setting up, like this:

sp = spfi().using(RequestDigest())
Fedor
  • 17,146
  • 13
  • 40
  • 131