Dynamics 365 Retrieve and Update using C#

Leave a comment

In this post we will look at how to retrieve a record using Retrieve Multiple, then update the record.

The example is, we are working with a customer and we need to find their record in an app using their first name, last name and email address. We then want to update their record with a new phone number.

Create a new Console App:

Add XrmTooling:

Add the code:

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Carl.Dynamics365RU
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var connectionString = @"AuthType = Office365; Url = https://yourorg.crm.dynamics.com/;Username=youremail;Password=yourpwd";
                CrmServiceClient conn = new CrmServiceClient(connectionString);

                IOrganizationService service;
                service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

                ConditionExpression condition1 = new ConditionExpression();
                condition1.AttributeName = "lastname";
                condition1.Operator = ConditionOperator.Equal;
                condition1.Values.Add("Smith");

                ConditionExpression condition2 = new ConditionExpression();
                condition2.AttributeName = "firstname";
                condition2.Operator = ConditionOperator.Equal;
                condition2.Values.Add("Bob");

                ConditionExpression condition3 = new ConditionExpression();
                condition3.AttributeName = "emailaddress1";
                condition3.Operator = ConditionOperator.Equal;
                condition3.Values.Add("test@test.com");

                FilterExpression filter1 = new FilterExpression();
                filter1.Conditions.Add(condition1);
                filter1.Conditions.Add(condition2);
                filter1.Conditions.Add(condition3);

                QueryExpression query = new QueryExpression("contact");
                query.ColumnSet.AddColumns("firstname", "lastname", "emailaddress1");
                query.Criteria.AddFilter(filter1);

                EntityCollection result = service.RetrieveMultiple(query);
                foreach (var c in result.Entities)
                {
                    Console.WriteLine("Found contact: " + c.Attributes["firstname"] + " " + c.Attributes["lastname"]);

                    // Update with new phone number
                    Entity contact = new Entity("contact");
                    contact = service.Retrieve(contact.LogicalName, c.Id, new ColumnSet(true));
                    contact["telephone1"] = "111-222-1111";
                    service.Update(contact);
                    Console.WriteLine("Updated contact");
                }
            }
            catch(Exception ex)
            {
                // Handle exception
            }
        }
    }
}

After we run this, the record will be retrieved and updated:

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

Your email address will not be published. Required fields are marked *