Microsoft Graph C# SDK: How to use AppendSegmentToRequestUrl with microsoft.graph.user

Ahsan Habib 126 Reputation points
2021-10-28T16:13:06.207+00:00

I am trying to get all users and their group memberships, using the GraphServiceClient in C#
I have tried this endpoint: https://graph.microsoft.com/v1.0/groups/{GroupId}/members/microsoft.graph.user?$select=displayName,id,mail&$expand=memberOf($select=id,displayName)
The request works fine with Postman. However not able to make it work with C#. This is the Code I tried

var membersRequestUrl = _graphClient.Groups["GroupId"]
                .Members
                .AppendSegmentToRequestUrl("microsoft.graph.user?$count=true");
var requestBuilder = new GroupMembersCollectionWithReferencesRequestBuilder(membersRequestUrl, _graphClient);
var memberRequest = requestBuilder
                .Request()
                .Header("ConsistencyLevel", "eventual")
                .Select("displayName,id,mail")
                .Expand("memberOf($select=id,displayName)");
Console.WriteLine(memberRequest.GetHttpRequestMessage().RequestUri);
var members = await memberRequest.GetAsync();
 Console.WriteLine(JsonConvert.SerializeObject(members));

In line #10, I see the correct URL has been generated. But I do not get the proper response at line #12.
The response I got is:

{
  "DeletedDateTime": null,
  "Id": "{User-Id}",
  "ODataType": null,
  "AdditionalData": {
    "displayName": {
      "ValueKind": 3
    },
    "mail": {
      "ValueKind": 3
    },
    "memberOf": {
      "ValueKind": 2
    }
  }
}

As I can see, the properties are correct, but the values are missing. Event the User-Id is correct.

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

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 37,216 Reputation points
    2021-10-29T09:13:27.62+00:00

    Hi dear @Ahsan Habib I tested your code and it seems to be no problem, I can return all the results. Below is my complete code, you can have a try.

    using System;  
    using Microsoft.Identity.Client;  
    using Microsoft.Graph.Auth;  
    using Microsoft.Graph;  
    using System.Collections.Generic;  
    using Newtonsoft.Json;  
      
    namespace test  
      
    {  
        class Program  
        {  
      
            static async System.Threading.Tasks.Task Main(string[] args)  
      
             
            {  
                IConfidentialClientApplication app;  
                app = ConfidentialClientApplicationBuilder.Create("{client id}")  
                        .WithClientSecret("{Client Secret}")  
                        .WithAuthority(new Uri("https://login.microsoftonline.com/{tenant id}"))  
                        .Build();  
      
                AuthenticationResult result = null;  
                string[] scopes = new string[] { "https://graph.microsoft.com/.default" };  
                result = await app.AcquireTokenForClient(scopes).ExecuteAsync();  
                string accesstoken = result.AccessToken;  
      
                //Console.WriteLine(accesstoken);  
      
                ClientCredentialProvider authProvider = new ClientCredentialProvider(app);    
      
                GraphServiceClient graphClient = new GraphServiceClient(authProvider);  
      
                var membersRequestUrl = graphClient.Groups["{group id}"]  
                       .Members  
                       .AppendSegmentToRequestUrl("microsoft.graph.user?$count=true");  
                var requestBuilder = new GroupMembersCollectionWithReferencesRequestBuilder(membersRequestUrl, graphClient);  
                var memberRequest = requestBuilder  
                                .Request()  
                                .Header("ConsistencyLevel", "eventual")  
                                .Select("displayName,id,mail")  
                                .Expand("memberOf($select=id,displayName)");  
                Console.WriteLine(memberRequest.GetHttpRequestMessage().RequestUri);  
                var members = await memberRequest.GetAsync();  
                Console.WriteLine(JsonConvert.SerializeObject(members));  
      
            }  
        }  
    }  
         
    

    144929-image.png