Thursday, 5 October 2017

Code for secure/unsecured configuration

Unsecured Configuration
  • is viewable by any CRM user
  • will automatically move between environments with your CRM solutions.
  • Available when Plugin is Registered for Outlook Offline Mode

Secured Configuration
  • is only viewable by CRM Administrators
XML code for configuration block:
    <Settings>
          <setting name="TracingEnabled">
            <value>true</value>
          </setting>
    </Settings>

Code to extract value from XML document
     class PluginConfiguration
    {
        private static string GetValueNode(XmlDocument doc, string key)
        {
            XmlNode node = doc.SelectSingleNode(String.Format("Settings/setting[@name='{0}']", key));

            if (node != null)
            {
                return node.SelectSingleNode("value").InnerText;
            }
            return string.Empty;
        }

        public static Guid GetConfigDataGuid(XmlDocument doc, string label)
        {
            string tempString = GetValueNode(doc, label);

            if (tempString != string.Empty)
            {
                return new Guid(tempString);
            }
            return Guid.Empty;
        }

        public static bool GetConfigDataBool(XmlDocument doc, string label)
        {
            bool retVar;

            if (bool.TryParse(GetValueNode(doc, label), out retVar))
            {
                return retVar;
            }
            else
            {
                return false;
            }
        }

        public static int GetConfigDataInt(XmlDocument doc, string label)
        {
            int retVar;

            if (int.TryParse(GetValueNode(doc, label), out retVar))
            {
                return retVar;
            }
            else
            {
                return -1;
            }
        }

        public static string GetConfigDataString(XmlDocument doc, string label)
        {
            return GetValueNode(doc, label);
        }
    }

Plugin code to retrieve config data
    public TracingPluginSimple(string unsecureString, string secureString)
        {
            if (!String.IsNullOrWhiteSpace(unsecureString))
            {
                try
                {
                    _unsecureString = unsecureString;
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(_unsecureString);

                    bool TracingEnabled = PluginConfiguration.GetConfigDataBool(doc, "TracingEnabled");

                    _unsecureString = unsecureString;
                    //_secureString = secureString;
                    _tracingEnabled = TracingEnabled;
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("Unsecure or secure strings are required by the TracingPluginSimple Plug-in, but not provided.");
                }
            }
            if (!String.IsNullOrWhiteSpace(secureString))
            {
                try
                {
                    _secureString = secureString;
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(unsecureString);

                    bool TracingEnabled = PluginConfiguration.GetConfigDataBool(doc, "TracingEnabled");

                    //_unsecureString = unsecureString;
                    _secureString = secureString;
                    _tracingEnabled = TracingEnabled;
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("Unsecure or secure strings are required by the TracingPluginSimple Plug-in, but not provided.");
                }
            }
         
        public void Execute(IServiceProvider serviceProvider)
        {
            if (_tracingEnabled)
            {
                //Implement functionality
             }
      }

No comments:

Post a Comment

Have you ever faced an issue where you need to import some record without changing GUID of the record? For example if we need to migr...