Reading unsecure and secure Configuration in Plugins

Sometimes, we need to send custom parameters to our plugins such as server URLs, method names, API Controllers, etc. As we all now, hardcoding code is not a good practice due to the security risks it causes. For this type of requirements, we use unsecure and secure configuration. With these two plugin attributes, we can send parameters dynamically without changing our code. To read and use them, follow steps below:

  • Open your plugin step and add the following code in Unsecure Configuration:
<Settings>
  <setting name="Parameter_1">
         <value>EXAMPLE VALUE</value>
  </setting>
</Settings>
  • Add the following code in Secure Configuration:
<Settings>
  <setting name="Parameter_2">
         <value>ANOTHER EXAMPLE VALUE</value>
  </setting>
</Settings>

 Note: You can change the parameter name and value for your desired values.

After following the steps above, the plugin configuration should look like this:

plugin config

    • Go to your plugin code and set the lines below.

Note: You can copy the entire class in case your plugin is empty or just take the lines you need

using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;

/*
* Required for the use of XmlDocument class.
*/
using System.Xml;

///
/// <summary>
/// MyPlugin Class.
/// </summary>

public class MyPlugin : IPlugin
{
// Attribute to save: Unsecure Configuration
private static XmlDocument Unsecure_Configuration;

// Attribute to save: Secure Configuration
private static XmlDocument Secure_Configuration;

///
/// <summary>
/// Constructor to get Plugin configurations
/// </summary>
/// <param name="UnsecureConfiguration"> Unsecure Configuration. Must be set in Plugin Step </param>
/// <param name="SecureConfigation"> Secure Configuration. Must be set in Plugin Step </param>
public MyPlugin(string UnsecureConfiguration, string SecureConfigation)
{
// If Unsecure Configuration missing, throw error
if (string.IsNullOrEmpty(UnsecureConfiguration))
{
throw new InvalidPluginExecutionException("Unsecure configuration was not found");
}

// If Secure Configuration missing, throw error
if (string.IsNullOrEmpty(SecureConfigation))
{
throw new InvalidPluginExecutionException("Secure configuration was not found");
}

// Load Unsecure Configuration and save it
Unsecure_Configuration = new XmlDocument();
Unsecure_Configuration.LoadXml(UnsecureConfiguration);

// Load Secure Configuration and save it
Secure_Configuration = new XmlDocument();
Secure_Configuration.LoadXml(SecureConfigation);
}

///
/// <summary>
/// MyPlugin's Main Method
/// </summary>
/// <param name="serviceProvider"> Service Provider </param>
public void Execute(IServiceProvider serviceProvider)
{
// Get: Parameter_1, from Unsecure Configuration
string Parameter_1 = Get_Data(Unsecure_Configuration, "Parameter_1");

// Get: Parameter_2, from Secure Configuration
string Parameter_2 = Get_Data(Secure_Configuration, "Parameter_2");

/*
* Your logic here
*/
}

///
/// <summary>
/// Get specific data from plugin configuration, based on a given key
/// </summary>
/// <param name="Configuration"> XML Document containing the plugin configuration </param>
/// <param name="KeyValue"> Key to be searched in plugin configuration </param>
/// <returns> Value of the key </returns>
public static string Get_Data(XmlDocument Configuration, string KeyValue)
{
// Get node in XML document, based on its Key
XmlNode Node = Configuration.SelectSingleNode(String.Format("Settings/setting[@name='{0}']", KeyValue));

// If node was found, then...
if (Node != null)
{
// Return node value
return Node.SelectSingleNode("value").InnerText;
}
else
{
// Return not found string
return "Key not found in configuration";
}
}
}
  • Add your custom logic
  • Compile the assembly
  • Upload it
  • Fire it.

Now your plugin should read unsecure and secure configuration!

Hope it helps!

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 )

w

Connecting to %s