Thursday, June 23, 2011

Using RetrieveMultiple From Silverlight to Retrieve Entities in Microsoft Dynamics CRM 2011

This tutorial will show you how to retrieve an entity records in Microsoft Dynamics CRM 2011 using C# in Silverlight using RetrieveMultiple.  This  particular call was put together by a colleague of mine named Stephen Walsh by starting from the other examples on my blog, his main interest is mobility so he told me I could use this code in my blog.

IMPORTANT: First things first.  You have to set up your Silverlight app to make a web services connection to CRM.   The best tutorial I have found for this is located here in the MSDN:
After you finish that setup you are ready to implement the rest of this post.

Now once that is done you can retrieve records in CRM using the following syntax.  In the following example I am retrieving a webresource record wit the name new_mywebresourcename.

Here is the call in C#:

ColumnSet Columns = new ColumnSet();
Columns.Columns = new System.Collections.ObjectModel.ObservableCollection<string>(new string[] { "content" });
QueryExpression Query = new QueryExpression();
Query.EntityName = "webresource";
Query.ColumnSet = Columns;
Query.Criteria = new FilterExpression
    FilterOperator = LogicalOperator.And,
    Conditions = 
       new ConditionExpression
           AttributeName = "name",
           Operator = ConditionOperator.Equal,
           Values = { "new_mywebresourcename" }
OrganizationRequest Request = new OrganizationRequest() { RequestName = "RetrieveMultiple" };
Request["Query"] = Query;
IOrganizationService Service = SilverlightUtility.GetSoapService();
Service.BeginExecute(Request, new AsyncCallback(GetCertificateResult), Service);

Now here is the call-back code:

private void GetCertificateResult(IAsyncResult Result)
        OrganizationResponse Response = ((IOrganizationService)Result.AsyncState).EndExecute(Result);
        EntityCollection EntityResult = (EntityCollection)Response["EntityCollection"];
        Entity wr = new Entity();
        wr = EntityResult.Entities[0];
        byte[] certbytes = Convert.FromBase64String(wr.Attributes[0].Value.ToString());
        X509Certificate MyCert = new X509Certificate(certbytes, "Certname");
    catch (Exception ex)
        throw ex;

You will need to use the this.Dispatcher.BeginInvoke to call a method to act on the UI or perform actions in the main thread.  I did not include the dispatcher invoked method called "DisplayCert" in this case because it could do anything in the UI.

You will notice that the biggest change in thinking and syntax from standard CRM SDK work will be that you have to specify your request and response properties as strings explicitly in code.

I hope this helps!



  1. hello, i like your examples, but explain me what is X509Certificate, certbytes, wr

    can you do an example with contact entity for example? please, i want understand how do retrieve from data in silverlight from crm

  2. The code above was retrieving an X509 Certificate that later was used for a secure web service call. The certificate was stored as a web resource and so I had to retrieve the web resource and reconstitute its bytes from the web resource's content attribute which is a base64 encoded string that represents the bytes of the file. In short, this represents one way to store and retrieve a file as a web resource in CRM.