Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching (strongly typed) related entities using FetchXML

I'm using fetchXML to join two entities and can only get the results for the first list. I'm probably making a silly mistake, but this has me mucking around for hours.

var query = @"
  <fetch version='1.0' mapping='logical' distinct='true'>
  <entity name='contact'>
    <all-attributes />
    <link-entity name='new_custom' from='new_contactid' to='contactid' link-type='inner'>
      <all-attributes />
    </link-entity>
  </entity>
</fetch>
";

var fe = new FetchExpression(query);
var result = service.RetrieveMultiple(fe);

// This contact entity is properly filled
var contact = result.Entities.First().Cast<Contact>();

// This relationship is null, that's unexpected!
var custom = contact.new_contact_new_custom;

// Also null (not sure if this should work)
var related = contact.GetRelatedEntity<new_custom>("new_contact_new_custom", null);

I do see that the correct data was retrieved in the Attributes and FormattedValues properties on the Contact entity. But why isn't the relationship set up correctly? And how can I cast this data to the correct "custom" entity type?

like image 775
Robert Massa Avatar asked Feb 14 '23 14:02

Robert Massa


1 Answers

First: are you sure your query returns early-bound objects? In my example below I assume you get Entity objects (late-bound), but for this example it would not really make much of a difference.

The query's response is basically a plain table of entity objects of the type you requested, in this case "contact". Fields of joined entities can be added to this table too, but they are wrapped in AliasedValue objects. Their column names are prefixed by a LinkEntity alias in order to prevent name collisions.

Your line var contact = result.Entities.Cast<Contact>(); suggests that variable contact is of type Contact, but it is in fact an IEnumerable<Contact>. (Using var is not always that helpful.) For this reason I suspect your last two lines do not even compile.

In the example below a fetch-query is sent to the Organization.svc endpoint. (Note the alias attribute.) Then field "new_name" of entity "new_custom" is taken from the first row of the query's resultset.

var fetchXml = @"
    <fetch version='1.0' mapping='logical' distinct='true'>
        <entity name='contact'>
        <all-attributes />
        <link-entity name='new_custom' alias='nc' from='new_contactid' to='contactid' link-type='inner'>
            <all-attributes />
        </link-entity>
        </entity>
    </fetch>
";

var query = new FetchExpression(fetchXml);
var response = service.RetrieveMultiple(query);
var contact = response.Entities[0];
var customField = contact.GetAttributeValue<AliasedValue>("nc.new_name");
string name = customField != null ? (string)customField.Value : null;
like image 154
Henk van Boeijen Avatar answered Mar 03 '23 04:03

Henk van Boeijen