msgraph-sdk-java icon indicating copy to clipboard operation
msgraph-sdk-java copied to clipboard

No way to list `Sites.Selected` collection available for application

Open denysobukh opened this issue 1 year ago • 8 comments

Describe the bug

When an app has Sites.Read.All api permission, it can list all available sites. But when it has Sites.Selected only and was granted permissions to selected sites via POST /v1.0/sites/{siteId}/permissions

{
    "roles": [
        "read"
    ],
    "grantedToIdentities": [
        {
            "application": {
                "id": "{clientId}",
                "displayName": "Name"
            }
        }
    ]
} 

There is no way to list these site available to the application:

client.sites().get().getValue(); results 0

client.sites().get(requestConfiguration -> {
    requestConfiguration.queryParameters.select = new String[]{"siteCollection", "webUrl"};
    requestConfiguration.queryParameters.filter = "siteCollection/root ne null";
}).getValue()

results: Access denied

client.sites().getAllSites().get().getValue();

results: Access denied

Expected behavior

Some api call which returns available via Sites.Selected

How to reproduce

  1. Set Sites.Selected permissions to application
  2. Add site permission to the selected collection POST /v1.0/sites/{siteId}/permissions
{
    "roles": [
        "read"
    ],
    "grantedToIdentities": [
        {
            "application": {
                "id": "{clientId}",
                "displayName": "Name"
            }
        }
    ]
}
  1. Call
client.sites().getAllSites().get().getValue();

observe empty result collection

SDK Version

6.10.0

Latest version known to work for scenario above?

No response

Known Workarounds

No response

Debug output

Click to expand log ```
</details>


### Configuration

_No response_

### Other information

_No response_

denysobukh avatar Jul 23 '24 09:07 denysobukh

The same empty site result list for SDK v.6.16.0 and UsernamePasswordCredential provider https://github.com/microsoftgraph/msgraph-sdk-java/issues/2184

SashaUsov avatar Oct 02 '24 16:10 SashaUsov

@denysobukh @SashaUsov to double-check, is the Graph client you're instantiating to call client.sites().getAllSites().get().getValue(); using the client_id you've granted Sites.Selected permissions?

I notice that the application that creates the permission needs Sites.FullControl.All

Then you'd need to initialise a new client with the client_id passed in the payload of the request above and potentially client.sites().getAllSites().get().getValue(); should work?

Ndiritu avatar Oct 03 '24 06:10 Ndiritu

@SashaUsov just noticed that getAllSites does not work with signed in users (delegated scenarios), it only supports application permissions.

Ndiritu avatar Oct 03 '24 06:10 Ndiritu

@Ndiritu I have no problem with permissions, it works for SDK version 5.80.0. I trying to use site search now, but it returns error "com.microsoft.graph.models.odataerrors.ODataError: Syntax error: character '' is not valid at position 0 in "" ":

graphServiceClient.sites()
                .get(requestConfiguration -> {
                    var queryParameters = requestConfiguration.queryParameters;
                        queryParameters.select = new String[]{SELECT_SITE_NAME};
                        queryParameters.search = "*";
                })
.getValue();

since search = "*" is not valid for the new Graph version I am trying to get sites without a search query, but get empty result. But it works for Graph version 5.80.0, same client_id and user, and call:

graphServiceClient
                .sites()
                .buildRequest(new QueryOption("search", "*"), new QueryOption("select", "id,name"))
                .get()
                .getCurrentPage()

SashaUsov avatar Oct 03 '24 07:10 SashaUsov

Thanks for clarifying.

Using an empty search parameter works for me @SashaUsov

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

Does this help?

Ndiritu avatar Oct 03 '24 07:10 Ndiritu

@Ndiritu empty search for me cause error: com.microsoft.graph.models.odataerrors.ODataError: Expression expected at position 0 in ''.

a search query like "all" returns for me only 3 sites when Java SDK 5.80.0 returns 99 sites to which my user has access.

SashaUsov avatar Oct 03 '24 07:10 SashaUsov

@SashaUsov Please try using withUrl() to override the request URL. Does this unblock you?

graphClient.sites().withUrl("https://graph.microsoft.com/v1.0/sites?select=id,name&search=").get()

Would you also mind upgrading to the latest SDK version & sharing the failing code sample. It's not clear what properties you're selecting from your previous 6.x sample.

Ndiritu avatar Oct 03 '24 09:10 Ndiritu

.withUrl("https://graph.microsoft.com/v1.0/sites?select=id,name&search=").get()

@Ndiritu withUrl() works for me, thank you!

Tell me please, will this bug be fixed in the next SDK releases?

SashaUsov avatar Oct 03 '24 09:10 SashaUsov