0

I'm new to graph API and trying to use graph API to read SP list items from a custom list using the current user's rights (not service account). But I'm getting an error. I have this so far:

In the package-solution.json file, I have this in the solution object:

"webApiPermissionRequests": [  
  {  
    "resource": "Microsoft Graph",  
    "scope": "Sites.Read.All"  
  }  
]  

In the helloworld.ts file I have this to init the graph API connection:

public async render(): Promise<void> {
const element: React.ReactElement<IHelloWorldProps> = React.createElement(
  HelloWorld,
  {
    description: this.properties.description,
    client: await this.context.msGraphClientFactory.getClient()
  }
);

ReactDom.render(element, this.domElement);
}

  public onInit(): Promise<void> {
    graph.setup({
      spfxContext: this.context
    });

  return super.onInit();
}

and in the helloworld.tsx file I have this:

let items = await props.client.api('/sites/TestSite/lists(\'MyFeatures\')/items').get();
console.log(items);

But I see this error in the dev tools console:

Uncaught (in promise) Error: To view the information on this page, ask a global or SharePoint admin in your organization to go to the API management page in the new SharePoint admin center.
    at e.getToken (chunk.aadTokenProvider_none_3888c2e89a4ae65276c0.js:1)
    at sp-webpart-workbench-assembly_en-us_6db5d03d6aef1b7d63505cff87b165b2.js:65

I haven't done anything on the administrative side to create any API registration or approve anything.

I was wondering do I need to do that, and if so, is there a tutorial for how to set it up? And is there anything more in the code I need to adjust like add a client id/secret or something?

1 Answer 1

1

As you are requesting for Sites.Read.All permissions in your web part, you have to approve this from SharePoint admin center --> API management.

  1. In the new SharePoint Admin Center, in the left quick launch menu, select the API management menu item. You'll see a page similar to the following:

    enter image description here

  2. Choose the permission that you requested in the package-solution.json file of your solution, select Approve or reject access, and then select Approve. The following screenshot shows the panel in the Admin UI:

    enter image description here

Source documentation: Consume the Microsoft Graph in the SharePoint Framework

5
  • I added the web part to the app catalogue and it told me to approve it from the API management centre. However when I went there, there was nothing listed in pending or approved. And it says there is an error Could not retrieve Global Service Principal ID Error: Error: {}. Researching that I found this eschrader.com/2020/06/23/… Do I need to do this?
    – omega
    Commented May 8, 2021 at 20:12
  • Also I found the command Enable-SPOTenantServicePrincipal. Do I need to enable this for the SharePoint Online Client Extensibility service principal? Is this needed to be enabled for the SPFx webparts to use graph api?
    – omega
    Commented May 8, 2021 at 20:12
  • Never faced this issue, you can try the suggestions given in this article to remove the bad service principal. After removing the bad registration, you should see the permissions request under API management page & you can approve it from there. So, no need to use the PowerShell commands. Commented May 9, 2021 at 13:18
  • According to this answer from a Microsoft Employee, he says you do need to run that powershell command docs.microsoft.com/en-us/answers/questions/387796/…
    – omega
    Commented May 11, 2021 at 4:44
  • As per the documentation, If administrators have disabled the "SharePoint Online Client Extensibility" service principal previously, The service principal can be re-enabled by using the Enable-SPOTenantServicePrincipal cmdlet. Commented May 12, 2021 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.