Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

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

Jamie Miley Profile Picture Jamie Miley 2,060
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:
http://msdn.microsoft.com/en-us/library/gg594452.aspx
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)
{
    try
    {
        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");
        this.Dispatcher.BeginInvoke(DisplayCert);
    }
    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!

-



This was originally posted here.

Comments

*This post is locked for comments