Graph API - Get list of SharePoint sites with Sites.Selected

Billy 10 Reputation points
2023-07-17T06:55:31.05+00:00

Hi there,

I'm building an integration with SharePoint using the Graph API. I have an Azure AD application with the Site.Selected permission and consent is granted. I have also granted read/write permissions to this application for a number of sites in SharePoint. When using the Microsoft Graph SDK in C#, I'm unable to get a list of sites that the application has read access to, but I am able to get details for individual sites. For reference:

GraphServiceClient graphClient = GetGraphClient(); // Setup with client credentials (app-based authentication)
var sites = await graphClient.Sites.GetAsync(); // List of sites is empty, even though the application has read access to some sites
var site = await graphClient.Sites["Id from one of the sites the app has read permissions"].GetAsync(); // Site details are returned

The fact that I can get individual site details tells me that everything is setup correctly. Is there a way to query all sites that my application has read permissions without requesting full read/read permission to all sites in SharePoint?

Thanks

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,007 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,202 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. ChengFeng - MSFT 4,995 Reputation points Microsoft Vendor
    2023-07-17T09:45:47.6966667+00:00

    Hi @Billy

    Please try the code below. Judging by obtaining the information of the site

    GraphServiceClient graphClient = GetGraphClient(); 
    var sites = await graphClient.Sites.Request().Expand("permissions").GetAsync();
    
    var filteredSites = sites.Where(site => site.Permissions.Any(permission => permission.Roles.Any(role => role.Id == "Sites.Read.All")));
    
    foreach (var site in filteredSites)
    {
        Console.WriteLine($"Site: {site.Id} - {site.DisplayName}");
    }
    
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards

    Cheng Feng