To disable a Dynamics CRM 2016 user with C # code, SetStateRequest is currently used. Example:
var requestToDisableUser = new SetStateRequest() { EntityMoniker = new EntityReference("systemuser", userGuid), State = new OptionSetValue(1), Status = new OptionSetValue(-1) }; organizationService.Execute(requestToDisableUser);
However, according to Microsoft, SetStateRequest is deprecated and needs to be replaced with Update
But when I try to use Update to disconnect the user
Example:
var userToDisable = new Entity("systemuser", userGuid) { ["statecode"] = new OptionSetValue(1), ["statuscode"] = new OptionSetValue(-1) }; service.Update(userToDisable);
Then it raises an error:
Unhandled exception: System.ServiceModel.FaultException`1 [Microsoft.Xrm.Sdk.OrganizationServiceFault]: The "systemuser" object does not contain an attribute with the name = 'Statecode'.
This is true because the Entity system user does not have a status code.
And the Entity system user has the IsDisabled attribute, but that one is read-only.
So how can a user be disconnected / enabled without using SetStateRequest ?
source share