question

GuyMalul-4149 avatar image
1 Vote"
GuyMalul-4149 asked saldana-msft edited

Add OData cast via c# graph sdk

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-sdkmicrosoft-graph-usersmicrosoft-graph-groups
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Adding right tags/teams to assist

0 Votes 0 ·
NandeeshSwami-8857 avatar image
0 Votes"
NandeeshSwami-8857 answered NandeeshSwami-8857 edited

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PaulSchaeflein avatar image
2 Votes"
PaulSchaeflein answered MattAmphlett-0858 edited

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/

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This works. Thanks!

Only slight issue is that the response is still mapped to a list of DirectoryObject objects with the other user fields within the AddtionalData dictionary.

Is there any way to have the SDK return a list of User objects instead?

0 Votes 0 ·