javasharepointmicrosoft-graph-apimicrosoft-graph-sdks

How to fetch all SharePoint sites with Microsoft Graph SDK for Java


I want to fetch all SharePoint Sites a user has access to using graph SDK for Java (version 6.32.0). I've obtained the access token and created a graph client (I'd like to use delegated permissions to get information user has access to).

The suggestions in Microsoft Graph docs suggest using graphClient.sites().get() however this return an empty list. Even if I try the same query in Microsoft Graph Explorer the list is empty as some comment on the Microsoft learn platform suggested this only works with Application permission type (https://learn.microsoft.com/en-us/answers/questions/1103068/how-to-get-all-sharepoint-sites-of-organization-us). I then found the following API that returns all sites available to user:

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

and this indeed returns all the sites. Inside the graph explorer Code snippets tab there is an SDK snippet that corresponds to the above API:

SiteCollectionResponse result = graphClient.sites().get(requestConfiguration -> {
    requestConfiguration.queryParameters.search = "*";
});

which I've tried to use but I get the following ODataError:

com.microsoft.graph.models.odataerrors.ODataError: Syntax error: character '*' is not valid at position 0 in '*'.
    at com.microsoft.graph.models.odataerrors.ODataError.createFromDiscriminatorValue(ODataError.java:36) ~[microsoft-graph-6.32.0.jar:na]
    at com.microsoft.kiota.serialization.JsonParseNode.getObjectValue(JsonParseNode.java:212) ~[microsoft-kiota-serialization-json-1.8.4.jar:na]
    at com.microsoft.kiota.http.OkHttpRequestAdapter.lambda$throwIfFailedResponse$0(OkHttpRequestAdapter.java:704) ~[microsoft-kiota-http-okHttp-1.8.4.jar:na]
    at com.microsoft.kiota.ApiExceptionBuilder.<init>(ApiExceptionBuilder.java:26) ~[microsoft-kiota-abstractions-1.8.4.jar:na]
    at com.microsoft.kiota.http.OkHttpRequestAdapter.throwIfFailedResponse(OkHttpRequestAdapter.java:703) ~[microsoft-kiota-http-okHttp-1.8.4.jar:na]
    at com.microsoft.kiota.http.OkHttpRequestAdapter.send(OkHttpRequestAdapter.java:307) ~[microsoft-kiota-http-okHttp-1.8.4.jar:na]
    at com.microsoft.graph.sites.SitesRequestBuilder.get(SitesRequestBuilder.java:119) ~[microsoft-graph-6.32.0.jar:na]

I can't figure out what I'm missing and what would be the way to do this?


Solution

  • The JAVA SDK generates the URL like https://graph.microsoft.com/v1.0/sites?$search=*. It adds $ to the search query parameter and it leads to the error you have mentioned.

    If you use requestConfiguration.queryParameters.search = "\"*\""; it will return an empty collection (at least for me).

    You can specify your tenant name requestConfiguration.queryParameters.search = "\"contoso\"";, but it will not return all sites.

    From my experience, the best way is to use application permissions and call getAllSites

    GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
    
    var result = graphClient.sites().getAllSites().get();
    

    As a workaround, you can use withUrl method

    GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
    
    var result = graphClient.sites().withUrl("https://graph.microsoft.com/v1.0/sites?search=*").get();