Silverlight 5 Async Tasks with Microsoft Dynamics CRM 2011

Posted by on September 10, 2012  |  commentsComments (3)

Have you ever needed to execute several queries in CRM 2011 from Silverlight and perform some task when all the data is retrieved?  If so, you’ll know that this can be a major pain as well as create some ugly code due to asynchronous requests.

With Silverlight 5 you can use the System.Threading.Tasks namespace to simplify your code.  In the following code sample I will use two simple queries, one to retrieve all contacts and one to retrieve all accounts.  I will then utilize Tasks to execute the queries asynchronously and wait until they are both finished in order to perform more logic.  The following code sample uses the SilverlightUtility from the SDK to use the SOAP endpoint which can be found here.

        public void RetrieveData()
        {
            IOrganizationService orgService = SilverlightUtility.GetSoapService();

            QueryExpression accountQuery = new QueryExpression();
            accountQuery.EntityName = "account";
            accountQuery.ColumnSet = new ColumnSet() { AllColumns = true };            

            QueryExpression contactQuery = new QueryExpression();
            contactQuery.EntityName = "contact";
            contactQuery.ColumnSet = new ColumnSet() { AllColumns = true };

            var accountTask = CreateRetrieveMultipleTask(orgService, accountQuery);
            var contactTask = CreateRetrieveMultipleTask(orgService, contactQuery);

            Task.Factory.ContinueWhenAll(new Task[] { accountTask, contactTask },
                completedTasks =>
                {
                    EntityCollection accounts = accountTask.Result;
                    EntityCollection contacts = contactTask.Result;

                    // Perform task
                });
        }

        public Task<EntityCollection> CreateRetrieveMultipleTask(IOrganizationService orgService,                         
            QueryBase query)
        {
            return 
                Task.Factory.FromAsync<QueryBase, EntityCollection>(
                    orgService.BeginRetrieveMultiple,
                    orgService.EndRetrieveMultiple,
                    query,
                    null);
        }
 

If you are using Visual Studio 2012 then you can make asynchronous processing even cleaner with the Async Targeting Pack and the 'await' keyword.  More information can be found here.

Comments

  1. For .Net development if you need to develop applications with CRM instead of SQL the most obvious advantage you will get from Microsoft Dynamic CRM is the number of in-the-box entities and applications that are instantly usable.

    Posted by: Madison Miller  |  Sep 12, 2012 2:12:41 AM

  2. Have you ever needed to execute several queries in CRM 2011 from Silverlight and perform some task when all the data is retrieved? If so, you’ll know that this can be a major pain as well as create some ugly code due to asynchronous requests.

    Posted by: Regan Layman  |  Sep 24, 2012 6:05:40 AM

  3. Have you ever needed to execute several queries in CRM 2011 from Silverlight and perform some task when all the data is retrieved? If so, you’ll know that this can be a major pain as well as create some ugly code due to asynchronous requests.

    Posted by: Elida High  |  Sep 24, 2012 6:06:41 AM

Post a Comment

  • *Required

Contact Us for a Quote, or Personalized Demonstrationof Microsoft Dynamics CRM for Your Business.

Contact Us