Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I was having exactly the same issue as you. I saw those properties and was perplexed as to why they always returned <code>null</code>. They clearly were <em>intended</em> to retrieve entities for a particular relationship, yet they seemed to be "dormant."</p> <p>It occurred to me that maybe the context object was not configured properly to lazy-load these "navigation" properties (to borrow a term from Entity Framework). So I started researching what I could about <a href="http://msdn.microsoft.com/en-us/library/gg308389.aspx" rel="noreferrer" title="MSDN - OrganizationServiceContext Class"><code>OrganizationServiceContext</code></a>, and found this bit about its <a href="http://msdn.microsoft.com/en-us/library/gg307150.aspx" rel="noreferrer" title="MSDN - OrganizationServiceContext.LoadProperty Method"><code>LoadProperty</code></a> method:</p> <blockquote> <p>If the property represents an association, link or deferred property, calling this method provides the client a way to lazily load related resources.</p> </blockquote> <p>That sounded like what I needed, and one of the overloads takes an <code>Entity</code> and a <code>Relationship</code> as input. So, once you have an instance of an entity with one or more relationships, you need to ask the context to load the corresponding properties. Keep in mind, the entity must be attached to the context, either automatically (because you retrieved the entity via a <a href="http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.client.organizationservicecontext.createquery.aspx" rel="noreferrer" title="MSDN - OrganizationServiceContext.CreateQuery Method">context query</a>), or manually using the <a href="http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.client.organizationservicecontext.attach.aspx" rel="noreferrer" title="MSDN - OrganizationServiceContext.Attach Method"><code>Attach</code></a> method.</p> <p>I'm a little confused by your code because you're using a <code>CrmDataContext</code> object rather than an <code>OrganizationServiceContext</code> object. The code samples in the 2011 SDK use the latter, and the crmsvcutil will even generate a custom instance of <code>OrganizationServiceContext</code> with methods in the form of "[ENTITY_NAME]Set" (i.e. <code>AccountSet</code> as in your example). You may need to switch over to the newer context type.</p> <p>So, using your example and assuming <code>dc</code> is now an instance of <code>OrganizationServiceContext</code>, it would look like:</p> <pre><code>Relationship contactRel = new Relationship("contact_customer_accounts"); foreach (var a in dc.AccountSet) { dc.LoadProperty(a, contactRel); // Tell context to load entities from this relationship foreach (var c in a.contact_customer_accounts) { c.FullName.ToString(); } } </code></pre> <p>It is a pain to have to manually load each relationship, but I can find no other way of activating those properties.</p> <hr> <p><strong>Note:</strong> To get crmsvcutil to generate a custom <code>OrganizationServiceContext</code>, specify the <strong>serviceContextName</strong> switch:</p> <pre><code>crmsvcutil.exe /url:&lt;your_crm_url&gt; /out:Xrm.cs /serviceContextName:XrmServiceContext </code></pre> <p>This would create a derived class named <code>XrmServiceContext</code> with accessors for all of the different entity types in your organization.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload