LukStorms
LukStorms

Reputation: 29677

Microsoft CRM: How to enable/disable Systemuser without deprecated SetStateRequest

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?

Upvotes: 5

Views: 3028

Answers (2)

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.

Upvotes: 1

jcjr
jcjr

Reputation: 1503

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.

Upvotes: 3

Related Questions