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.");
-
- string impersonationAccount = "DomainName\ImpersonationServiceAccount";
- tracer.Trace("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");
-
- 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);
- request.Status = new OptionSetValue(-1);
- tracer.Trace("Setting User as Enabled.");
- } else {
- request.EntityMoniker = user.ToEntityReference();
- request.State = new OptionSetValue(1);
- 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);
- }
- }
- }
- }
AppEnableOrDisableUser.cs
- using System;
- using System.ServiceModel;
- using Microsoft.Xrm.Sdk;
- using Microsoft.Xrm.Sdk.Query;
- using Microsoft.Xrm.Sdk.Messages;
- namespace EnableOrDisableUser {
- public class AppEnableOrDisableUser {
- public static Guid GetUserId(IOrganizationService service, string userName) {
- Guid systemUserId = Guid.Empty;
- try {
- QueryByAttribute queryByAttribute = new QueryByAttribute("systemuser");
- ColumnSet columns = new ColumnSet("systemuserid");
- queryByAttribute.AddAttributeValue("domainname", userName);
- EntityCollection retrieveUser = service.RetrieveMultiple(queryByAttribute);
- systemUserId = ((Entity) retrieveUser.Entities[0]).Id;
- } catch (FaultException < OrganizationServiceFault > e) {
- throw new Exception("Error: >>" + e.Message);
- }
- return systemUserId;
- }
- public static Entity GetUser(IOrganizationService service, string userName) {
- Entity systemUser = new Entity();
- try {
- QueryByAttribute queryByAttribute = new QueryByAttribute("systemuser");
- ColumnSet columns = new ColumnSet(new String[] {
- "systemuserid",
- "firstname",
- "lastname"
- });
- queryByAttribute.AddAttributeValue("domainname", userName);
- EntityCollection retrieveUser = service.RetrieveMultiple(queryByAttribute);
- systemUser = (Entity) retrieveUser.Entities[0];
- } catch (FaultException < OrganizationServiceFault > e) {
- throw new Exception("Error: >>" + e.Message);
- }
- return systemUser;
- }
- }
- }
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.