最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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 (). I then found the following API that returns all sites available to user:

.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?

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/en-us/answers/questions/1103068/how-to-get-all-sharepoint-sites-of-anization-us). I then found the following API that returns all sites available to user:

https://graph.microsoft/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?

Share Improve this question asked Mar 19 at 15:57 vulejvulej 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The JAVA SDK generates the URL like https://graph.microsoft/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/v1.0/sites?search=*").get();
发布评论

评论列表(0)

  1. 暂无评论