Unable to fetch some group properties via C# SDK

Yasitha Pandithawatta 141 Reputation points
2024-09-13T04:16:44.4666667+00:00

I am trying to fetch groups via graph api and get resourceBehaviorOptions and resourceProvisioningOptions properties. Eventhough these properties exist in the direct graph api response, I am unable to find them when I use c# graph SDK. I can see these properties existing under AdditionalData but value is empty even though graph response had values.

var graphClient = new GraphServiceClient(accessTokenCredential);
GroupCollectionResponse
{
  requestConfiguration.QueryParameters.Count = true;
  requestConfiguration.Headers.Add("ConsistencyLevel", "eventual");
});

foreach (var group in groups.Value)
{
  Console.WriteLine(JsonSerializer.Serialize(group.AdditionalData["resourceProvisioningOptions"]));
  #always gives {}
}

I also found separate api request to fetch resourceBehaviorOptions via https://graph.microsoft.com/v1.0/groups/{group_id}/resourceProvisioningOptions but the value is still empty.

How can I fetch these property values using graph SDK?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,273 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,012 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Yakun Huang-MSFT 6,570 Reputation points Microsoft Vendor
    2024-09-13T08:24:37.5266667+00:00

    Hi @Yasitha Pandithawatta

    Try this code:

    // Code snippets are only available for the latest version. Current version is 5.x
    // To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
    var result = await graphClient.Groups["{group-id}"].GetAsync((requestConfiguration) =>
    {
    	requestConfiguration.QueryParameters.Select = new string []{"resourceBehaviorOptions","resourceProvisioningOptions" };
    });
    

    Note that make sure the property has a value.

    User's image

    Reference document:

    https://learn.microsoft.com/en-us/graph/teams-list-all-teams#request-1

    https://learn.microsoft.com/en-us/graph/group-set-options

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


  2. Yasitha Pandithawatta 141 Reputation points
    2024-09-19T23:35:17.5066667+00:00

    When I debug the response object, I can see these properties exist under additionalData as non-public properties, even in the initial /groups API request. I wrote the code snippet below and was able to retrieve them.

    if (group.AdditionalData != null && group.AdditionalData.TryGetValue("resourceProvisioningOptions", out object provisioningOptionsObj))
            {
                if (provisioningOptionsObj is UntypedArray untypedArray)
                {
                    var valueField = untypedArray.GetType().GetField("_value", BindingFlags.NonPublic | BindingFlags.Instance);
                    if (valueField != null)
                    {
                        var enumerable = valueField.GetValue(untypedArray) as IEnumerable;
    
                        if (enumerable != null)
                        {
                            foreach (var item in enumerable)
                            {
                                if (item is UntypedString untypedString)
                                {
                                    var stringValueField = untypedString.GetType().GetField("_value", BindingFlags.NonPublic | BindingFlags.Instance);
    
                                    if (stringValueField != null)
                                    {
                                        var actualValue = stringValueField.GetValue(untypedString) as string;
                                        Console.WriteLine(actualValue);
                                    }
                                }
                            }
                        }
                    }
                }
            }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.