Stuart Frankish
Stuart Frankish

Reputation: 868

Microsoft Graph API - Update Existing Group

I'm trying to update the description for an existing group in Azure AD, but I'm getting an error message that I'm not sure how to resolve.

public static async Task<bool> UpdateGroup(GraphServiceClient graphClient, Group group)
{
    // Update the group.
    Group grp = await graphClient.Groups[group.Id].Request().GetAsync();
    grp.Description = group.Description;

    await graphClient.Groups[group.Id].Request().UpdateAsync(grp);

    return true;
}

The above just throws an exception:

Code: BadRequest Message: Operation not supported.

I'm not sure if this is a lack of enabled permissions for the API in Azure or if updating a group genuinely isn't supported? I can Create/Delete groups easily enough, so surely updating an existing group should be just as easy?

Upvotes: 4

Views: 1881

Answers (3)

Thomas
Thomas

Reputation: 732

Big thanks to @Marc LaFleur above. The package version I am using differs; the code that worked for me is slightly different without the .Request() and UpdateAsync() methods.

        await _graphServiceClient.Groups[ActiveDirectoryGroupObjectId]
            .PatchAsync(new Group()
            {
                DisplayName = "Updated Name",
                Description = "Updated Description"
                // Additional properties...
            });

Upvotes: 1

Marc LaFleur
Marc LaFleur

Reputation: 33122

Your problem is that you're first populating grp, changing a single property, and then attempting to PATCH the entire Group object. So along with your updated description, you're also attempting to PATCH (and this is where the error comes from) several read-only properties (e.g. id).

Your code should look like this:

await graphClient
    .Groups[group.Id]
    .Request()
    .UpdateAsync(new Group() {
        Description = group.Description
    });

Upvotes: 4

Seiya Su
Seiya Su

Reputation: 1874

The update operation for group is supported in Graph API.enter image description here

I don't have VS env in personal PC, so I cannot test the Graph Library now(I can test it on Oct 2th). If Graph Libray have supported the API too, the code should works well, so you can check your permission config first.

await graphClient.Groups[group.Id].Request().UpdateAsync(grp);

Some reference from Graph Library code:

/// <summary>
 /// Updates the specified Group using PATCH.
 /// </summary>
 /// <param name="groupToUpdate">The Group to update.</param>
 /// <returns>The updated Group.</returns>
  public System.Threading.Tasks.Task<Group> UpdateAsync(Group groupToUpdate)
   {
   }




   /// <summary>
    /// Updates the specified Group using PATCH.
    /// </summary>
    /// <param name="groupToUpdate">The Group to update.</param>
    /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param>
    /// <returns>The updated Group.</returns>
    public async System.Threading.Tasks.Task<Group> UpdateAsync(Group groupToUpdate, CancellationToken cancellationToken)
    {

    } 

https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/f807196101e20d30fbc8628206a2eb5850334a92/src/Microsoft.Graph/Requests/Generated/GroupRequest.cs

Upvotes: 0

Related Questions