Share via


Access entity relationships (Dynamics CRM 2013)

 

Applies To: Dynamics CRM 2013

Developer Extensions for Microsoft Dynamics CRM also provides helper methods for retrieving related entities. The standard behavior of the OrganizationServiceContext requires entity relationship members to be explicitly loaded before the related entities can be accessed as shown in this example.

var connection = CrmConnection.Parse("Url=http://crm.contoso.com/xrmContoso; ProxyTypesAssembly=Xrm;");

using (var service = new OrganizationService(connection))
using (var context = new OrganizationServiceContext(service))
{
var contact = context.CreateQuery<Contact>().First(c => c.FirstName == "Bob");

var currencyEmpty = contact.GetRelatedEntity<Entity>("transactioncurrency_contact");
var currencyEmptyStatic = contact.transactioncurrency_contact;

// currency is always null until after calling LoadProperty

Console.WriteLine(currencyEmpty == null);
Console.WriteLine(currencyEmptyStatic == null);

context.LoadProperty(contact, "transactioncurrency_contact");

// read related entity dynamically
var currency = contact.GetRelatedEntity<Entity>("transactioncurrency_contact");
Console.WriteLine(currency.GetAttributeValue<string>("currencyname"));

// read related entity statically
var currencyStatic = contact.transactioncurrency_contact;
Console.WriteLine(currencyStatic.CurrencyName);
}

For those who prefer to work with statically typed members rather than string-based relationship and attribute names, there is a set of helper methods that take relationship arguments as static expressions. The expression takes the form of a lambda expression with a single Entity parameter. Helper overloads exist for all the OrganizationServiceContext relationship methods including the following:
 LoadProperty
 GetRelatedEntity
 GetRelatedEntities
 AttachLink
 DetachLink
 AddLink
 DeleteLink
  AddRelatedObject 

Use of the LoadProperty helper method is shown in the following example.

using Microsoft.Xrm.Client;

using (var service = new OrganizationService(connection))
using (var context = new OrganizationServiceContext(service))
{
var contact = context.CreateQuery<Contact>().First(c => c.FirstName == "Bob");
context.LoadProperty(contact, c => c.transactioncurrency_contact);
var currency = contact.transactioncurrency_contact;
Console.WriteLine(currency.CurrencyName);
}

Another set of helpers calls the LoadProperty method implicitly and takes an OrganizationServiceContext reference as the first parameter. This applies to the GetRelatedEntity and GetRelatedEntities methods as shown here.

using (var service = new OrganizationService(connection))
using (var context = new OrganizationServiceContext(service))
{
// use helper method that calls LoadProperty implicitly
var contact = context.CreateQuery<Contact>().First(c => c.FirstName == "Bob");
var currency = contact.GetRelatedEntity(context, "transactioncurrency_contact");
Console.WriteLine(currency.GetAttributeValue<string>("currencyname"));
}

using (var service = new OrganizationService(connection))
using (var context = new OrganizationServiceContext(service))
{
// use helper method that calls LoadProperty implicitly and maintains static typing
var contact = context.CreateQuery<Contact>().First(c => c.FirstName == "Bob");
var currency = contact.GetRelatedEntity(context, c => c.transactioncurrency_contact);
Console.WriteLine(currency.CurrencyName);
}

By using the CrmOrganizationServiceContext, or the generated XrmServiceContext, you have the most concise code for accessing relationships. This context allows automatic lazy loading of related entities by tracking the context reference internally and calling LoadProperty implicitly, as shown here.

using (var service = new OrganizationService(connection))
using (var context = new CrmOrganizationServiceContext(service))
{
// this context manages the context reference internally
var contact = context.CreateQuery<Contact>().First(c => c.FirstName == "Bob");
var currency = contact.transactioncurrency_contact;
Console.WriteLine(currency.CurrencyName);
}

See Also

Developer extensions context object model (Dynamics CRM 2013)
Configure the context with the configuration file (Dynamics CRM 2013)
Attach entities to the context (Dynamics CRM 2013)
Context enhancements