Reputation: 667
As of now, it seems there is no standard way of achieving it.
After researching for quite some time I came up with the following piece of code that, unfortunatelly returns me an obscure error:
var user = await _graphApiService.GetUserAsync(userId); ; // gets existent user
var tempUser = new User
{
Extensions = new UserExtensionsCollectionPage
{
AdditionalData = new Dictionary<string, object>()
{
{ residentialPostcode, userToUpdate.extensionAttributes.extension_9614eff520454054b6d79fbe0c7a5491_residentialPostcode },
{ address, userToUpdate.extensionAttributes.extension_9614eff520454054b6d79fbe0c7a5491_address },
{ city, userToUpdate.extensionAttributes.extension_9614eff520454054b6d79fbe0c7a5491_city },
{ telephone, userToUpdate.extensionAttributes.extension_9614eff520454054b6d79fbe0c7a5491_Telephone },
{ termsOfUseConsentVersion, userToUpdate.extensionAttributes.extension_9614eff520454054b6d79fbe0c7a5491_termsOfUseConsentVersion },
{ dateOfBirth, userToUpdate.extensionAttributes.extension_9614eff520454054b6d79fbe0c7a5491_dateOfBirth },
{ termsOfUseConsentDateTime, userToUpdate.extensionAttributes.extension_9614eff520454054b6d79fbe0c7a5491_termsOfUseConsentDateTime },
{ haspassword, userToUpdate.extensionAttributes.extension_9614eff520454054b6d79fbe0c7a5491_haspassword }
}
}
};
user.Extensions = tempUser.Extensions;
await _graphServiceClient.Users[$"{userId}"]
.Request()
.UpdateAsync(userToUpdate);
Finally the error:
"Error! Updating User information failed with message Code: BadRequest\r\nMessage: The request is currently not supported on the targeted entity set\r\nInner error:\r\n\tAdditionalData:\r\n\tdate: 2022-01-14T21:23:33\r\n\trequest-id: d6c9c8b9-47fc-4aa0-bb9b-328a5b260753\r\n\tclient-request-id: d6c9c8b9-47fc-4aa0-bb9b-328a5b260753\r\nClientRequestId: d6c9c8b9-47fc-4aa0-bb9b-328a5b260753\r\n"
--
Additional information:
My Graph API app has the following delegated permissions:
Directory.AccessAsUser.All
Directory.ReadWrite.All
User.Read
User.ReadWrite.All
Graph SDK version
<PackageReference Include="Microsoft.Graph" Version="4.14.0" />
Upvotes: 1
Views: 929
Reputation: 667
The following worked the error out. It seems that the proper way to update b2c User object custom fields is by assigning the new value to User.AdditionalData
directly and not toUser.Extensions.AdditionalData
FWIW, the code:
var additionalData = new Dictionary<string, object>();
additionalData[residentialPostcode] = userToUpdate.extensionAttributes.extension_9614eff520454054b6d79fbe0c7a5491_residentialPostcode;
additionalData[address] = userToUpdate.extensionAttributes.extension_9614eff520454054b6d79fbe0c7a5491_address;
additionalData[city] = userToUpdate.extensionAttributes.extension_9614eff520454054b6d79fbe0c7a5491_city;
additionalData[telephone] = userToUpdate.extensionAttributes.extension_9614eff520454054b6d79fbe0c7a5491_Telephone;
additionalData[termsOfUseConsentVersion] = userToUpdate.extensionAttributes.extension_9614eff520454054b6d79fbe0c7a5491_termsOfUseConsentVersion;
additionalData[termsOfUseConsentDateTime] = userToUpdate.extensionAttributes.extension_9614eff520454054b6d79fbe0c7a5491_termsOfUseConsentDateTime;
additionalData[haspassword] = userToUpdate.extensionAttributes.extension_9614eff520454054b6d79fbe0c7a5491_haspassword;
additionalData[dateOfBirth] = userToUpdate.extensionAttributes.extension_9614eff520454054b6d79fbe0c7a5491_dateOfBirth;
var tempUser = new User
{
AdditionalData = additionalData
};
await _graphApiService.EditUserAsync(userId, tempUser);
Upvotes: 1