简体   繁体   中英

Retrieve attribute value of link-entity in FetchXML

I have the following code:

    Entity account = service.Retrieve("account", ID, new ColumnSet(true));
    string name = (string)account.Attributes["name"];

        const string fetchXmlPattern =
        @"<fetch mapping='logical' version='1.0'>
            <entity name='account'>
                <attribute name='maintname' />
            <filter>
                <condition attribute='maintname' operator='eq' value='{0}' />
            </filter>
            <link-entity name='contact' from='contactid' to='primarycontactid' link-type='inner'>
                <attribute name='accountidname'/>
            </link-entity>
            </entity>
        </fetch>";

        string fetchXml = string.Format(fetchXmlPattern, name);

        var fetchExpression = new FetchExpression(fetchXml);
        EntityCollection response = service.RetrieveMultiple(fetchExpression);
        var records = response.Entities;

        List<Account> Accounts= new List<Account>();

        foreach (var item in records)
        {
            Account AccountFranchisee = new Account();
            AccountFranchisee.ID = (Guid)item.Attributes["accountid"];
            AccountFranchisee.Name = (string)item.Attributes["accountidname"];
            Accounts.Add(AccountFranchisee);
        }
        return Accounts;

I get a Key Not Found Exception at this line:

AccountFranchisee.Name = (string)item.Attributes["accountidname"];

Meaning it can't find the attribute that is in the link-entity. I tried putting aliases, changing pattern, changing syntax, and several solution on the internet, etc... I spent a lot of hours on this thing, still can't figure out what is wrong.

How can I get the value?

Thank you!

accountidname is an attribute of the linked entity. Therefore you need to use the Aliased value to retrieve its value.

First, modify your FetchXml to specify an alias for the link-entity . Here I've given an alias of "PrimaryContact" - this step isn't really mandatory, but I think it's better to be explicit. If you don't specify an alias, CRM will automatically create one for you (probably "contact1")

<fetch mapping='logical' version='1.0'>
  <entity name='account'>
    <attribute name='maintname' />
    <filter>
      <condition attribute='maintname' operator='eq' value='{0}' />
    </filter>
    <link-entity name='contact' from='contactid' to='primarycontactid' link-type='inner' alias='PrimaryContact'>
      <attribute name='accountidname'/>
    </link-entity>
  </entity>
</fetch>

Now when you're retrieving the value, you need to combine the alias and the field name Try something like

AccountFranchisee.Name = item.GetAttributeValue<AliasedValue>("PrimaryContact.accountidname").Value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM