Search code examples
office365office365apimicrosoft-graph-api

Create and update group with Microsoft Graph returns 500 - Internal Server Error


I am new to REST programming and I am trying to use the Microsoft Graph API to create and update Office 365 Groups, but for both operations I get a 500 - Internal Server Error response not saying much to figure out what is wrong.

Here is a simplification of the code to create a new group:

public async Task<Group> CreateGroup()
{
    string accessToken = GetAccessToken(); // method for getting the Graph token

    string newGroup =   "{" +
                            "\"group\": " +
                            "{" +
                                "\"description\": \"description-value\"," +
                                "\"displayName\": \"displayName-value\"," +
                                "\"groupTypes\": [" +
                                   "\"Unified\"" +
                                "]," +
                                "\"mail\": \"[email protected]\"," +
                                "\"mailEnabled\": true," +
                                "\"mailNickname\": \"mailNickname-value\"," +
                                "\"securityEnabled\": \"false\"" +
                            "}" +
                        "}";

    using (var client = new HttpClient())
    {
        using (var request = new HttpRequestMessage(HttpMethod.Post, https://graph.microsoft.com/v1.0/groups))
        {
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

            request.Content = new StringContent(JsonConvert.SerializeObject(newGroup), Encoding.UTF8, "application/json");

            using (HttpResponseMessage response = await client.SendAsync(request))
            {
                if (response.IsSuccessStatusCode)
                {
                    // parse and return content
                }
                else 
                {
                    // handle error
                }
            }
        }
    }
}

and here are the request and the response message:

RequestMessage  {Method: POST, RequestUri: 'https://graph.microsoft.com/v1.0/groups', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
  Authorization: Bearer XXX...
  Content-Type: application/json; charset=utf-8
  Content-Length: 243
}}


ResponseMessage  {StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, 
    Headers:
    {
      Transfer-Encoding: chunked
      request-id: xxxx-...
      client-request-id: xxxx-...
      x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"West Europe","Slice":"SliceA","ScaleUnit":"001","Host":"AGSFE_IN_1","ADSiteName":"AMS"}}
      Cache-Control: private
      Date: Sun, 06 Dec 2015 12:51:26 GMT
      Server: Microsoft-IIS/8.5
      X-Powered-By: ASP.NET
      Content-Type: application/json; charset=utf-8
    }
}

I have seen two posts here where people have managed to create unified groups, so I am guessing that it something in the code which I cannot find. Has anybody else experienced the same error?


Solution

  • The request shouldn't have all the group properties wrapped in a "group" element. A correct request payload sample would be:

    {  
        "description": "description-value",
        "displayName": "displayName-value",
        "groupTypes": [
          "Unified"
        ],
        "mailEnabled": true,
        "mailNickname": "mailNickname-value",
        "securityEnabled": false
    }
    

    I created https://github.com/OfficeDev/microsoft-graph-docs/issues/65 to correct the sample used in the documentation.