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

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 ?

+5
source share
2 answers

I do not think it is possible if someone does not prove it. To support my opinion: the latest SDK (8.2.1.1; 4/6/2017) still gives an example with SetStateRequest in the SDK\SampleCode\CS\BusinessDataModel\UsersAndRoles\DisableOREnableUser.cs . Therefore, this should be the recommended way to do this, although it is marked as deprecated in another part of the documentation.

You can start an incident with MS support or give a suggestion on Connect; but, in my experience, they don't really care if a workaround is available.

+3
source

After the CRM online 2015 1 update, special messages were deprecated for special attributes. More details .

In metadata, the IsDisabled systemuser attribute may appear to be invalid for updating. And some other objects belonging to the business, such as Team, BU, etc., will not use the status code and status code.

You should use the IsDisabled attribute if you plan not to use deprecated methods.

This is probably a flaw in the documentation. But tips can be found by wrt specialized operations using the Update message, as shown below, for MSDN

These custom communications will continue to work with the 2011 endpoint. However, it is recommended that you use the UpdateRequest or Update method when possible to set these attributes. The Update message simplifies the organization’s service and simplifies the coding of the standard data integration tools used in Dynamics 365. In addition, it is easier to code and register the plug-in to run for a single Update message instead of several specialized messages. The AttributeMetadata.IsValidForUpdate property for the above attributes has been changed to true in this version to enable this feature.

You can continue to use these specialized endpoint 2011 messages in your code. However, the web API, which ultimately replaces the organization’s service, only supports the Update message for these types of operations. If you want to start by modifying the code to align with the web API, you can do this. For more information, see Using the Microsoft Dynamics 365 Web API.

+1
source

Source: https://habr.com/ru/post/1271422/


All Articles