Question

Can we use REST API to get all site collections in sharepoint tenant.? I don't want powershell script basically i want to bind all asite collections to a dropdown.

my code looks like bleow. RESTAPI Serach

Was it helpful?

Solution

You can use SharePoint search API for that, with a query like this:

https://yourtenantname.sharepoint.com/_api/search/query?querytext='contentClass:STS_Site'&trimduplicates=false&selectproperties='SiteLogo%2cTitle'

This query gets all sites and selects the logo and title. You can modified to get more properties.

If you want to go the graph route, you can do it like this:

https://graph.microsoft.com/v1.0/sites?search=*

That'd also get you back all sites, but for simplicity (and if you're using on prem) you can just use search.

Updated to answer the question in the comment:

If you want to filter based on the url, you can do something like this:

https://yourtenantname.sharepoint.com/_api/search/query?querytext='contentClass:STS_Site'&trimduplicates=false&selectproperties='SiteLogo,Title,SPSiteUrl'&refinementfilters='SPSiteUrl:("https://yourtenantname.sharepoint.com/sites/*")'

You would want to escape the double quotes when used in code, like this:

 const url =  "https://yourtenantname.sharepoint.com/_api/search/query?querytext='contentClass:STS_Site'&trimduplicates=false&selectproperties='SiteLogo,Title,SPSiteUrl'&refinementfilters='SPSiteUrl:(\"https://yourtenantname.sharepoint.com/sites/*\")'"

or with escape it with backticks like this:

`https://yourtenantname.sharepoint.com/_api/search/query?querytext='contentClass:STS_Site'&trimduplicates=false&selectproperties='SiteLogo,Title,SPSiteUrl'&refinementfilters='SPSiteUrl:("https://yourtenantname.sharepoint.com/sites/*")'`

This way you're getting only sites under /sites. or you can have something like /sites/p* to have all sites starting with the letter p.

OTHER TIPS

You can have a try following endpoint:

https://xxx-admin.sharepoint.com/_api/web/lists/getbytitle('DO_NOT_DELETE_SPLIST_TENANTADMIN_AGGREGATED_SITECOLLECTIONS')/items?$select=Title,SiteUrl 

I have tested it on my SP online and it works well.

For more details, please refer to :

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top