Microsoft 365: How to retrieve administrators of all site collections in SharePoint Framework (SPFx) component using sharepoint REST API

Hi All,
Greetings for the day!!!
I am compiling frequently used SharePoint REST APIs. In this series of articles today we are listing all site collections and respective Admins.
REST API :
What is Rest API?
A Representational State Transfer (REST) API is a type of web service that allows different software applications to communicate with each other over the internet. It follows a set of architectural principles and constraints, using standard HTTP methods like GET, POST, and PUT to perform various operations on data. REST APIs are widely used for retrieving, creating, updating, and deleting data in a simple and consistent manner, making them a fundamental technology for web-based interactions between applications.
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, Executes the SharePoint Search query using the SPHttpClient class, SPHttpClientResponse represents the response from a SharePoint HTTP request. WebPartContext is for accessing the web part’s context, including SharePoint-specific information.

Step 2 :
We have defined variables in typescript.

Step 3 :
Initializing the state

Step 4 :
We are using the SPHttpClient to make a GET request to the SharePoint REST API to get a list of all site collections in the tenant. The provided API is a SharePoint REST API query used to search for site collections. It searches for site collections based on the condition “contentclass:STS_Site,” and it specifies the properties “Title” and “Path” to be retrieved for each site collection in the search results.

Step 5 :
We are using the SPHttpClient to make a GET request to the SharePoint REST API to fetch data for each site collection in the tenant. It filters the results to only include users with the property “IsSiteAdmin” set to true, which represents users with administrative privileges for the site.

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

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.



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

I hope this article will help you to know about SharePoint REST API , SharePoint Framework (SPFx) component and
how to get all site collections and admins in tenant using SharePoint Rest API.
References :
https://knowledge-junction.in/2022/09/01/frequently-used-rest-apis-part-1/
https://knowledge-junction.in/2022/12/13/frequently-used-rest-apis-part-2/
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 must be logged in to post a comment.