Accessing and Manipulating Data in Dataverse with the XRM Client
Using the XRM client, you can perform CRUD (Create, Read, Update, Delete) operations and retrieve related data in Microsoft Dataverse. This guide walks you through these tasks step-by-step.
Setup
Ensure the following packages are installed:
Install-Package Microsoft.CrmSdk.XrmTooling.CoreAssembly
Install-Package Microsoft.CrmSdk.XrmTooling.CrmWebApi
Connect to Dataverse
Establish a connection to your Dataverse environment:
using Microsoft.Xrm.Tooling.Connector;
using Microsoft.Xrm.Sdk;
var connectionString = "AuthType=OAuth;Username=YOUR_USERNAME;Password=YOUR_PASSWORD;Url=https://YOUR_ORG.crm.dynamics.com;AppId=YOUR_APP_ID;RedirectUri=YOUR_REDIRECT_URI;";
var service = new CrmServiceClient(connectionString);
1. Read (Retrieve) Operation
Retrieve an entity record by its ID:
var accountId = new Guid("ACCOUNT_ID");
var columns = new ColumnSet("name", "address1_city", "emailaddress1", "revenue");
Entity account = service.Retrieve("account", accountId, columns);
Console.WriteLine($"Account Name: {account["name"]}");
Console.WriteLine($"City: {account["address1_city"]}");
Console.WriteLine($"Email: {account["emailaddress1"]}");
Console.WriteLine($"Revenue: {account["revenue"]}");
2. Insert (Create) Operation
Create a new entity record:
Entity newAccount = new Entity("account");
newAccount["name"] = "New Account";
newAccount["address1_city"] = "New York";
newAccount["emailaddress1"] = "newaccount@example.com";
newAccount["revenue"] = new Money(1000000);
Guid newAccountId = service.Create(newAccount);
Console.WriteLine($"New Account ID: {newAccountId}");
3. Update Operation
Update an existing entity record:
Entity updatedAccount = new Entity("account", new Guid("ACCOUNT_ID"));
updatedAccount["address1_city"] = "Los Angeles";
updatedAccount["emailaddress1"] = "updatedaccount@example.com";
updatedAccount["revenue"] = new Money(2000000);
service.Update(updatedAccount);
Console.WriteLine("Account updated successfully.");
4. Joining Tables (Relationships)
Retrieve related entity records using FetchXML queries:
var fetchXml = @"
<fetch>
<entity name='contact'>
<attribute name='fullname' />
<attribute name='emailaddress1' />
<link-entity name='account' from='accountid' to='parentcustomerid' alias='account'>
<filter>
<condition attribute='accountid' operator='eq' value='ACCOUNT_ID' />
</filter>
</link-entity>
</entity>
</fetch>";
EntityCollection contacts = service.RetrieveMultiple(new FetchExpression(fetchXml));
foreach (var contact in contacts.Entities)
{
Console.WriteLine($"Contact Name: {contact["fullname"]}");
Console.WriteLine($"Email: {contact["emailaddress1"]}");
}
Summary of Data Types
- String:
string
(e.g.,"name"
) - Integer:
int
(e.g.,"address1_city"
) - Money:
Money
(e.g.,"revenue"
) - DateTime:
DateTime
(e.g.,"birthdate"
) - Lookup:
EntityReference
(e.g.,"parentcustomerid"
)
Complete Example
Here’s a full example to demonstrate CRUD operations and retrieving related records:
using Microsoft.Xrm.Tooling.Connector;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
class Program
{
static void Main()
{
var connectionString = "AuthType=OAuth;Username=YOUR_USERNAME;Password=YOUR_PASSWORD;Url=https://YOUR_ORG.crm.dynamics.com;AppId=YOUR_APP_ID;RedirectUri=YOUR_REDIRECT_URI;";
var service = new CrmServiceClient(connectionString);
// Read
var accountId = new Guid("ACCOUNT_ID");
var columns = new ColumnSet("name", "address1_city", "emailaddress1", "revenue");
Entity account = service.Retrieve("account", accountId, columns);
Console.WriteLine($"Account Name: {account["name"]}");
Console.WriteLine($"City: {account["address1_city"]}");
Console.WriteLine($"Email: {account["emailaddress1"]}");
Console.WriteLine($"Revenue: {account["revenue"]}");
// Insert
Entity newAccount = new Entity("account");
newAccount["name"] = "New Account";
newAccount["address1_city"] = "New York";
newAccount["emailaddress1"] = "newaccount@example.com";
newAccount["revenue"] = new Money(1000000);
Guid newAccountId = service.Create(newAccount);
Console.WriteLine($"New Account ID: {newAccountId}");
// Update
Entity updatedAccount = new Entity("account", newAccountId);
updatedAccount["address1_city"] = "Los Angeles";
updatedAccount["emailaddress1"] = "updatedaccount@example.com";
updatedAccount["revenue"] = new Money(2000000);
service.Update(updatedAccount);
Console.WriteLine("Account updated successfully.");
// Join
var fetchXml = @"
<fetch>
<entity name='contact'>
<attribute name='fullname' />
<attribute name='emailaddress1' />
<link-entity name='account' from='accountid' to='parentcustomerid' alias='account'>
<filter>
<condition attribute='accountid' operator='eq' value='ACCOUNT_ID' />
</filter>
</link-entity>
</entity>
</fetch>";
EntityCollection contacts = service.RetrieveMultiple(new FetchExpression(fetchXml));
foreach (var contact in contacts.Entities)
{
Console.WriteLine($"Contact Name: {contact["fullname"]}");
Console.WriteLine($"Email: {contact["emailaddress1"]}");
}
}
}
Conclusion
This guide provides a comprehensive approach to performing CRUD operations and retrieving related records in Dataverse using the XRM client. With these examples, you’ll be equipped to interact with Dataverse seamlessly in your ASP.NET Core applications.