Faure Ugo
Faure Ugo

Reputation: 69

Microsoft Graph API inaccurate error message

I'm trying to add users in an Azure AD B2C using the Microsoft Graph API. My code works just fine when I add a User, but I tried to add it a second time with the same info given to the API (mail, names, etc...) and expected an error like

User already exists

or something similar.

My API call is done like this :

var result = await graphClient.Users.Request().AddAsync(new User()
            {
                AccountEnabled = true,
                Mail = "[email protected]",
                MailNickname = "example",
                UserPrincipalName = "[email protected]",
                Surname = "TEST",
                DisplayName = "test",
                GivenName = "TEST test",
                PasswordProfile = new PasswordProfile
                {
                    ForceChangePasswordNextSignIn = true,
                    Password = "tmpPwd"
                }
            });

Keeping in mind that my first call correctly add the user to the AD, why the API would return me this message :

Microsoft.Graph.ServiceException : 'Code: Request_BadRequest
Message: One or more properties contains invalid values.

Thank you in advance.

Upvotes: 0

Views: 1106

Answers (1)

Sruthi J
Sruthi J

Reputation: 1602

According to the document Create the user using the below property without mail .so that when a user has already existed then you will get the error as below

Code: Request_BadRequest
Message: Another object with the same value for property userPrincipalName already exists.

code:

var result = await graphClient.Users.Request().AddAsync(new User()
            {
                AccountEnabled = true,
               
                MailNickname = "example",
                UserPrincipalName = "[email protected]",
                Surname = "TEST",
                DisplayName = "test",
                GivenName = "TEST test",
                PasswordProfile = new PasswordProfile
                {
                    ForceChangePasswordNextSignIn = true,
                    Password = "password@1234"
                }
            });
            Console.WriteLine(JsonConvert.SerializeObject(result));
        }

Using additional property may change the error context as you are receiving currently

Upvotes: 1

Related Questions