Patsuan
Patsuan

Reputation: 137

I keep getting a 400 code error when trying to create an O365 group with Graph api

This is part of a test project, which I use to test things before adding them to a larger project, so I'm just trying to create a new unified group in the simplest way possible here.

Here are the function supposed to do it and the class that represents the group :

private static void CreateGroup(HttpClient httpclient)
        {
            Group group = new Group()
            {
                description = "group created with Graph",
                displayName = "creationTest",
                groupTypes = new List<string>() { "Unified" },
                mailEnabled = true,
                mailNickname = "creationTest",
                securityEnabled = false
            };
            var json = JsonConvert.SerializeObject(group);
            var body = new StringContent(json, Encoding.UTF8, "application/json");

            var response = httpclient.PostAsync("https://graph.microsoft.com/v1.0/groups", body).Result;
        }

 public class Group
    {
        public string description { get; set; }
        public string displayName { get; set; }
        public List<string> groupTypes { get; set; }
        public bool mailEnabled { get; set; }
        public string mailNickname { get; set; }
        public bool securityEnabled { get; set; }

    }

The HttpClient object has a Bearer access token. The token is valid as it is used in other requests in the program with no problem.

However I always get a 400 Bad Request error. It seems so simple and yet it doesn't work and at this point, I really have no idea why. I tried to write the json myself instead of using JSONConvert, like this :

string postBody = "{\"mailEnabled\":true,"
                        + "\"displayName\":\"creationTest\","
                        + "\"mailNickName\":\"creationTest\","
                        + "\"securityEnabled\":false,"
                        + "\"groupTypes\":[\"Unified\"]"
                        + "}";

But the result is the same. The request uri seems fine to me, so : what is the problem?

Upvotes: 1

Views: 59

Answers (1)

Marc LaFleur
Marc LaFleur

Reputation: 33122

The most common reason to get a 400 error returned is that your mailNickName group name is already in use.

Each Group in your organization needs to have a unique mailNickName.

Upvotes: 1

Related Questions