Sometimes, we would like to read data of configuration file which has been added as web resource of type Data (Xml) in Dynamic CRM.
In this example I’ve used a web resource named as “TestData.xml” which contains Data in xml format as shown below (Organization name and Entity name). I would like to explain how to read Organization name from this web resource, a Data (Xml) file.
Add the following class files in your class library project from this location “..\sdk\samplecode\cs\helpercode” once you have successfully downloaded Dynamics CRM SDK 5.0.9 (http://www.microsoft.com/download/en/details.aspx?id=24004).
· crmservicehelpers.cs
· myorganizationcrmsdktypes.cs
· optionsets.cs
Below is the code to retrieve a web resource record.
Once you retrieve a data xml record in above “webResource” variable then convert the content attribute in byte array. By using “UnicodeEncoding” class we will get an encoded string which can be further used with XDocument class.
I hope this will be helpful.
In this example I’ve used a web resource named as “TestData.xml” which contains Data in xml format as shown below (Organization name and Entity name). I would like to explain how to read Organization name from this web resource, a Data (Xml) file.
Add the following class files in your class library project from this location “..\sdk\samplecode\cs\helpercode” once you have successfully downloaded Dynamics CRM SDK 5.0.9 (http://www.microsoft.com/download/en/details.aspx?id=24004).
· crmservicehelpers.cs
· myorganizationcrmsdktypes.cs
· optionsets.cs
Below is the code to retrieve a web resource record.
using System.Xml.Linq;
using Microsoft.Xrm.Sdk.Query;
//create request to retrieve Webresource
QueryByAttribute requestWebResource = new QueryByAttribute
{
EntityName = WebResource.EntityLogicalName,
ColumnSet = new ColumnSet(true),
};
requestWebResource.Attributes.AddRange(“name”);
requestWebResource.Values.AddRange(“acm_/XML/TestData.xml”);
WebResource webResource = null;
EntityCollection webResourceCollection = organizationService.RetrieveMultiple(requestWebResource);
if (webResourceCollection.Entities.Count == 0)
throw new InvalidPluginExecutionException("Specified Webresource does not exist");
webResource = (WebResource)webResourceCollection.Entities[0];
byte[] binary = Convert.FromBase64String(webResource.Attributes[“content”].ToString());
string resourceContent = UnicodeEncoding.UTF8.GetString(binary);
Once you retrieve a data xml record in above “webResource” variable then convert the content attribute in byte array. By using “UnicodeEncoding” class we will get an encoded string which can be further used with XDocument class.
//Load xml document
XDocument xDoc = XDocument.Parse(resourceContent);
string orgName= string.Empty;
XElement orgNameElement = xDoc.XPathSelectElement("//OrgName");
if (orgNameElement != null)
orgName = orgNameElement.Value;
Hi Ankit,
ReplyDeletethanks for your helpful post. In my case I had to make a small modification since I kept receiving an exception:
"Data at the root level is invalid. line 1 position 1."
I found out that this was caused by the UTF8 preamble which takes the first 3 bytes of the binary-array. So I modified the line which reads the binary-array into the resourceContent-string.
My line is now:
string resourceContent = UnicodeEncoding.UTF8.GetString(binary, 3, binary.Length - 3);
instead of:
string resourceContent = UnicodeEncoding.UTF8.GetString(binary);
Hope this helps
Todor
Hey,
ReplyDeleteis there an update on the helpercode files which i have to add to my project? Is it really necessary to inculde all of them?
Not all of them, only the files that were specified as bulleted points on above post.
ReplyDeleteHey, good post, thank you. It really helped me.
ReplyDeleteCan you explain me why I should include helper files from SDK? It works perfectly without them as long as you remember to include System.Xml.XPath.
I also did have same problem than Todor Danskalov but as he pointed out there's a solution for it.
Anyway, good work!
Hello again. Now all of a sudden the problem Todor Daskalov and me had is gone. I spent a decent amount of time for figuring out why my code was not working anymore. Then I realized that the problem was this "UnicodeEncoding.UTF8.GetString(binary, 3, binary.Length - 3);". So I changed it back to the original line and it seems to be working again.
ReplyDeleteAmazing article. It saved me today :)
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThanks your post helped me.
ReplyDeletehi all,
ReplyDeleteI have added all the entities to the drop down list.i would like to get the entity name and based on the entity name i have generate button on the form dynamically on that entity form.any body please help me out............................