Introduction
This article demonstrates how to create a custom workflow activity to Enable or Disable the Dynamics CRM system user. We can make use of the below custom activity within the workflow or action. We have to provide the User Account (that has to be Enabled/Disabled) and the flag to Enable or Disable.
Steps to create a Visual Studio project for the custom workflow activity
Visual Studio Project: EnableOrDisableUser
Make sure all the references mentioned in the screen shot are added to the Visual Studio project.

ActivityToEnableOrDisableUser.cs
- using System;
- using System.Activities;
- using Microsoft.Xrm.Sdk;
- using Microsoft.Xrm.Sdk.Workflow;
- using System.ServiceModel;
- using Microsoft.Crm.Sdk.Messages;
- namespace EnableOrDisableUser {
- public class ActivityToEnableOrDisableUser: CodeActivity {
- [Input("UserAccount")]
- [ArgumentDescription("User Account that has to be Enabled or Disabled.")]
- public InArgument < string > UserAccount {
- get;
- set;
- }
- [Input("IsEnabled")]
- public InArgument < bool > IsEnabled {
- get;
- set;
- }
- protected override void Execute(CodeActivityContext executionContext) {
- ITracingService tracer = executionContext.GetExtension < ITracingService > ();
- IWorkflowContext context = executionContext.GetExtension < IWorkflowContext > ();
- IOrganizationServiceFactory serviceFactory = executionContext.GetExtension < IOrganizationServiceFactory > ();
- IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
- tracer.Trace("Starting Activity to Enable/Disable User - Workflow");
- try {
- tracer.Trace("Set the Impersonation service account.");
- //To get the pre-configured Impersonation user in Site Settings
- string impersonationAccount = "DomainName\ImpersonationServiceAccount";
- tracer.Trace("To get the impersonation account to perform the enable or disable User Account");
- // To get the impersonation account to perform the enable or disable User Account
- Guid impersonationID = AppEnableOrDisableUser.GetUserId(service, impersonationAccount);
- IOrganizationService impersonationService = serviceFactory.CreateOrganizationService(impersonationID);
- tracer.Trace("To get the User Account inorder to Enable or Disable");
- // To get the User Account Guid inorder to Enable or Disable
- string usr = UserAccount.Get < string > (executionContext);
- tracer.Trace(" User name got from the input param:{0}", usr);
- Entity user = AppEnableOrDisableUser.GetUser(service, usr);
- if (user != null) {
- tracer.Trace("Got User Account inorder to Enable or Disable.");
- SetStateRequest request = new SetStateRequest();
- if (IsEnabled.Get < bool > (executionContext)) {
- request.EntityMoniker = user.ToEntityReference();
- request.State = new OptionSetValue(0); // Enabled
- request.Status = new OptionSetValue(-1);
- tracer.Trace("Setting User as Enabled.");
- } else {
- request.EntityMoniker = user.ToEntityReference();
- request.State = new OptionSetValue(1); // Disabled
- request.Status = new OptionSetValue(-1);
- tracer.Trace("Setting User as Disabled.");
- }
- impersonationService.Execute(request);
- tracer.Trace("Ran the script to set the User status.");
- }
- } catch (FaultException < Microsoft.Xrm.Sdk.OrganizationServiceFault > ex) {
- tracer.Trace("Error: Activity to Enable/Disable User - Workflow:{0}", ex.Message);
- } catch (Exception e) {
- tracer.Trace("Error: Activity to Enable/Disable User - Workflow:{0}", e.Message);
- throw new InvalidPluginExecutionException(e.Message);
- }
- }
- }
- }



murali krishnaPosted Jan 12, 2020, 10:29 AM
Why you using 2 codes can give explanation
murali krishnaPosted Jan 11, 2020, 11:08 PM
Hiii....sir what is the use of Enable or Disable the Dynamics CRM system user.