How to call graph API to read SP list items from SPFx in SP Online?

%3CLINGO-SUB%20id%3D%22lingo-sub-2338030%22%20slang%3D%22en-US%22%3EHow%20to%20call%20graph%20API%20to%20read%20SP%20list%20items%20from%20SPFx%20in%20SP%20Online%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2338030%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20new%20to%20graph%20api%20and%20trying%20to%20use%20graph%20api%20to%20read%20sp%20list%20items%20from%20a%20custom%20list%20using%20the%20current%20user's%20rights%20(not%20service%20account).%20But%20i'm%20getting%20an%20error.%20I%20have%20this%20so%20far%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ein%20the%20package-solution.json%20file%2C%20I%20have%20this%20in%20the%20%60solution%60%20object%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%22webApiPermissionRequests%22%3A%20%5B%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%22resource%22%3A%20%22Microsoft%20Graph%22%2C%3CBR%20%2F%3E%22scope%22%3A%20%22Sites.Read.All%22%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%5D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20the%20helloworld.ts%20file%20I%20have%20this%20to%20init%20the%20graph%20api%20connection%3C%2FP%3E%3CP%3E%3CBR%20%2F%3Epublic%20async%20render()%3A%20Promise%3CVOID%3E%20%7B%3CBR%20%2F%3Econst%20element%3A%20React.ReactElement%3CIHELLOWORLDPROPS%3E%20%3D%20React.createElement(%3CBR%20%2F%3EHelloWorld%2C%3CBR%20%2F%3E%7B%3CBR%20%2F%3Edescription%3A%20this.properties.description%2C%3CBR%20%2F%3Eclient%3A%20await%20this.context.msGraphClientFactory.getClient()%3CBR%20%2F%3E%7D%3CBR%20%2F%3E)%3B%3C%2FIHELLOWORLDPROPS%3E%3C%2FVOID%3E%3C%2FP%3E%3CP%3EReactDom.render(element%2C%20this.domElement)%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3Epublic%20onInit()%3A%20Promise%3CVOID%3E%20%7B%3CBR%20%2F%3Egraph.setup(%7B%3CBR%20%2F%3EspfxContext%3A%20this.context%3CBR%20%2F%3E%7D)%3B%3CBR%20%2F%3E%3CBR%20%2F%3Ereturn%20super.onInit()%3B%3CBR%20%2F%3E%7D%3C%2FVOID%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eand%20in%20the%20helloworld.tsx%20file%20I%20have%20this%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Elet%20items%20%3D%20await%20props.client.api('%2Fsites%2FTestSite%2Flists(%5C'MyFeatures%5C')%2Fitems').get()%3B%3CBR%20%2F%3Econsole.log(items)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20I%20see%20this%20error%20in%20the%20dev%20tools%20console%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUncaught%20(in%20promise)%20Error%3A%20To%20view%20the%20information%20on%20this%20page%2C%20ask%20a%20global%20or%20SharePoint%20admin%20in%20your%20organization%20to%20go%20to%20the%20API%20management%20page%20in%20the%20new%20SharePoint%20admin%20center.%3CBR%20%2F%3Eat%20e.getToken%20(chunk.aadTokenProvider_none_3888c2e89a4ae65276c0.js%3A1)%3CBR%20%2F%3Eat%20sp-webpart-workbench-assembly_en-us_6db5d03d6aef1b7d63505cff87b165b2.js%3A65%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20haven't%20done%20anything%20on%20the%20administrative%20side%20to%20create%20any%20api%20registration%20or%20approve%20anything.%20I%20was%20wondering%20do%20I%20need%20to%20do%20that%2C%20and%20if%20so%2C%20is%20there%20a%20tutorial%20for%20how%20to%20set%20it%20up%3F%20And%20is%20there%20anything%20more%20in%20the%20code%20I%20need%20to%20adjust%20like%20add%20a%20client%20id%2Fsecret%20or%20something%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EEDIT%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20added%20the%20web%20part%20to%20the%20app%20catalogue%20and%20it%20told%20me%20to%20approve%20it%20from%20the%20API%20management%20centre.%20However%20when%20I%20went%20there%2C%20there%20was%20nothing%20listed%20in%20pending%20or%20approved.%20And%20it%20says%20there%20is%20an%20error%20Could%20not%20retrieve%20Global%20Service%20Principal%20ID%20Error%3A%20Error%3A%20%7B%7D.%20Researching%20that%20I%20found%20this%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Feschrader.com%2F2020%2F06%2F23%2Fsharepoint-online-bad-service-principal-breaks-sharepoint-admin-api-access-global-service-principal-id-error-resolved%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Feschrader.com%2F2020%2F06%2F23%2Fsharepoint-online-bad-service-principal-breaks-sharepoint-admin-api-access-global-service-principal-id-error-resolved%3C%2FA%3E%2F%20Do%20I%20need%20to%20do%20this%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlso%20I%20found%20the%20command%20Enable-SPOTenantServicePrincipal.%20Do%20I%20need%20to%20enable%20this%20for%20the%20SharePoint%20Online%20Client%20Extensibility%20service%20principal%3F%20Is%20this%20needed%20to%20be%20enabled%20for%20the%20SPFx%20webparts%20to%20use%20graph%20api%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2338030%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAPIs%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExtensibility%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2347018%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20call%20graph%20API%20to%20read%20SP%20list%20items%20from%20SPFx%20in%20SP%20Online%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2347018%22%20slang%3D%22en-US%22%3EHi%2C%20I%20was%20also%20having%20the%20%22Could%20not%20retrieve%20Global%20Service%20Principal%20ID%20Error%3A%20Error%3A%20%7B%7D%22%20on%20my%20tenant%20and%20the%20process%20described%20on%20the%20link%20that%20you%20shared%20solved%20the%20problem.%20This%20included%20deleting%20the%20problematic%20app%20registration%20and%20getting%20a%20new%20one%20created.%3CBR%20%2F%3E%3CBR%20%2F%3ERegarding%20consuming%20the%20Graph%20API%2C%20there%20are%20many%20SPFx%20PnP%20web%20part%20samples%20that%20demonstrate%20this.%20Just%20search%20for%20%22Graph%22%20on%20the%20samples%20website%3A%20%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fsp-dev-fx-webparts%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fpnp.github.io%2Fsp-dev-fx-webparts%2F%3C%2FA%3E%3C%2FLINGO-BODY%3E
New Contributor

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?

 

 

EDIT

 

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 https://eschrader.com/2020/06/23/sharepoint-online-bad-service-principal-breaks-sharepoint-admin-api.../ Do I need to do this?

 

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?

2 Replies
Hi, I was also having the "Could not retrieve Global Service Principal ID Error: Error: {}" on my tenant and the process described on the link that you shared solved the problem. This included deleting the problematic app registration and getting a new one created.

Regarding consuming the Graph API, there are many SPFx PnP web part samples that demonstrate this. Just search for "Graph" on the samples website: https://pnp.github.io/sp-dev-fx-webparts/

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

 

Anyone else facing this issue can find more details about this in below thread:

How to call graph API to read SP list items from SPFx in SP Online? 


Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.