Search code examples
azuremicrosoft-graph-apiazure-ad-b2cmicrosoft-graph-sdks

Create user custom attribute for whole B2C instance via graph api or graph sdk


I want to create 'user custom attribute' for whole b2c instance via graph api or sdk. Just like I do thru Azure Portal following ms documentation quoted below.

1. Sign in to the Azure portal as the global administrator of your Azure AD B2C tenant.
2. Make sure you're using the directory that contains your Azure AD B2C tenant by switching to it in the top-right corner of the Azure portal. 
3. Select your subscription information, and then select Switch Directory.
4. Choose All services in the top-left corner of the Azure portal, search for and select Azure AD B2C.
5. Select User attributes, and then select Add.
6. Provide a Name for the custom attribute (for example, "ShoeSize")
7. Choose a Data Type. Only String, Boolean, and Int are available.
8. Optionally, enter a Description for informational purposes.
9. Click Create.

Just to be clear I don't want to create user with custom attribute, but a custom attribute for all users in b2c.

Can I do it and if yes how?


Solution

  • You can use Create extensionProperty to create a 'user custom attribute' for whole b2c instance.

    Please note we need to specify the targetObjects as "User".

    An example:

    Post https://graph.microsoft.com/v1.0/applications/{object id of the Azure AD application}/extensionProperties
    
    {"name":"customAttribute","dataType":"string","targetObjects":["User"]}
    

    It will generate an extension property named extension_{client id of the Azure AD application without "-"}_customAttribute.

    Then you can update the extension property for any of your B2C account:

    Patch https://graph.microsoft.com/v1.0/users/{user id}
    
    {"extension_{client id of the Azure AD application without "-"}_customAttribute":"value"}
    

    Please note:

    The user custom attribute you created on Azure Portal is associated with the Azure AD app named b2c-extensions-app. Do not modify. Used by AADB2C for storing user data. You can find it under Azure AD B2C -> App registrations -> All applications.

    But the custom attribute you created using Microsoft Graph won't appear on Azure Portal | User attributes no matter whether you create the custom attribute under b2c-extensions-app or any other Azure AD app.