I'm automating the creation of an application user in Dynamics 365. The user does get created, but when I try to assign a role to it later in the code, I get this message: The user Id(s) [a guid here] is invalid.
When I try assigning the user a role from the Dynamics web UI, I get a variant of the same error: The user ID associated with the current record is not valid.
.
I am using a CrmServiceClient in C# to connect to the organization.
I am supplying the following fields when creating the user programmatically:
firstname
lastname
businessunitid
applicationid
internalemailaddress
One thing I noticed is that when I look at the user via the web UI, the app id I assigned in the creation does not show up.
This is my current code:
private void SetUpPermissions()
{
var roleId = GetRoleId();
var userId = CreateApplicationUser();
CrmSvc.Associate(
"systemuser",
userId,
new Relationship("systemuserroles_association"),
new EntityReferenceCollection {
new EntityReference("role", roleId)
});
}
private Guid CreateApplicationUser() => CrmSvc.CreateNewRecord("systemuser", new Dictionary<string, CrmDataTypeWrapper>
{
{ "firstname", new CrmDataTypeWrapper(ProposalManagerApplicationName, CrmFieldType.String) },
{ "lastname", new CrmDataTypeWrapper("Application", CrmFieldType.String) },
{ "businessunitid", new CrmDataTypeWrapper(BusinessUnitId.Value, CrmFieldType.Lookup) },
{ "applicationid", new CrmDataTypeWrapper(ApplicationId, CrmFieldType.Key) },
{ "internalemailaddress", new CrmDataTypeWrapper("testcrm3@mydomain.com", CrmFieldType.String) }
});
I would expect this user to show up in the Web UI the same way an application user created via the UI would, but it does not. It does not show the application id I assigned to it in the code, and it throws the aforementioned error when trying to assign a role to it.
Am I doing something wrong? If the answer is backed up by existing documentation please point me to it as well so I can learn why I did not find it.