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.

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 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.

fig : Fetching site collections

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.

fig : Fetching Admins

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.

fig : Site collection and Admins Name rendering

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 and admins output

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/

https://knowledge-junction.in/2023/01/24/microsoft-365-sharepoint-online-frequently-used-rest-apis-part-3/

https://knowledge-junction.in/2022/07/11/microsoft-365-sharepoint-online-get-the-group-membership-id-from-ui-fetching-all-the-users-of-sharepoint-group-using-rest-api/

https://knowledge-junction.in/2022/03/23/spfx-component-simplifying-understanding-of-service-locator-pattern-part-1-adding-service-class-to-spfx-project-structure-and-accessing-in-spfx-component/

https://knowledge-junction.in/2023/08/11/microsoft-365-sharepoint-online-spfx-resolving-error-error-making-httpclient-request-in-queryable-400-microsoft-sharepoint-client-invalidclientqueryexception-primitivevalue-or-startobjec/

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