private SdkMessageProcessingStepImage RegisterImage(CrmPluginRegistrationAttribute stepAttribute, SdkMessageProcessingStep step, SdkMessageProcessingStepImage[] existingImages, string imageName, ImageTypeEnum imagetype, string attributes)
        {
            if (String.IsNullOrWhiteSpace(imageName))
            {
                return(null);
            }
            var image = existingImages.Where(a =>
                                             a.SdkMessageProcessingStepId.Id == step.Id
                                             &&
                                             a.EntityAlias == imageName &&
                                             a.ImageType == (sdkmessageprocessingstepimage_imagetype)imagetype).FirstOrDefault();

            if (image == null)
            {
                image = new SdkMessageProcessingStepImage();
            }

            image.Name = imageName;

            image.ImageType = (sdkmessageprocessingstepimage_imagetype)imagetype;
            image.SdkMessageProcessingStepId = new EntityReference(SdkMessageProcessingStep.EntityLogicalName, step.Id);
            image.Attributes1         = attributes;
            image.EntityAlias         = imageName;
            image.MessagePropertyName = stepAttribute.Message == "Create" ? "Id" : "Target";
            if (image.Id == Guid.Empty)
            {
                _trace.WriteLine("Registering Image '{0}'", image.Name);
                image.Id = _service.Create(image);
            }
            else
            {
                _trace.WriteLine("Updating Image '{0}'", image.Name);
                _ctx.UpdateObject(image);
            }
            return(image);
        }
Esempio n. 2
0
        public void AddAttribute(CrmPluginRegistrationAttribute attribute, string className)
        {
            // Locate the start of the class and add the attribute above
            var classLocation = _pluginClasses.ContainsKey(className) ? _pluginClasses[className] : null;

            if (classLocation == null)
            {
                throw new Exception(String.Format("Cannot find class {0}", className));
            }

            var pos = _code.IndexOf(classLocation.Value);

            // Find previouse line break
            var lineBreak = _code.LastIndexOf("\r\n", pos - 1);

            // Indentation
            var indentation = _code.Substring(lineBreak, pos - lineBreak);

            // Add the attribute
            var attributeCode = attribute.GetAttributeCode(indentation);

            // Insert
            _code = _code.Insert(lineBreak, attributeCode);
        }
        public static CrmPluginRegistrationAttribute CreateFromData(this CustomAttributeData data)
        {
            CrmPluginRegistrationAttribute attribute = null;
            var arguments = data.ConstructorArguments.ToArray();

            // determine which constructor is being used by the first type
            if (data.ConstructorArguments.Count == 8 && data.ConstructorArguments[0].ArgumentType.Name == "String")
            {
                attribute = new CrmPluginRegistrationAttribute(
                    (string)arguments[0].Value,
                    (string)arguments[1].Value,
                    (StageEnum)Enum.ToObject(typeof(StageEnum), (int)arguments[2].Value),
                    (ExecutionModeEnum)Enum.ToObject(typeof(ExecutionModeEnum), (int)arguments[3].Value),
                    (string)arguments[4].Value,
                    (string)arguments[5].Value,
                    (int)arguments[6].Value,
                    (IsolationModeEnum)Enum.ToObject(typeof(IsolationModeEnum), (int)arguments[7].Value)
                    );
            }
            else if (data.ConstructorArguments.Count == 8 && data.ConstructorArguments[0].ArgumentType.Name == "MessageNameEnum")
            {
                attribute = new CrmPluginRegistrationAttribute(
                    (MessageNameEnum)Enum.ToObject(typeof(MessageNameEnum), (int)arguments[0].Value),
                    (string)arguments[1].Value,
                    (StageEnum)Enum.ToObject(typeof(StageEnum), (int)arguments[2].Value),
                    (ExecutionModeEnum)Enum.ToObject(typeof(ExecutionModeEnum), (int)arguments[3].Value),
                    (string)arguments[4].Value,
                    (string)arguments[5].Value,
                    (int)arguments[6].Value,
                    (IsolationModeEnum)Enum.ToObject(typeof(IsolationModeEnum), (int)arguments[7].Value)
                    );
            }
            else if (data.ConstructorArguments.Count == 5 && data.ConstructorArguments[0].ArgumentType.Name == "String")
            {
                attribute = new CrmPluginRegistrationAttribute(
                    (string)arguments[0].Value,
                    (string)arguments[1].Value,
                    (string)arguments[2].Value,
                    (string)arguments[3].Value,
                    (IsolationModeEnum)Enum.ToObject(typeof(IsolationModeEnum), (int)arguments[4].Value)
                    );
            }

            foreach (var namedArgument in data.NamedArguments)
            {
                switch (namedArgument.MemberName)
                {
                case "Id":
                    attribute.Id = (string)namedArgument.TypedValue.Value;
                    break;

                case "FriendlyName":
                    attribute.FriendlyName = (string)namedArgument.TypedValue.Value;
                    break;

                case "GroupName":
                    attribute.FriendlyName = (string)namedArgument.TypedValue.Value;
                    break;

                case "Image1Name":
                    attribute.Image1Name = (string)namedArgument.TypedValue.Value;
                    break;

                case "Image1Attributes":
                    attribute.Image1Attributes = (string)namedArgument.TypedValue.Value;
                    break;

                case "Image2Name":
                    attribute.Image2Name = (string)namedArgument.TypedValue.Value;
                    break;

                case "Image2Attributes":
                    attribute.Image2Attributes = (string)namedArgument.TypedValue.Value;
                    break;

                case "Image1Type":
                    attribute.Image1Type = (ImageTypeEnum)namedArgument.TypedValue.Value;
                    break;

                case "Image2Type":
                    attribute.Image2Type = (ImageTypeEnum)namedArgument.TypedValue.Value;
                    break;

                case "Description":
                    attribute.Description = (string)namedArgument.TypedValue.Value;
                    break;

                case "DeleteAsyncOperaton":
                    attribute.DeleteAsyncOperaton = (bool)namedArgument.TypedValue.Value;
                    break;

                case "UnSecureConfiguration":
                    attribute.UnSecureConfiguration = (string)namedArgument.TypedValue.Value;
                    break;

                case "SecureConfiguration":
                    attribute.SecureConfiguration = (string)namedArgument.TypedValue.Value;
                    break;

                case "Offline":
                    attribute.Offline = (bool)namedArgument.TypedValue.Value;
                    break;

                case "Server":
                    attribute.Server = (bool)namedArgument.TypedValue.Value;
                    break;

                case "Action":
                    attribute.Action = (PluginStepOperationEnum)namedArgument.TypedValue.Value;
                    break;
                }
            }
            return(attribute);
        }
        public static string GetAttributeCode(this CrmPluginRegistrationAttribute attribute, string indentation)
        {
            var code       = string.Empty;
            var targetType = (attribute.Stage != null) ? TargetType.Plugin : TargetType.WorkflowAcitivty;

            string additionalParmeters = "";

            // Image 1
            if (attribute.Image1Name != null)
            {
                additionalParmeters += indentation + ",Image1Type = ImageTypeEnum." + attribute.Image1Type;
            }
            if (attribute.Image1Name != null)
            {
                additionalParmeters += indentation + ",Image1Name = \"" + attribute.Image1Name + "\"";
            }
            if (attribute.Image1Name != null)
            {
                additionalParmeters += indentation + ",Image1Attributes = \"" + attribute.Image1Attributes + "\"";
            }

            // Image 2
            if (attribute.Image2Name != null)
            {
                additionalParmeters += indentation + ",Image2Type = ImageTypeEnum." + attribute.Image2Type;
            }
            if (attribute.Image2Name != null)
            {
                additionalParmeters += indentation + ",Image2Name = \"" + attribute.Image2Name + "\"";
            }
            if (attribute.Image2Attributes != null)
            {
                additionalParmeters += indentation + ",Image2Attributes = \"" + attribute.Image2Attributes + "\"";
            }


            if (targetType == TargetType.Plugin)
            {
                // Description is only option for plugins
                if (attribute.Description != null)
                {
                    additionalParmeters += indentation + ",Description = \"" + attribute.Description + "\"";
                }
                if (attribute.Offline)
                {
                    additionalParmeters += indentation + ",Offline = " + attribute.Offline;
                }
                if (!attribute.Server)
                {
                    additionalParmeters += indentation + ",Server = " + attribute.Server;
                }
            }
            if (attribute.Id != null)
            {
                additionalParmeters += indentation + ",Id = \"" + attribute.Id + "\"";
            }

            if (attribute.DeleteAsyncOperaton != null)
            {
                additionalParmeters += indentation + ",DeleteAsyncOperaton = " + attribute.DeleteAsyncOperaton;
            }

            if (attribute.UnSecureConfiguration != null)
            {
                additionalParmeters += indentation + ",UnSecureConfiguration = \"" + attribute.UnSecureConfiguration + "\"";
            }

            if (attribute.SecureConfiguration != null)
            {
                additionalParmeters += indentation + ",SecureConfiguration = \"" + attribute.SecureConfiguration + "\"";
            }

            if (attribute.Action != null)
            {
                additionalParmeters += indentation + ",Action = PluginStepOperationEnum." + attribute.Action.ToString();
            }

            //string tab = "    ";
            //Regex parser = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");

            // determine which template to use
            if (targetType == TargetType.Plugin)
            {
                // Plugin Step
                string template = "{9}[CrmPluginRegistration(\"{0}\", {9}\"{1}\", StageEnum.{2}, ExecutionModeEnum.{3},{9}\"{4}\",\"{5}\", {6}, {9}IsolationModeEnum.{7} {8} {9})]";

                code = String.Format(template,
                                     attribute.Message,
                                     attribute.EntityLogicalName,
                                     attribute.Stage.ToString(),
                                     attribute.ExecutionMode.ToString(),
                                     attribute.FilteringAttributes,
                                     attribute.Name,
                                     attribute.ExecutionOrder.ToString(),
                                     attribute.IsolationMode.ToString(),
                                     additionalParmeters,
                                     indentation);

                //// Plugin Step
                //string template = "\"{0}\",\"{1}\",StageEnum.{2},ExecutionModeEnum.{3},\"{4}\",\"{5}\",{6},IsolationModeEnum.{7}{8}";

                //code = String.Format(template,
                //    attribute.Message,
                //    attribute.EntityLogicalName,
                //    attribute.Stage,
                //    attribute.ExecutionMode,
                //    attribute.FilteringAttributes,
                //    attribute.Name,
                //    attribute.ExecutionOrder,
                //    attribute.IsolationMode,
                //    additionalParmeters);
            }
            else
            {
                // Workflow Step
                string template = "{6}[CrmPluginRegistration({6}\"{0}\", \"{1}\",\"{2}\",\"{3}\",IsolationModeEnum.{4}{6}{5})]";

                code = String.Format(template,
                                     attribute.Name,
                                     attribute.FriendlyName,
                                     attribute.Description,
                                     attribute.GroupName,
                                     attribute.IsolationMode.ToString(),
                                     additionalParmeters,
                                     indentation);

                //// Workflow Step
                //string template = "\"{0}\",\"{1}\",\"{2}\",\"{3}\",IsolationModeEnum.{4}{5}";

                //code = String.Format(template,
                //    attribute.Name,
                //    attribute.FriendlyName,
                //    attribute.Description,
                //    attribute.GroupName,
                //    attribute.IsolationMode,
                //    additionalParmeters);
            }

            //String[] fields = parser.Split(code);
            //code = String.Join($",{indentation}{tab}", fields);
            //string regionName = targetType == TargetType.Plugin
            //    ? $"{attribute.Message} {attribute.EntityLogicalName}"
            //    : $"{attribute.Name}";
            //code = $"{indentation}#region {attribute.Message}{regionName}{indentation}[CrmPluginRegistration({indentation}{tab}" + code + $"{indentation})]{indentation}#endregion";

            return(code);
        }
        private void RegisterStep(PluginType sdkPluginType, IEnumerable <SdkMessageProcessingStep> existingSteps, CrmPluginRegistrationAttribute pluginStep)

        {
            SdkMessageProcessingStep step = null;

            if (pluginStep.Id != null)
            {
                Guid stepId = new Guid(pluginStep.Id);
                // Get by ID
                step = existingSteps.Where(s => s.Id == stepId).FirstOrDefault();
            }

            if (step == null)
            {
                // Get by Name
                step = existingSteps.Where(s => s.Name == pluginStep.Name && s.SdkMessageId.Name == pluginStep.Message).FirstOrDefault();
            }

            // Register images
            if (step == null)
            {
                step = new SdkMessageProcessingStep();
            }
            Guid?sdkMessageId       = null;
            Guid?sdkMessagefilterId = null;

            if (pluginStep.EntityLogicalName == "none")
            {
                var message = ServiceLocator.Queries.GetMessage(_ctx, pluginStep.Message);
                sdkMessageId = message.SdkMessageId;
            }
            else
            {
                var messageFilter = ServiceLocator.Queries.GetMessageFilter(_ctx, pluginStep.EntityLogicalName, pluginStep.Message);

                if (messageFilter == null)
                {
                    _trace.WriteLine("Warning: Cannot register step {0} on Entity {1}", pluginStep.Message, pluginStep.EntityLogicalName);
                    return;
                }

                sdkMessageId       = messageFilter.SdkMessageId.Id;
                sdkMessagefilterId = messageFilter.SdkMessageFilterId;
            }

            // Update attributes
            step.Name          = pluginStep.Name;
            step.Configuration = pluginStep.UnSecureConfiguration;
            step.Description   = pluginStep.Description;
            step.Mode          = pluginStep.ExecutionMode == ExecutionModeEnum.Asynchronous ? sdkmessageprocessingstep_mode.Asynchronous : sdkmessageprocessingstep_mode.Synchronous;
            step.Rank          = pluginStep.ExecutionOrder;
            int stage = 10;

            switch (pluginStep.Stage)
            {
            case StageEnum.PreValidation:
                stage = 10;
                break;

            case StageEnum.PreOperation:
                stage = 20;
                break;

            case StageEnum.PostOperation:
                stage = 40;
                break;
            }

            step.Stage = (sdkmessageprocessingstep_stage)stage;
            int supportDeployment = 0;

            if (pluginStep.Server == true && pluginStep.Offline == true)
            {
                supportDeployment = 2; // Both
            }
            else if (!pluginStep.Server == true && pluginStep.Offline == true)
            {
                supportDeployment = 1; // Offline only
            }
            else
            {
                supportDeployment = 0; // Server Only
            }
            step.SupportedDeployment = (sdkmessageprocessingstep_supporteddeployment)supportDeployment;
            step.PluginTypeId        = sdkPluginType.ToEntityReference();
            step.SdkMessageFilterId  = sdkMessagefilterId != null ? new EntityReference(SdkMessageFilter.EntityLogicalName, sdkMessagefilterId.Value) : null;
            step.SdkMessageId        = new EntityReference(SdkMessage.EntityLogicalName, sdkMessageId.Value);
            step.FilteringAttributes = pluginStep.FilteringAttributes;
            if (step.Id == Guid.Empty)
            {
                _trace.WriteLine("Registering Step '{0}'", step.Name);
                // Create
                step.Id = _service.Create(step);
            }
            else
            {
                _trace.WriteLine("Updating Step '{0}'", step.Name);
                // Update
                _ctx.UpdateObject(step);
            }

            // Get existing Images
            SdkMessageProcessingStepImage[] existingImages = ServiceLocator.Queries.GetPluginStepImages(_ctx, step);

            var image1 = RegisterImage(pluginStep, step, existingImages, pluginStep.Image1Name, pluginStep.Image1Type, pluginStep.Image1Attributes);
            var image2 = RegisterImage(pluginStep, step, existingImages, pluginStep.Image2Name, pluginStep.Image2Type, pluginStep.Image2Attributes);

            if (SolutionUniqueName != null)
            {
                AddStepToSolution(SolutionUniqueName, step);
            }
        }