private void DeleteExistingSteps(SdkMessageInfo messageInfo, string unsecureConfig)
        {
            log.Log($"Retrieving existing steps: '{messageInfo.ExecutionStage}' '{messageInfo.MessageId}'"
                    + $" '{messageInfo.FilteredId}' '{unsecureConfig}' ...");
            var existingSteps =
                (from stepQ in new XrmServiceContext(service).SdkMessageProcessingStepSet
                 where (stepQ.ExecutionStage == SdkMessageProcessingStep.ExecutionStageEnum.Preoperation ||
                        stepQ.ExecutionStage == SdkMessageProcessingStep.ExecutionStageEnum.Postoperation) &&
                 stepQ.SDKMessage == messageInfo.MessageId && stepQ.SdkMessageFilter == messageInfo.FilteredId &&
                 stepQ.Configuration == unsecureConfig
                 select new SdkMessageProcessingStep
            {
                SdkMessageProcessingStepIdId = stepQ.Id,
                Name = stepQ.Name
            }).ToList();

            log.Log($"Retrieved {existingSteps.Count} steps.");

            foreach (var existingStep in existingSteps)
            {
                log.Log($"Deleting step '{existingStep.Id}':'{existingStep.Name}', with config '{unsecureConfig}' ...");
                service.Delete(SdkMessageProcessingStep.EntityLogicalName, existingStep.Id);
                log.Log($"Deleted step.");
            }
        }
        private Guid?CreateStep(SdkMessageInfo messageInfo, string entityName, int order,
                                string unsecureConfig = null, params string[] filteringAttributes)
        {
            var stepName = GetStepName(messageInfo.TypeName, messageInfo.MessageName, entityName,
                                       messageInfo.ExecutionStage.ToString(), "Sync", order.ToString());

            var step =
                new SdkMessageProcessingStep
            {
                Name           = stepName,
                ExecutionOrder = order,
                ExecutionStage = messageInfo.ExecutionStage,
                ExecutionMode  = SdkMessageProcessingStep.ExecutionModeEnum.Synchronous,
                Deployment     = SdkMessageProcessingStep.DeploymentEnum.ServerOnly,
                AsynchronousAutomaticDelete = false,
                SDKMessage       = messageInfo.MessageId,
                SdkMessageFilter = messageInfo.FilteredId,
                EventHandler     = new EntityReference(PluginType.EntityLogicalName, messageInfo.PluginTypeId)
            };

            if (filteringAttributes?.Any() == true)
            {
                step.FilteringAttributes = filteringAttributes.Aggregate((a1, a2) => a1 + "," + a2);
            }

            if (unsecureConfig.IsNotEmpty())
            {
                step.Configuration = unsecureConfig;
            }

            var stepId = service.Create(step);

            log.Log($"Created step '{stepName}', with unsecure config '{unsecureConfig}'.");

            return(stepId);
        }