Sridevi
Sridevi

Reputation: 121

Creating guest users in Azure AD with Microsoft Graph API

I tried to create a guest user with Microsoft Graph API. I used the property UserType.

user.UserType =  "Guest";

But the response shows Invalid User principal Name.

I am able to create the same user in portal.

Upvotes: 1

Views: 6261

Answers (1)

Fei Xue
Fei Xue

Reputation: 14649

To add a external user to the organization, we need to use the invitation REST instead of create a user directly. Here is the REST and code sample(Microsoft Graph SDK) for your reference:

POST https://graph.microsoft.com/v1.0/invitations
Content-type: application/json
Content-length: 551

{
  "invitedUserEmailAddress": "[email protected]",
  "inviteRedirectUrl": "https://myapp.com"
}

Code Sample:

string accessToken = "";
var graphserviceClient = new GraphServiceClient(
new DelegateAuthenticationProvider(
       (requestMessage) =>
      {
          requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);

          return Task.FromResult(0);
 }));

Invitation invitation = new Invitation();
invitation.SendInvitationMessage = true;
invitation.InvitedUserEmailAddress = "[email protected]";
invitation.InviteRedirectUrl = "http://localhost";
var result= graphserviceClient.Invitations.Request().AddAsync(invitation).Result;

Upvotes: 8

Related Questions