Add OData cast via c# graph sdk

Guy Malul 6 Reputation points

I want to execute the following query:

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:

Select(member => new { member.Id }).


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

    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:

    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");

    The library is on Nuget:

    2 people found this answer helpful.

  2. Nandeesh Swami-MSFT 1 Reputation point Microsoft Employee

    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

    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  

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