microsoft-graph-docs icon indicating copy to clipboard operation
microsoft-graph-docs copied to clipboard

Example 2: Create a new group

Open digiofficerobin opened this issue 2 years ago • 4 comments

We are updating our code from Microsoft.Graph from version 4.48.0 to 5.11.0. While I have managed to solve the breaking changes with the article https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/feature/5.0/docs/upgrade-to-v5.md#query-options I could not find a solution for the following breaking change.

In 4.48.0 we have the following function to create a new group directly in an Administrative Unit:

	public Group CreateNewGroupInAdministrativeUnit(string name, string administrativeUnitID)
	{
		var group = new Group
		{
			DisplayName = name,
			MailEnabled = false,
			SecurityEnabled = true,
			MailNickname = name.MailNickname()
		};

		// There is no PostAsync() available in Graph SDK to create a new group in an AU.
		// The workaround is to post it with the underlying HttpProvider. 
		var requestUrl = $"https://graph.microsoft.com/v1.0/directory/administrativeUnits/{administrativeUnitID}/members";
		var response = new HttpResponseMessage();
		
		using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUrl))
		{
			httpRequestMessage.Content = _graphClient.HttpProvider.Serializer.SerializeAsJsonContent(group);
			var result = _graphClient.HttpProvider.SendAsync(httpRequestMessage).Result.Content.ReadAsStringAsync().Result;
			
			return _graphClient.HttpProvider.Serializer.DeserializeObject<Group>(result);
		}
	}

In "Example 2 Create a new group" of the documentation https://learn.microsoft.com/en-us/graph/api/administrativeunit-post-members?view=graph-rest-1.0&tabs=csharp#example-2-create-a-new-group is shown exactly what we need in the new version of the GraphClient. The new code should look something like this (I guess):

	public async Group CreateNewGroupInAdministrativeUnit(string name, string administrativeUnitID)
	{
		// https://learn.microsoft.com/en-us/graph/api/administrativeunit-post-members?view=graph-rest-1.0&tabs=csharp#example-2-create-a-new-group
		var additionalData = new Dictionary<string, object>()
		{
			{
			"@odata.type" , "#microsoft.graph.group"
			}
		};
		var group = new Group
		{
			DisplayName = name,
			MailEnabled = false,
			SecurityEnabled = true,
			MailNickname = name.MailNickname(),
			AdditionalData = additionalData
		};

		await _graphClient.Directory.AdministrativeUnits[administrativeUnitID].Members.PostAsync(group);

	}

The problem is that the ...Members.PostAsync(group) method is still not available, but the example suggests otherwise. Also the Microsoft.Graph.DirectoryNamespace.AdministrativeUnits.Item.Members.MembersPostRequestBody class is not available anymore in 5.11.0. And also the underlying _graphClient.HttpProvider is not available anymore, so are stuck at this point while updating to the latest package.

Could you update the documentation of example 2 on how to create a new group directly into an administrative unit?

By either:

  • Let ...Members.PostAsync(group) be added by Microsoft.Graph team
  • Show me a way to get the underlying HttpProvider and use the workaround for the PostAsync() method again
  • Some other ideas to solve this issue

Kind regards, Robin

Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

digiofficerobin avatar May 22 '23 13:05 digiofficerobin