To disable a Dynamics CRM 2016 user with C# code the 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 the SetStateRequest is deprecated and should be replaced by using Update
But when I try to use Update to disable a user
Example:
var userToDisable = new Entity("systemuser", userGuid)
{
["statecode"] = new OptionSetValue(1),
["statuscode"] = new OptionSetValue(-1)
};
service.Update(userToDisable);
Then it raises the error :
Unhandled Exception: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: 'systemuser' entity doesn't contain attribute with Name = 'statecode'.
Which is true, because the systemuser Entity doesn't have a statecode.
And the systemuser Entity has a IsDisabled
attribute, but that one is read-only.
So how can a user then be disabled/enabled without using SetStateRequest
?
Access Dynamics 365 as a user with the System Administrator role. Navigate to Settings -> Security. Select Users, and then change the view to Disabled users. If you see your user as disabled, then enable it.
You can enable and disable plugin steps using CRM Web User Interface. If you are going to enable disable plugins steps, you are in right place, follow this article to do your job.
The following client APIs are deprecated to reorganize the Xrm client API object model to better align with the need of using the same client scripts without having to change them based on the context or the client (web client or the new Unified Interface) where they run.
Go to Settings > Security. Choose Users > Enabled Users, and then click a user's full name. In the user form, scroll down under Administration to the Client Access License (CAL) Information section and select Administrative for Access Mode. You then need to remove the Dynamics 365 (online) license from the account.
I don’t think it is possible, unless someone proves it is. To support my opinion: the latest SDK (8.2.1.1; 4/6/2017) still gives an example with SetStateRequest
in SDK\SampleCode\CS\BusinessDataModel\UsersAndRoles\DisableOREnableUser.cs
. Therefore, it should be the recommend way to do it, regardless the fact it is marked as deprecated in another part of documentation.
You might start an incident with MS support, or give suggestion on Connect; but according my experience, they don’t care much if workaround is available.
After CRM online 2015 update 1, special messages were deprecated for special attributes. Read more.
Per metadata, IsDisabled
attribute of systemuser
may be showing as not valid for update. And few other business owned entities like Team, BU, etc won't use statecode & statuscode.
You should be using IsDisabled
attribute if planning to not to use deprecated methods.
It's probably a missing piece in documentation. But hints can be found wrt Specialized operations using Update message like below per MSDN
These specialized messages will continue to work with the 2011 endpoint. However, the recommendation is to use the UpdateRequest or Update method when possible to set these attributes. The Update message simplifies the Organization Service and makes it easier to code standard data integration tools used with Dynamics 365. In addition, it is easier to code and register a plug-in to execute for a single Update message instead of multiple specialized messages. The AttributeMetadata.IsValidForUpdate property for the above listed attributes has been changed to true in this release to enable this capability.
You can continue to use these specialized messages of the 2011 endpoint in your code. However, the Web API that eventually replaces the Organization Service supports only the Update message for these types of operations. If you want to get a head start on changing your code to align with the Web API, you can now do so. See Use the Microsoft Dynamics 365 Web API for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With