Creating a user in Azure AD with custom user attribute using graph api SDK

emctradr 31 Reputation points
2020-10-02T09:40:47.853+00:00

I am creating a rest API that will create user with custom attribute into our Azure AD B2C tenant using graph SDK.

Sample code below:

 var oUser = new Microsoft.Graph.User()
          {
            AccountEnabled = true, //True by default
            DisplayName = request.DisplayName,
            GivenName = request.GivenName,
            Surname = request.Surname,
            UserPrincipalName = request.UserPrincipalName,
            MailNickname = request.MailNickname,
            MobilePhone = request.MobileNumber,
            PostalCode = request.PostalCode,


            //extesnsion_AccountId = request.AccountId, 
            //LeadId = request.LeadId,
            PasswordProfile = new PasswordProfile() { ForceChangePasswordNextSignIn = true, Password = _configuration["AppSettings:UserDefaultPWD"] }// SET PASSWORD IN CONFIG

          };

          //User custom attributes
          IDictionary<string, object> extensionInstance = new Dictionary<string, object>();
          extensionInstance.Add("extension_{b2c-extensions-app. clientID }_{UserAttribute1}", request.attribute1);
          extensionInstance.Add("extension_{b2c-extensions-app. clientID }_{UserAttribute2}", request.attribute2);
          oUser.AdditionalData = extensionInstance;

          var client = _graphClient.Connect();
          var user = await client.Users.Request().AddAsync(oUser);

However I always get a "The following extension properties are not available" error.

The user attribute is configured in Azure portal.

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
21,420 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. 2020-10-02T16:27:16.823+00:00

    This can happen if the extension property TargetObjects property does not include "User". Please double check.

    --
    Please let us know if this answer was helpful to you. If so, please remember to mark it as the answer so that others in the community with similar questions can more easily find a solution.


  2. 2020-10-30T06:33:46.653+00:00

    Somehow I could not able to reply to your post anonymous user-msft , so posting in the answer part.

    Question: I need the same solution in Java SDK using Graph API.

    Below is my code which i have written in Java using microsoft-graph-2.3.1.jar :

        com.microsoft.graph.models.extensions.User user = new com.microsoft.graph.models.extensions.User();
        user.displayName = "xxxxxxxxx";
    
        List<ObjectIdentity> identitiesList = new LinkedList<ObjectIdentity>();
        ObjectIdentity identities = new ObjectIdentity();
        identities.signInType = "emailAddress";
        identities.issuer = "<tenant-name>.onmicrosoft.com";
        identities.issuerAssignedId = "xxxxxxxx@gmail.com";
        identitiesList.add(identities);
    
        user.identities = identitiesList;
        PasswordProfile passwordProfile = new PasswordProfile();
        passwordProfile.password = "xxxxxxxxxx";
        passwordProfile.forceChangePasswordNextSignIn = false;
        user.passwordProfile = passwordProfile;
        user.passwordPolicies = "DisablePasswordExpiration";
    
        Now the challenge is to set **extensionInstance ..... ** similar to above...
        How should I set in Java code how you wrote the same in .Net as above IDictionary<string, object> extensionInstance = new Dictionary<string, object>(); 
    
        Please do help me on the same.
    

    Thanks in advance!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.