Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 892
LukStorms Avatar asked Sep 01 '17 10:09

LukStorms


People also ask

How do I enable a disabled user in Dynamics 365?

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.

Can we enable/disable the plugin steps from CRM solution?

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.

Why XRM page is deprecated?

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.

How do I disable a user in Microsoft Dynamics CRM?

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.


2 Answers

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.

like image 161
jcjr Avatar answered Oct 11 '22 12:10

jcjr


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.

like image 26
Arun Vinoth - MVP Avatar answered Oct 11 '22 14:10

Arun Vinoth - MVP