XML Web Resource to store configurations

In order to store and access configurations on Dynamics CRM different approaches may be followed:

  • Custom entities to save data settings;
  • Plugin secure or/and unsecure configurations if those settings are to be used on plugin steps;
  • XML web resource (technique shown here).

The example outlined here consists on using a XML web resource to store settings required to access the reporting server web service to render a Dynamics CRM report in pdf format.

The XML web resource is then accessed on a custom action where an email record is created and the report attached to it.

The XML web resource followed the next structure:

<?xml version="1.0" encoding="utf-8" ?>
<ReportServer>
  <Url>http://servername/ReportServer/ReportExecution2005.asmx</Url>
  <UserDomain>domain</UserDomain>
  <UserName>username</UserName>
  <UserPassword>Password</UserPassword>
</ReportServer>

After the web resource is published it may be retrieved. The next code show how to retrieve the web resource and read its content which is stored in base64 format:

String webResourceContent = service.RetrieveMultiple(
    new QueryExpression
    {
        EntityName = "webresource",
        ColumnSet = new ColumnSet("content"),
        NoLock = true,
        Criteria = new FilterExpression
        {
            Conditions =
            {
                new ConditionExpression("name", ConditionOperator.Equal, "XML_WebResourceName")
            }
        }
    }).Entities[0].GetAttributeValue<String>("content");

byte[] binary = Convert.FromBase64String(webResourceContent);
XDocument xmlDocument = XDocument.Parse(System.Text.Encoding.UTF8.GetString(binary));

String url = xmlDocument.XPathSelectElement("//Url").Value;
String userDomain = xmlDocument.XPathSelectElement("//UserDomain").Value;
String userName = xmlDocument.XPathSelectElement("//UserName").Value;
String userPass = xmlDocument.XPathSelectElement("//UserPassword").Value;

Leave a comment