Microsoft 365: Microsoft Graph API – Get All Site Collections in Tenant using Microsoft Graph API in SharePoint Framework (SPFx) component – Part 1

Hi All,

Greetings for the day!!!

I am compiling frequently used Microsoft Graph APIs. In this series of articles today we are listing get all site collections Tenant.

The Microsoft Graph API is a powerful tool that can be used to access and manage Microsoft 365 data from SPFx web parts. One of the use case is we can enlist all site collection in our tenant with in SPFx webpart using Microsoft Graph API.

Graph Explorer :

What is Microsoft Graph Explorer?

It is tool provided by Microsoft to queries or test the Microsoft Graph API. We can access Microsoft Graph Explorer with URL –

https://developer.microsoft.com/en-us/graph/graph-explorer

Then very good series of the Graph Explorer kindly please have a look.

https://knowledge-junction.in/2018/03/31/office-365-microsoft-graph-and-graph-explorer/

fig : Microsoft 365 – Graph Explorer sites collection data API response

Let’s look into SPFx Component :

Step 1 : This code imports the necessary modules for creating a SharePoint Framework web part.

The ‘react‘ import is for using React for building the user interface, MSGraphClientV3 is for making requests to the Microsoft Graph API, WebPartContext is for accessing the web part’s context, including SharePoint-specific information.

fig : Imports the necessary modules

Step 2 : We have defined variables in typescript.

fig : Variable declaration in typescript

Step 3 : Initializing the state

fig : State initialization

Step 4 : We are using the MSGraphClientV3 to make a GET request to the Microsoft Graph API to get a list of all site collections in the tenant. The search=* query parameter is used to filter the results to include all site collections.

fig : Fetching site collections

Step 5 : Let’s iterate through the list of site collections in the state and renders a list item for each site collection.

fig : Site collection rendering

Step 6 : Configure the API Permissions Requests :

We are specifying this permission from package-solution.json file under the config folder of the solution.

fig : Permission request

Build a component using gulp build and gulp bundle for Bundling files and for testing purpose I am using gulp serve so it opens the workbench.aspx.

fig : Gulp build for project compilation
fig : Bundling files using gulp bundle
fig : Running gulp serve for local development.

and after successfully execution of above command, component will show the following result.

fig : Site collection output

I hope this article will help you to know about Microsoft Graph API , SharePoint Framework (SPFx) component and

how to Get All Site Collections in Tenant using Microsoft Graph API.

Also get my article updates on my social media handles. 

LinkedIn – https://www.linkedin.com/in/ganesh-barde-860302218/

Twitter – https://twitter.com/GaneshB64176973?t=6N6FyRy-YOKAF5ujrI5QHQ&s=09

Have a wonderful day.  

Thanks for reading. 

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading