Oct 8, 2019

Passing Unsecure Configurations to a Plugin

We discussed how to pass Configurations to a JavaScript in a previous post. Click this to read that.

Lets see how we can pass Configurations to a Plug-in. In this scenarios, we'll see how we pass some Key Value pairs to a plug-in written for Lead entity.. We are going to store these values in a XML format within Unsecure Configuration section in registered plugin step.


This is the XML data format.

<leadConfig>
  <setting name="RegionCode" value="000X23AA55" />
  <setting name="IntegrationKey" value="1200-6753-0980-0901" />
</leadConfig>

Here is the Plug-in code that reads above values to be used in whatever the logic within the Plug-in. Interestingly, you will notice how we use a constructor class where configurations are being read within. Then we use GetUnsecureConfigValue() method to read each value in the XML.

using System;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using System.Xml;

namespace TrialPlugin
{
    public class LeadPreCreate : IPlugin
    {
        private string UnsecureConfig { get; set; }

        public LeadPreCreate(string unsecureConfig, string secureConfig)
        {
            if (string.IsNullOrEmpty(unsecureConfig))
                throw new InvalidPluginExecutionException("Plugin Configuration missing.");
            UnsecureConfig = unsecureConfig;
        }

        public void Execute(IServiceProvider serviceProvider)
        {
            try
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity TargetEnt = (Entity)context.InputParameters["Target"];
                    if (TargetEnt.LogicalName != "lead")
                        return;

                    IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = factory.CreateOrganizationService(context.UserId);

                    string regionCode = GetUnsecureConfigValue("RegionCode");
                    //string regionCode = Regex.Replace(GetUnsecureConfigValue("RegionCode"), @"\t|\n|\r", "").Replace(" ", String.Empty);
                    string integrationKey = GetUnsecureConfigValue("IntegrationKey");
                    //string regionCode = Regex.Replace(GetUnsecureConfigValue("IntegrationKey"), @"\t|\n|\r", "").Replace(" ", String.Empty);

                    throw new InvalidPluginExecutionException("RegionCode: " + regionCode + ", IntegrationKey: " + integrationKey);
                }
            }
            catch (FaultException<OrganizationServiceFault> e)
            {
                throw e;
            }
        }
        
        private string GetUnsecureConfigValue(string key)
        {
            const string XPATH = "leadConfig/setting[@name='{0}']";
            string configVal = string.Empty;
            try
            {
                var xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(UnsecureConfig);
                var node = xmlDoc.SelectSingleNode(string.Format(XPATH, key));
                return node == null ? configVal : node.Attributes["value"].Value;
            }
            catch
            {
                return configVal;
            }
        }
    }
}

Caution
If you have lengthy values in XML, there is a chance that line-breaks and spaces being added without your intention. In such cases, you may use some Regular Expression functions to omit those. For above code, I have read the configuration data in two ways and commented out one. If explained issue likely to be hitting your situation, you may use the commented line instead of what is used.

Anyway, this method of passing configuration data is can be used to keep parameters differently for different environments. What is important to know is solution imports are overriding these vales.

No comments:

Post a Comment