1

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?

1 Answer 1

0

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();
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the answer. I also get an empty list with ` "\"*\"" `. Unfortunately specifying the tenant name doesn't work for me as I want my application to be multitenant. Also using application permissions means my application would act on its own with no user signed in, which is not what I need as I need the user to log in and select a site (so I can upload a file). I guess I might write a client as I got what I needed in the graph explorer with the REST APIs
@vulej. Thanks for clarification. Workaround can be using withUrl() method. Check updated answer
Thanks, this works! I'm still kinda confused why the SDK doesn't provide an easier approach (so that e.g. just calling graphClient.sites().get() works in this case) as the withUrl seems something inbetween the SDK and the REST API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.