Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

Syntax question, how to retrieve entity reference from linked entity field in C#?

Posted on by 725

I have a fetch string that retrieve a field from main entity a field from linked entity.

Both fields are entity reference, I was able to retrieve the entity reference from main entity using below:

EntityReference field1 = result.Entities[0].GetAttributeValue<EntityReference>("field name");

However, I was not able to retrieve the entity reference from linked entity, I tried the following and none worked.

EntityReference field2 = result.Entities[0].GetAttributeValue<EntityReference>("field name");

EntityReference field2 = result.Entities[0].GetAttributeValue<EntityReference>("linked alias.field name");

EntityReference field2 = (EntityReference)result.Entities[0].GetAttributeValue<EntityReference>("linked alias.field name");

I also see people adding .Value on some google tutorials but .Value is not a valid method for entity reference, please let me know what is the correct syntax to use.

  • TylerS.Dev Profile Picture
    TylerS.Dev 725 on at
    RE: Syntax question, how to retrieve entity reference from linked entity field in C#?

    Thank you Aditya, your syntax works perfectly.

  • Verified answer
    Aditya Profile Picture
    Aditya 260 on at
    RE: Syntax question, how to retrieve entity reference from linked entity field in C#?

    Hi TylerS.Dev,

    please refer the below example for your reference. Here, I have taken opportunity as main entity and its entityreference field is contact. I am retrieving the "Account Name" (logical name - parentcustomerid) of Contact which is an entityreference. In  order to retrieve any field  from linked entity you have to use the AliasedValue.

    string fetchquery = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
    "<entity name='opportunity'>" +
    "<attribute name='name' />" +
    "<attribute name='parentcontactid' />" +
    "<attribute name='opportunityid' />" +
    "<order attribute='name' descending='false' />" +
    "<filter type='and'>" +
    "<condition attribute='statecode' operator='eq' value='0' />" +
    "<condition attribute='parentcontactid' operator='not-null' />" +
    "</filter>" +
    "<link-entity name='contact' from='contactid' to='parentcontactid' visible='false' link-type='inner' alias='ab'>" +
    "<attribute name='parentcustomerid' />" +
    "<attribute name='emailaddress1' />" +
    "</link-entity>" +
    "</entity></fetch>";

    EntityCollection oppos = service.RetrieveMultiple(new FetchExpression(fetchquery));
    //foreach (var c in oppos.Entities)
    //{
    //opportunity's name
    string name = oppos.Entities[0].Attributes["name"].ToString();
    //opportunity's contact
    EntityReference contact = (EntityReference)oppos.Entities[0].Attributes["parentcontactid"];
    //opportunity's contact's email
    string email = ((AliasedValue)oppos.Entities[0].Attributes["ab.emailaddress1"]).Value.ToString();
    //opportunity's contact's company name (lookup)
    EntityReference contactscompany = (EntityReference)((AliasedValue)oppos.Entities[0].Attributes["ab.parentcustomerid"]).Value;

    // }

    Please mark the answer as verified,if it is useful to you.

  • TylerS.Dev Profile Picture
    TylerS.Dev 725 on at
    RE: Syntax question, how to retrieve entity reference from linked entity field in C#?

    Hi Clofly,

    I considered this option when I was thinking of a work around and know it would probably work but I want to know if I can just grab the field value from linked entity when I use retrievemultiple method so that the next time I need multiple fields across entities I can use a single retrievemultiple to get all fields across multiple entities.

  • TylerS.Dev Profile Picture
    TylerS.Dev 725 on at
    RE: Syntax question, how to retrieve entity reference from linked entity field in C#?

    Hi Shiva,

    as you can see from my original question, I already tried it and got unable to set variable error.  I checked fetchxml and was able to see the retrieved entityreference from there.

  • Suggested answer
    cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Syntax question, how to retrieve entity reference from linked entity field in C#?

    Hi Tyler,

    Since you've retrieved entity reference in main entity, you could just take this entity reference with its Id and Name attributes to retrieve linked entity's entity reference:

    As per my understanding, it could be relationship like contact > account > parentAccount.

                Entity entity = new Entity("contact");
                Guid guid = Guid.Parse("contactid");

                ColumnSet attributes =
                    new ColumnSet(new string[] { "parentcustomerid" });
                Entity retrievedResult =
                organizationService.Retrieve(entity.LogicalName, guid, attributes);

                EntityReference AccountName = retrievedResult.GetAttributeValue<EntityReference>("parentcustomerid");

                ColumnSet accountAttributes =
                    new ColumnSet(new string[] { "parentaccountid" });
                Entity accountResult = 
                    organizationService.Retrieve(AccountName.LogicalName, AccountName.Id, accountAttributes);

                EntityReference parentAccount = accountResult.GetAttributeValue<EntityReference>("parentaccountid");

                Console.WriteLine(parentAccount.Id);
                Console.WriteLine(parentAccount.Name);

    If it is not appropriate, please let me know your real requirement. :)

    Regards,

    Clofly

  • Shiva.B Profile Picture
    Shiva.B 150 on at
    RE: Syntax question, how to retrieve entity reference from linked entity field in C#?

    Hi Tyler S,

    you should use aliased values to get the linked entity attribute values

    result.Entities[0].GetAttributeValue<EntityReference>("aa.field name")

    here "aa" is the alias value of child entity

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans