How to update azure ad b2c custom user attribute using graph api
Please check the below code changes and also verify whether the user you are trying update has the custom attribute or not.
public static async Task UpdateCustomAtrributeUserId(GraphServiceClient graphClient)
{
Console.Write("Enter user object ID: ");
string userId = Console.ReadLine();
string CustomAtrribute = "B2C_Custom_AtrributeName";
Console.WriteLine($"Looking for user with object ID '{userId}'...");
try
{
//Get User details to Verify the existing values
var result = await graphClient.Users[userId]
.Request()
.Select($"id,givenName,surName,displayName,identities,{CustomAtrribute}")
.GetAsync();
Console.WriteLine(result);
if (result != null)
{
//Enter the New custom attribute value
Console.WriteLine("Enter custom attribute value");
string updatecustomeattribvalue = Console.ReadLine();
//Fill custom attribute value
IDictionary<string, object> extensionInstance = new Dictionary<string, object>();
extensionInstance.Add(CustomAtrribute, updatecustomeattribvalue);
//Updating the custom attribute
var updatedresult = await graphClient.Users[userId]
.Request()
.UpdateAsync(new User {
AdditionalData = extensionInstance
});
Console.WriteLine(JsonConvert.SerializeObject(updatedresult));
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.Message);
Console.ResetColor();
}
}