Forum Discussion

boris1255's avatar
boris1255
Copper Contributor
Nov 02, 2022

Using Microsoft Graph to filter on siteCollection property

I want to get a list of all SharePoint sites on the tenant using Graph. But when I make a request, it brings everything including user OneDrives. From the data it seems that you can use siteCollection property to filter the data.

siteCollection : @{hostname=xxxxxxx-my.sharepoint.com} - OneDrive

siteCollection : @{hostname=xxxxxxx.sharepoint.com} - SharePoint Site

 

I am making requests via PowerShell. So far tried variations of these:

$v1/sites?`$select=webUrl,siteCollection&`$search=`"siteCollection:hostname=xxxxxxx.sharepoint.com`"
$v1/sites?`$select=webUrl,siteCollection&`$count=true&`$filter=startsWith(siteCollection,'hostname=xxxxxxx.sharepoint.com')
$v1/sites?`$select=webUrl,siteCollection&`$search=%22siteCollection:hostname=xxxxxx.sharepoint.com%22

But no luck. Just brings back empty value

@odata.context                                                                                                  value
--------------                                                                                                       -----
https://graph.microsoft.com/v1.0/$metadata#sites(webUrl,siteCollection)       {}

 

  • Perneel's avatar
    Perneel
    Copper Contributor
    Exact same problem here, did you ever find a solution to this?
  • BarryGoblon's avatar
    BarryGoblon
    Iron Contributor

    boris1255 To filter SharePoint sites using Microsoft Graph, you should craft the correct OData query. To achieve this, you can employ a query that uses the $filter parameter to specifically target the siteCollection property's hostname attribute. Your query should resemble the following:

     

    $filter=siteCollection/hostname eq 'xxxxxxx.sharepoint.com'

     

    In practical terms, this query will retrieve a list of SharePoint sites on your tenant associated with the site collection using the specified hostname, 'xxxxxxx.sharepoint.com'. By implementing this query through PowerShell, you can access the Microsoft Graph API effectively. You can execute the query with the following PowerShell command:

     

    Invoke-WebRequest -Uri "https://graph.microsoft.com/v1.0/sites?$filter=siteCollection/hostname eq 'xxxxxxx.sharepoint.com'" -Method GET -Headers @{Authorization="Bearer $accessToken"} | ConvertFrom-Json

     

    Please replace $accessToken with the access token specific to your Microsoft Graph application.

    However, it's important to meticulously validate that you're using the correct Microsoft Graph API version, adhering to the precise OData query syntax, and possessing the necessary permissions to access the data. If issues persist, reaching out to Microsoft support for further assistance is a prudent step to ensure the efficacy of your filtering efforts. This approach ensures that you can tailor your Microsoft Graph queries to effectively retrieve SharePoint sites while excluding OneDrives, streamlining your data retrieval processes.

Resources