Home › Dynamics 365 Customer Engagement (CRM) › Dynamics 365 Retrieve and Update using C#
Dynamics 365 Customer Engagement (CRM) · Aug 20, 2017 · 2 MIN READ

Dynamics 365 Retrieve and Update using C#

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:

[sourcecode language=”CSharp”] 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
}
}
}
}
[/sourcecode]

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

 

Carl de Souza
Keep learning. Keep building.

Explore AI, agents & Microsoft technology.

I share practical ideas, tutorials, and videos about AI, AI agents, Microsoft technologies, and the Power Platform.

Subscribe on YouTube →
Carl de Souza Enterprise Architect at Microsoft · AI Technology Expert

2 Responses to Dynamics 365 Retrieve and Update using C#

  1. Hi,
    Thanks for your very useful article, I’ve tried to customize it to get an entity and update an attribute but it returns an error “object reference not set to an instance of an object” at EntityCollection result = service.RetrieveMultiple(query)

    The change I have is just renaming the entity from contact to incident (Case).

    Would you please offer any idea?

Leave a Reply

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

Carl de Souza
KEEP LEARNING. KEEP BUILDING.

Explore AI, agents & Microsoft technology.

I share practical ideas, tutorials, and videos about AI, AI agents, Microsoft technologies, and the Power Platform.