Activate-Deactivate Plugins/Process with code

while deploying our customisation to target environment many times we need disable/deactivate plugins and or process. Manually, its straight forward but what about when you are working with DevOps or you cannot touch target environment. In such environment, we need to handle it with code. So for that here are the steps …

  • fetch workflow
private List<Guid> GetWorkflows(string processName)
        {
            List<Guid> activatedWorkflowIds = new List<Guid>();

            QueryExpression query = new QueryExpression { EntityName = "workflow" };
            query.Criteria.AddCondition("statecode", ConditionOperator.Equal, 1);
            query.Criteria.AddCondition("statuscode", ConditionOperator.Equal, 2);
            query.Criteria.AddCondition("type", ConditionOperator.Equal, 1);
            query.Criteria.AddCondition("name", ConditionOperator.Equal, processName);

            EntityCollection results = msdService.orgService.RetrieveMultiple(query);
            activatedWorkflowIds = results.Entities.ToList().Select(e => e.Id).ToList();

            return activatedWorkflowIds;
        }
  • fetch plugins
private List<Guid> GetPluginStepsByName(string processingstep_name)
        {
            // Define Condition Values
            //var processingstep_sdkmessageid = "9ebdbb1b-ea3e-db11-86a7-000a3a5473e8";
            var qePluginStep = new QueryExpression("sdkmessageprocessingstep");

            qePluginStep.ColumnSet.AddColumns("name", "description", "eventhandler", "impersonatinguserid", "supporteddeployment", "statuscode", "statecode", "sdkmessagefilterid", "sdkmessageid", "filteringattributes", "configuration", "asyncautodelete");

            //qePluginStep.Criteria.AddCondition("sdkmessageid", ConditionOperator.Equal, processingstep_sdkmessageid);
            qePluginStep.Criteria.AddCondition("name", ConditionOperator.Like, processingstep_name);

            // Add link-entity
            var qePlugin_sdkmessagefilter = qePluginStep.AddLink("sdkmessagefilter", "sdkmessagefilterid", "sdkmessagefilterid", JoinOperator.LeftOuter);
            qePlugin_sdkmessagefilter.EntityAlias = "a1";

            qePlugin_sdkmessagefilter.Columns.AddColumns("secondaryobjecttypecode", "primaryobjecttypecode");

            EntityCollection pluginSteps = msdService.orgService.RetrieveMultiple(qePluginStep);
            List<Guid> pluginsStepIDsToProcess = pluginSteps.Entities.ToList().Select(e => e.Id).ToList();
            return pluginsStepIDsToProcess;
        }
  • Assign Workflows
/// <summary>
        /// Assign wiorkflow 
        /// </summary>
        /// <param name="systemUserId"></param>
        /// <param name="activatedWorkflowIds"></param>
        private void AssignWorkflows(Guid systemUserId, List<Guid> activatedWorkflowIds)
        {
            activatedWorkflowIds.ForEach(w =>
            {
                msdService.orgService.Execute(new AssignRequest
                {
                    Assignee = new EntityReference("systemuser", systemUserId),
                    Target = new EntityReference("workflow", w)
                });
            });
        }
  • activate or deactivate Plugin or process
/// <summary>
        /// Activate-Deactivate Plugins and or workflows 
        /// </summary>
        /// <param name="disable"></param>
        /// <param name="activatedWorkflowIds"></param>
        /// <param name="activatedPluginIds"></param>
private void ToggleProcessState(bool disable, List<Guid> activatedWorkflowIds=null, List<Guid> activatedPluginIds=null)
        {

            // workflows 
            if (activatedWorkflowIds != null)
            {
                // workflow stateCode = 0 and statusCode = 1 (inactive), stateCode = 1 and statusCode = 2 (active) 
                int wfStateCode = disable ? 0 : 1;
                int wfStatusCode = disable ? 1 : 2;

                activatedWorkflowIds.ForEach(w =>
                {
                    msdService.orgService.Execute(new SetStateRequest
                    {
                        EntityMoniker = new EntityReference("workflow", w),
                        State = new OptionSetValue(wfStateCode),
                        Status = new OptionSetValue(wfStatusCode)
                    });
                });
            }

            //plugins
            if (activatedPluginIds != null)
            {
                // plugin stateCode = 1 and statusCode = 2 (inactive), stateCode = 0 and statusCode = 1 (active) 
                int pluginStateCode = disable ? 1 : 0;
                int pluginStatusCode = disable ? 2 : 1;

                activatedPluginIds.ForEach(p =>
                {
                    msdService.orgService.Execute(new SetStateRequest
                    {
                        EntityMoniker = new EntityReference("sdkmessageprocessingstep", p),
                        State = new OptionSetValue(pluginStateCode),
                        Status = new OptionSetValue(pluginStatusCode)
                    });
                });
            }
        }

That’s it for the day. Thanks!

2 thoughts on “Activate-Deactivate Plugins/Process with code

  1. hi,
    Good one. just a question, can we add this in release pipeline after import and publishing, to make sure that all the Plugins are active

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s