Custom Workflow Activity To Enable Or Disable System User Account

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
  1. using System;  
  2. using System.Activities;  
  3. using Microsoft.Xrm.Sdk;  
  4. using Microsoft.Xrm.Sdk.Workflow;  
  5. using System.ServiceModel;  
  6. using Microsoft.Crm.Sdk.Messages;  
  7. namespace EnableOrDisableUser {  
  8.     public class ActivityToEnableOrDisableUser: CodeActivity {  
  9.         [Input("UserAccount")]  
  10.         [ArgumentDescription("User Account that has to be Enabled or Disabled.")]  
  11.         public InArgument < string > UserAccount {  
  12.                 get;  
  13.                 set;  
  14.             }  
  15.             [Input("IsEnabled")]  
  16.         public InArgument < bool > IsEnabled {  
  17.             get;  
  18.             set;  
  19.         }  
  20.         protected override void Execute(CodeActivityContext executionContext) {  
  21.             ITracingService tracer = executionContext.GetExtension < ITracingService > ();  
  22.             IWorkflowContext context = executionContext.GetExtension < IWorkflowContext > ();  
  23.             IOrganizationServiceFactory serviceFactory = executionContext.GetExtension < IOrganizationServiceFactory > ();  
  24.             IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);  
  25.             tracer.Trace("Starting Activity to Enable/Disable User - Workflow");  
  26.             try {  
  27.                 tracer.Trace("Set the Impersonation service account.");  
  28.                 //To get the pre-configured Impersonation user in Site Settings  
  29.                 string impersonationAccount = "DomainName\ImpersonationServiceAccount";  
  30.                 tracer.Trace("To get the impersonation account to perform the enable or disable User Account");  
  31.                 // To get the impersonation account to perform the enable or disable User Account  
  32.                 Guid impersonationID = AppEnableOrDisableUser.GetUserId(service, impersonationAccount);  
  33.                 IOrganizationService impersonationService = serviceFactory.CreateOrganizationService(impersonationID);  
  34.                 tracer.Trace("To get the User Account inorder to Enable or Disable");  
  35.                 // To get the User Account Guid inorder to Enable or Disable  
  36.                 string usr = UserAccount.Get < string > (executionContext);  
  37.                 tracer.Trace(" User name got from the input param:{0}", usr);  
  38.                 Entity user = AppEnableOrDisableUser.GetUser(service, usr);  
  39.                 if (user != null) {  
  40.                     tracer.Trace("Got User Account inorder to Enable or Disable.");  
  41.                     SetStateRequest request = new SetStateRequest();  
  42.                     if (IsEnabled.Get < bool > (executionContext)) {  
  43.                         request.EntityMoniker = user.ToEntityReference();  
  44.                         request.State = new OptionSetValue(0); // Enabled  
  45.                         request.Status = new OptionSetValue(-1);  
  46.                         tracer.Trace("Setting User as Enabled.");  
  47.                     } else {  
  48.                         request.EntityMoniker = user.ToEntityReference();  
  49.                         request.State = new OptionSetValue(1); // Disabled  
  50.                         request.Status = new OptionSetValue(-1);  
  51.                         tracer.Trace("Setting User as Disabled.");  
  52.                     }  
  53.                     impersonationService.Execute(request);  
  54.                     tracer.Trace("Ran the script to set the User status.");  
  55.                 }  
  56.             } catch (FaultException < Microsoft.Xrm.Sdk.OrganizationServiceFault > ex) {  
  57.                 tracer.Trace("Error: Activity to Enable/Disable User - Workflow:{0}", ex.Message);  
  58.             } catch (Exception e) {  
  59.                 tracer.Trace("Error: Activity to Enable/Disable User - Workflow:{0}", e.Message);  
  60.                 throw new InvalidPluginExecutionException(e.Message);  
  61.             }  
  62.         }  
  63.     }  
  64. }  
AppEnableOrDisableUser.cs
  1. using System;  
  2. using System.ServiceModel;  
  3. using Microsoft.Xrm.Sdk;  
  4. using Microsoft.Xrm.Sdk.Query;  
  5. using Microsoft.Xrm.Sdk.Messages;  
  6. namespace EnableOrDisableUser {  
  7.     public class AppEnableOrDisableUser {  
  8.         public static Guid GetUserId(IOrganizationService service, string userName) {  
  9.             Guid systemUserId = Guid.Empty;  
  10.             try {  
  11.                 QueryByAttribute queryByAttribute = new QueryByAttribute("systemuser");  
  12.                 ColumnSet columns = new ColumnSet("systemuserid");  
  13.                 queryByAttribute.AddAttributeValue("domainname", userName);  
  14.                 EntityCollection retrieveUser = service.RetrieveMultiple(queryByAttribute);  
  15.                 systemUserId = ((Entity) retrieveUser.Entities[0]).Id;  
  16.             } catch (FaultException < OrganizationServiceFault > e) {  
  17.                 throw new Exception("Error: >>" + e.Message);  
  18.             }  
  19.             return systemUserId;  
  20.         }  
  21.         public static Entity GetUser(IOrganizationService service, string userName) {  
  22.             Entity systemUser = new Entity();  
  23.             try {  
  24.                 QueryByAttribute queryByAttribute = new QueryByAttribute("systemuser");  
  25.                 ColumnSet columns = new ColumnSet(new String[] {  
  26.                     "systemuserid",  
  27.                     "firstname",  
  28.                     "lastname"  
  29.                 });  
  30.                 queryByAttribute.AddAttributeValue("domainname", userName);  
  31.                 EntityCollection retrieveUser = service.RetrieveMultiple(queryByAttribute);  
  32.                 systemUser = (Entity) retrieveUser.Entities[0];  
  33.             } catch (FaultException < OrganizationServiceFault > e) {  
  34.                 throw new Exception("Error: >>" + e.Message);  
  35.             }  
  36.             return systemUser;  
  37.         }  
  38.     }  
  39. }  
To know how to deploy the custom workflow activity, click here.
 

To configure the custom workflow activity in Out of the Box workflow with parameters

 
To add EnableOrDisableUser custom activity in the workflow.
 
 
After adding the custom activity, we need to set the Input parameters Users Account (map to the username field from the entity) and IsEnabled (Option button with True or False value).
 
 

Summary

 
In this article, I discussed how we can create a reusable Custom Workflow Activity to enable or disable the system user account. Hope it will be useful for you all. Very soon, I will be back with a new article.


Similar Articles