1
votes

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.

1
There isn't enough here to go on. How are you authenticating? What SDK are you using? What did you use for the userPrincipalName?Marc LaFleur

1 Answers

4
votes

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;