Search code examples
c#dynamics-crmfetchxml

Accessing data in `RetrieveMultiple`


I'm fetching Contactand Account entities and I wish to access the name of the contact and the name of the primary contact. While the first one is accessed very easily by

EntityCollection result = proxy.RetrieveMultiple(...);
Entity entity = result.Entities[0].Attributes["fullname"];

the other seems to fight me throwing an exception. As far I can see, it depends on the fetch XML that has attributes for the name directly in <entity> in the former case and under <entity><link-entity> in the latter.

How can I access the field fullname that is a linked entity?


Fetch XML for contacts:

<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
  <entity name='contact'>
    <attribute name='fullname' />
  </entity>
</fetch>

Fetch XML for accounts' primary contacts:

<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
  <entity name='account'>
    <attribute name='accountid'/>
    <link-entity name='listmember' from='entityid' to='accountid' visible='false' intersect='true'>
      <link-entity name='list' from='listid' to='listid' alias='ab'>
      </link-entity>
    </link-entity>
    <link-entity name='contact' from='contactid' to='primarycontactid' visible='false' link-type='outer' alias='...'>
      <attribute name='fullname'/>
    </link-entity>
  </entity>
</fetch>

Solution

  • This is a response to the original question, the answer from GCTANM and comments (my thoughts are too long for the comments).

    There appears to be a couple of confusions here I would like to clear up.

    GCTANM:

    RetrieveMultiple can only use LinkEntities for selection purposes, it cannot return attributes for entities other than the primary entity of the query.

    ...

    for any other attributes of those contact records, you'd have to take each contact's ID and do one of those Retrieve calls mentioned in my comment.

    These statements are not correct, you can return attributes of all the linked entities as well. There is an example of this on the MSDN.

    Chamster

    By the sound of its name, Fetch will only return a single result, won't it?

    FetchXml returns a single result set, but that result set can contain the details of many records.

    Somehow, I'd prefer to work with XML than entities

    If you really (really really) wanted to do that you could use the Crm 4 Web Services, the RetrieveMultiple still returns an Xml result set.

    To answer the original question.

    So the reason your probably struggling to get back your attribute it because the EntityCollection that is returned has a slightly peculiar behaviour which was introduced in Crm 2011.

    In short, Crm 4 FetchXml queries would return an Xml result you had to parse, in 2011 to help you out Microsoft parse the result into an EntityCollection. This results in some attributes having quite unexpected names.

    So taking the example of you contact above, you would have to access the fullname attribute using something like this: contact1.fullname, the contact is the link, the number is representative of the number of links, and fullname is the attribute.

    I can't remember the exact format and I'm struggling to find a decent example. I would suggest setting a breakpoint after your query and inspecting the results, I'm certain you will find the data but with names like above.

    If I can find a decent example (or create one tommorrow) I'll update my post.