Add OData cast via c# graph sdk

Guy Malul 6 Reputation points
2021-03-10T08:21:24.867+00:00

I want to execute the following query:
GET https://graph.microsoft.com/beta/groups/{id}/transitiveMembers/microsoft.graph.user?$count=true&$select=id

Is there a way to add this OData cast (microsoft.graph.user) to the query via c# sdk?

What I was able to do is:

GraphServiceClient.
Groups[{id}].
TransitiveMembers.
Request().
Select(member => new { member.Id }).
GetAsync();

Thanks,
Guy

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,317 questions
{count} vote

3 answers

Sort by: Most helpful
  1. Paul Schaeflein 11 Reputation points MVP
    2021-05-20T05:52:56.38+00:00

    The Graph.Community library now (a/o v3.32) contains an extension method to add the OData cast to the url.

    The test method mocks the request for a members groups, filtered to only include directory roles:

    [Fact]
    public void WithODataCastUpdatesUrl()
    {
      // ARRANGE
      var oDataCast = "microsoft.graph.directoryRole";
      var expectedUrl = "/me/memberOf/microsoft.graph.directoryRole";
    
      // ACT
      using (var gsc = GraphServiceTestClient.Create())
      {
        var request = gsc.GraphServiceClient.Me.MemberOf.WithODataCast(oDataCast).Request();
        var actualUrl = request.RequestUrl;
    
        // ASSERT
        Assert.True(actualUrl.IndexOf(expectedUrl) > -1, "RequestUrl does not include odata cast  value");
      }
    }
    

    https://github.com/microsoftgraph/msgraph-sdk-dotnet-contrib/blob/b7ff25ca9a1aa0e05ff399916e3265c3600b398d/test/BaseRequestBuilderExtensionTests.cs#L37

    The library is on Nuget: https://www.nuget.org/packages/Graph.Community/

    2 people found this answer helpful.

  2. Nandeesh Swami-MSFT 1 Reputation point Microsoft Employee
    2021-04-06T11:47:02.343+00:00

    Hey, I assume you want to get a user. There are multiple ways, 1 of which is to filter. Below I have demonstrated it by filtering per name. You can do so on other properties as well.
    graphClient.Users.Request().Filter("startsWith(displayName, 'N')").GetAsync()

    Let me know if for any queries.

    0 comments No comments

  3. Maisa Rissi 76 Reputation points Microsoft Employee
    2022-12-16T02:47:26.947+00:00

    The new version of the Microsoft Graph .NET SDK v5 enables OData casting.
    You can easily get User object by the following:

    var usersInGroup = await graphServiceClient  
        .Groups["group-id"]  
        .Members  
        .User  
        .GetAsync();  
    

    Also, you can use the upgrade-guide to help you with any migration info you might need.