protected void Execute(LocalPluginContext Context)
        {
            Trace("Get Target record");
            Entity Target = Context.GetInputParameters<CreateInputParameters>().Target;
            string pluginName = String.Format(PLUGIN_NAME, Target.GetAttributeValue<string>("cel_entityname"));

            if (Target.GetAttributeValue<OptionSetValue>("cel_triggerevent").Value == 1)
            {
                pluginName += " Update";
            }

            Trace("Check for existing plugin step");
            if (Context.OrganizationDataContext.CreateQuery("sdkmessageprocessingstep").Where(s => s.GetAttributeValue<string>("name").Equals(pluginName)).ToList().Any())
            {
                return;  // Step already exists, nothing to do here.
            }

            Trace("Build the configuration");
            AutoNumberPluginConfig config = new AutoNumberPluginConfig()
            {
                EntityName = Target.GetAttributeValue<string>("cel_entityname"),
                EventName = Target.GetAttributeValue<OptionSetValue>("cel_triggerevent").Value == 1 ? "Update" : "Create"
            };

            Trace("Get the Id of this plugin");
            Guid PluginTypeId = Context.OrganizationDataContext.CreateQuery("plugintype")
                 											   .Where(s => s.GetAttributeValue<string>("name").Equals("Celedon.GetNextAutoNumber"))
                                                               .Select(s => s.GetAttributeValue<Guid>("plugintypeid"))
                                                               .First();

            Trace("Get the message id from this org");
            Guid messageId = Context.OrganizationDataContext.CreateQuery("sdkmessage")
                                                            .Where(s => s.GetAttributeValue<string>("name").Equals(config.EventName))
                                                            .Select(s => s.GetAttributeValue<Guid>("sdkmessageid"))
                                                            .First();

            Trace("Get the filterId for for the specific entity from this org");
            Guid filterId = Context.OrganizationDataContext.CreateQuery("sdkmessagefilter")
                                                           .Where(s => s.GetAttributeValue<string>("primaryobjecttypecode").Equals(config.EntityName)
                                                               && s.GetAttributeValue<EntityReference>("sdkmessageid").Id.Equals(messageId))
                                                           .Select(s => s.GetAttributeValue<Guid>("sdkmessagefilterid"))
                                                           .First();

            Trace("Build new plugin step");
            Entity newPluginStep = new Entity("sdkmessageprocessingstep")
            {
                Attributes = new AttributeCollection()
                {
                    { "name", pluginName },
                    { "description", pluginName },
                    { "plugintypeid", PluginTypeId.ToEntityReference("plugintype") },  // This plugin type
                    { "sdkmessageid", messageId.ToEntityReference("sdkmessage") },  // Create or Update Message
                    { "configuration", config.ToJSON() },  // EntityName and RegisteredEvent in the UnsecureConfig
                    { "stage", PREOPERATION.ToOptionSetValue() },  // Execution Stage: Pre-Operation
                    { "rank", 1 },
                    { "impersonatinguserid", Context.PluginExecutionContext.UserId.ToEntityReference("systemuser") },  // Run as SYSTEM user. Assumes we are currently running as the SYSTEM user
                    { "sdkmessagefilterid", filterId.ToEntityReference("sdkmessagefilter") },
                }
            };

            Trace("Create new plugin step");
            Guid pluginStepId = Context.OrganizationService.Create(newPluginStep);
        }
Ejemplo n.º 2
0
        protected void Execute(LocalPluginContext context)
        {
            context.Trace("Get Target record");
            var target     = context.GetInputParameters <CreateInputParameters>().Target;
            var pluginName = string.Format(PluginName, target.GetAttributeValue <string>("cel_entityname"));

            if (target.GetAttributeValue <OptionSetValue>("cel_triggerevent").Value == 1)
            {
                pluginName += " Update";
            }

            context.Trace("Check for existing plugin step");
            if (context.OrganizationDataContext.CreateQuery("sdkmessageprocessingstep").Where(s => s.GetAttributeValue <string>("name").Equals(pluginName)).ToList().Any())
            {
                return;                  // Step already exists, nothing to do here.
            }

            context.Trace("Build the configuration");
            var config = new AutoNumberPluginConfig()
            {
                EntityName = target.GetAttributeValue <string>("cel_entityname"),
                EventName  = target.GetAttributeValue <OptionSetValue>("cel_triggerevent").Value == 1 ? "Update" : "Create"
            };

            context.Trace("Get the Id of this plugin");
            var pluginTypeId = context.OrganizationDataContext.CreateQuery("plugintype")
                               .Where(s => s.GetAttributeValue <string>("name").Equals(typeof(GetNextAutoNumber).FullName))
                               .Select(s => s.GetAttributeValue <Guid>("plugintypeid"))
                               .First();

            context.Trace("Get the message id from this org");
            var messageId = context.OrganizationDataContext.CreateQuery("sdkmessage")
                            .Where(s => s.GetAttributeValue <string>("name").Equals(config.EventName))
                            .Select(s => s.GetAttributeValue <Guid>("sdkmessageid"))
                            .First();

            context.Trace("Get the filterId for for the specific entity from this org");
            var filterId = context.OrganizationDataContext.CreateQuery("sdkmessagefilter")
                           .Where(s => s.GetAttributeValue <string>("primaryobjecttypecode").Equals(config.EntityName) &&
                                  s.GetAttributeValue <EntityReference>("sdkmessageid").Id.Equals(messageId))
                           .Select(s => s.GetAttributeValue <Guid>("sdkmessagefilterid"))
                           .First();

            context.Trace("Build new plugin step");
            var newPluginStep = new Entity("sdkmessageprocessingstep")
            {
                Attributes = new AttributeCollection()
                {
                    { "name", pluginName },
                    { "description", pluginName },
                    { "plugintypeid", pluginTypeId.ToEntityReference("plugintype") },                                 // This plugin type
                    { "sdkmessageid", messageId.ToEntityReference("sdkmessage") },                                    // Create or Update Message
                    { "configuration", config.ToJson() },                                                             // EntityName and RegisteredEvent in the UnsecureConfig
                    { "stage", PipelineStage.PreOperation.ToOptionSetValue() },                                       // Execution Stage: Pre-Operation
                    { "rank", 1 },
                    { "impersonatinguserid", context.PluginExecutionContext.UserId.ToEntityReference("systemuser") }, // Run as SYSTEM user. Assumes we are currently running as the SYSTEM user
                    { "sdkmessagefilterid", filterId.ToEntityReference("sdkmessagefilter") },
                }
            };

            context.Trace("Create new plugin step");
            var sdkmessageprocessingstepid = context.OrganizationService.Create(newPluginStep);

            // only add the image if the type is update, on create a value cannot be overridden
            if (target.GetAttributeValue <OptionSetValue>("cel_triggerevent").Value == 1)
            {
                context.Trace("Build new plugin step image");
                var newPluginStepImage = new Entity("sdkmessageprocessingstepimage")
                {
                    Attributes = new AttributeCollection()
                    {
                        { "sdkmessageprocessingstepid", sdkmessageprocessingstepid.ToEntityReference("sdkmessageprocessingstep") },
                        { "imagetype", 0.ToOptionSetValue() },       // PreImage
                        { "messagepropertyname", "Target" },
                        { "name", "Image" },
                        { "entityalias", "Image" },
                        { "attributes", target.GetAttributeValue <string>("cel_attributename") },      //Only incluce the one attribute we really need.
                    }
                };

                context.Trace("Create new plugin step image");
                context.OrganizationService.Create(newPluginStepImage);
            }
        }
        protected void Execute(LocalPluginContext Context)
        {
            Trace("Get Target record");
            Entity Target     = Context.GetInputParameters <CreateInputParameters>().Target;
            string pluginName = String.Format(PLUGIN_NAME, Target.GetAttributeValue <string>("cel_entityname"));

            if (Target.GetAttributeValue <OptionSetValue>("cel_triggerevent").Value == 1)
            {
                pluginName += " Update";
            }

            Trace("Check for existing plugin step");
            if (Context.OrganizationDataContext.CreateQuery("sdkmessageprocessingstep").Where(s => s.GetAttributeValue <string>("name").Equals(pluginName)).ToList().Any())
            {
                return;                  // Step already exists, nothing to do here.
            }

            Trace("Build the configuration");
            AutoNumberPluginConfig config = new AutoNumberPluginConfig()
            {
                EntityName = Target.GetAttributeValue <string>("cel_entityname"),
                EventName  = Target.GetAttributeValue <OptionSetValue>("cel_triggerevent").Value == 1 ? "Update" : "Create"
            };

            Trace("Get the Id of this plugin");
            Guid PluginTypeId = Context.OrganizationDataContext.CreateQuery("plugintype")
                                .Where(s => s.GetAttributeValue <string>("name").Equals("Celedon.GetNextAutoNumber"))
                                .Select(s => s.GetAttributeValue <Guid>("plugintypeid"))
                                .First();

            Trace("Get the message id from this org");
            Guid messageId = Context.OrganizationDataContext.CreateQuery("sdkmessage")
                             .Where(s => s.GetAttributeValue <string>("name").Equals(config.EventName))
                             .Select(s => s.GetAttributeValue <Guid>("sdkmessageid"))
                             .First();

            Trace("Get the filterId for for the specific entity from this org");
            Guid filterId = Context.OrganizationDataContext.CreateQuery("sdkmessagefilter")
                            .Where(s => s.GetAttributeValue <string>("primaryobjecttypecode").Equals(config.EntityName) &&
                                   s.GetAttributeValue <EntityReference>("sdkmessageid").Id.Equals(messageId))
                            .Select(s => s.GetAttributeValue <Guid>("sdkmessagefilterid"))
                            .First();

            Trace("Build new plugin step");
            Entity newPluginStep = new Entity("sdkmessageprocessingstep")
            {
                Attributes = new AttributeCollection()
                {
                    { "name", pluginName },
                    { "description", pluginName },
                    { "plugintypeid", PluginTypeId.ToEntityReference("plugintype") },                                 // This plugin type
                    { "sdkmessageid", messageId.ToEntityReference("sdkmessage") },                                    // Create or Update Message
                    { "configuration", config.ToJSON() },                                                             // EntityName and RegisteredEvent in the UnsecureConfig
                    { "stage", PREOPERATION.ToOptionSetValue() },                                                     // Execution Stage: Pre-Operation
                    { "rank", 1 },
                    { "impersonatinguserid", Context.PluginExecutionContext.UserId.ToEntityReference("systemuser") }, // Run as SYSTEM user. Assumes we are currently running as the SYSTEM user
                    { "sdkmessagefilterid", filterId.ToEntityReference("sdkmessagefilter") },
                }
            };

            Trace("Create new plugin step");
            Guid pluginStepId = Context.OrganizationService.Create(newPluginStep);
        }