microsoftgraph / msgraph-sdk-dotnet

Microsoft Graph Client Library for .NET!
https://graph.microsoft.com
Other
674 stars 243 forks source link

[v5.0] CountRequestBuilder doesn't have QueryParameters Filter and Search #1587

Open Licantrop0 opened 1 year ago

Licantrop0 commented 1 year ago

Describe the bug $count requests depends on $filter and $search parameters. CountRequestBuilder should include the related QueryParameters in the CountRequestBuilderGetRequestConfiguration

To Reproduce

var count = _graphClient.Users.Count.GetAsync(rc =>
   {
       rc.Headers.Add("ConsistencyLevel","eventual");
       rc.QueryParameters.Search = "displayName:Luca";
       rc.QueryParameters.Filter = "startsWith(mail, luca)";
   });

Expected behavior the above code should work.

stevedcc commented 1 year ago

I have the same problem - but also notice that the Select property is missing. In addition, since an integer is returned, it is not clear how to get at the response contents. In this case, it is easier to use HTTP Client directly than the SDK. Either your documentation or your SDK is broken.

The documentation at https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/feature/5.0/docs/upgrade-to-v5.md even directly compares the new "count" returning an int with the old "count" which may have been more complicated to build, but gave easy access to the response. The documented code in the two versions is a very long way from offering equivalent functionality.

I have queries working in Postman that I am unable to get working in the SDK (example below). I can't even figure out how to see which URLs the SDK is producing without resorting to Fiddler.

 https://graph.microsoft.com/v1.0/users?
              $count=true
              &$filter=(businessPhones/any(p:p ge ' ') or mobilePhone ge ' ')
                  and accountEnabled ne false
                  and showInAddressList ne false
                  and givenName ge ' '
                  and surname ge ' '
              &$select=AccountEnabled,BusinessPhones,CompanyName,City,Country,Department,EmployeeId,GivenName,Id,JobTitle,Mail,OtherMails,MobilePhone,PostalCode,Photo,OfficeLocation,ShowInAddressList,State,StreetAddress,Surname
Licantrop0 commented 1 year ago

@stevedcc in the new SDK, the above http request can be translated to:

var users = await  graphClient.Users
    .GetAsync(rc =>
    {
        rc.Headers = new() { { "ConsistencyLevel", "eventual" } };
        rc.QueryParameters.Count = true;
        rc.QueryParameters.Select = new[] { "AccountEnabled", "BusinessPhones", "CompanyName" };
        rc.QueryParameters.Filter = "(businessPhones/any(p:p ge ' ') or mobilePhone ge ' ') and accountEnabled ne false and showInAddressList ne false and givenName ge ' ' and surname ge ' '";
    });

(list of selected properties shortened).
You can access the count property with users.OdataCount.

stevedcc commented 1 year ago

Thank you for the quick response. This is nothing like the description in the upgrade link. Is there better documentation available anywhere?

Licantrop0 commented 1 year ago

@maisarissi recently published this blog post, I assume the documentation will be updated for the final release.

stevedcc commented 1 year ago

Thank you. Seeing as the v4 API does not work in MAUI, it is more than a little frustrating having to work with the beta api with the current leveling documentation - from a quick skim the blog post seems to encourage the use of count the same way as the upgrade guide, which is what lead me astray .

Your support is much appreciated - everything is working now.

Licantrop0 commented 1 year ago

@stevedcc all the samples in the Graph Docs will be upgraded to the latest version it would be easier to understand how to map between REST and C#.

$count can be used in two ways:

  1. As a path segment: ~/users/$count: this will return the raw count (just a number in the response)
  2. As a query parameter: ~/users?$count=true: this will return the count in @odata.count property of the json response, alongside the full list of users.

These two methods are modeled in the new SDK (examples in my previous posts).

maisarissi commented 1 year ago

Hello. Yes, the documentation is going to be updated once we reach GA.

AFAIK (and I could be wrong here hahaha) the .Count() as part of the request builder is to simplify extending the use of $count only as a URL segment, e.g. GET /groups/$count, that's why an integer as response. The use of $count as a query string parameter, like /servicePrincipals?$count=true same as your scenario @stevedcc, one needs to use the requestConfiguration, hence why adding rc.QueryParameters.Count = true; in the request works as expected. BTW, thanks for your reply @Licantrop0.

@andrueastman can you please correct me if I misunderstood something?

Also, let's update the upgrade guide to make the information clearer.