How To : Retrieve Web Resource Contents in CRM using C# SDK

Hi, in this article i am gonna show how to retrieve the content of an "HTML Web Resource" in Microsoft Dynamics CRM using C# SDK

You need to perform 2 steps 

1. perform a "RetrieveMultiple" call to pull back your web resource entity.
2. Decode the "content" property from base-64 to a string.

code :

RetrieveMultipleRequest RetrieveWebResources= new RetrieveMultipleRequest();
RetrieveMultipleResponse resp = new RetrieveMultipleResponse();
Entity webresource = new Entity();

QueryExpression query = new QueryExpression()
{
EntityName = "webresource",
ColumnSet = new ColumnSet("content"),
Criteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = "name",
Operator = ConditionOperator.Equal,
Values = { "web resource name"}
}
}
}
};

RetrieveWebResources.Query = query;
resp = (RetrieveMultipleResponse)_serviceproxy.Execute(RetrieveWebResources);
webresource= resp.EntityCollection.Entities[0];

byte[] b = Convert.FromBase64String(webresource["content"].ToString());

string strHTML = System.Text.Encoding.UTF8.GetString(b);