Performing CRUD Operations and Joining Tables with IOrganizationService

When working with Microsoft Dataverse, IOrganizationService is a powerful API that enables direct interaction with the Dataverse environment. This guide demonstrates how to perform CRUD operations and retrieve related records using the OrganizationService in an ASP.NET Core application.


Setup

Before starting, ensure the required packages are installed:

Install-Package Microsoft.CrmSdk.CoreAssemblies
Install-Package Microsoft.CrmSdk.XrmTooling.CoreAssembly

Connect to Dataverse

Establish a connection to your Dataverse environment using a connection string:

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;

var serviceProxy = new OrganizationServiceProxy(new Uri("https://YOUR_ORG.crm.dynamics.com/XRMServices/2011/Organization.svc"), null, null, null)
{
    CallerId = Guid.Parse("YOUR_CALLER_ID")
};
var service = (IOrganizationService)serviceProxy;

1. Read (Retrieve) Operation

Retrieve an entity record by its ID:

using Microsoft.Xrm.Sdk.Query;

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"] = "[email protected]";
newAccount["revenue"] = new Money(1000000);

Guid newAccountId = service.Create(newAccount);
Console.WriteLine($"New Account ID: {newAccountId}");

3. Update Operation

Update an existing record:

Entity updatedAccount = new Entity("account", new Guid("ACCOUNT_ID"));
updatedAccount["address1_city"] = "Los Angeles";
updatedAccount["emailaddress1"] = "[email protected]";
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>";

var fetchExpression = new FetchExpression(fetchXml);
EntityCollection contacts = service.RetrieveMultiple(fetchExpression);
foreach (var contact in contacts.Entities)
{
    Console.WriteLine($"Contact Name: {contact["fullname"]}");
    Console.WriteLine($"Email: {contact["emailaddress1"]}");
}

Summary of Datatypes

  • 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")

Conclusion

By leveraging IOrganizationService, you can perform CRUD operations and handle relationships in Microsoft Dataverse efficiently. This makes it a versatile tool for developing robust ASP.NET Core applications.

Related Posts