Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Get related entities programmatically by 1:N relationship name (CRM 4.0)

Posted on by Microsoft Employee

Hi All,

I have entity A and B with thousand records. A has a 1:N relationship named "new_Bs". What is the correct method to get the related Bs for a given A using Crm.Discovery.* APIs? (Not Xrm)

*This post is locked for comments

  • Inogic Profile Picture
    Inogic 24,094 on at
    RE: Get related entities programmatically by 1:N relationship name (CRM 4.0)

    If you are working on plugin, getting organization service context won’t be problem from you. For standalone application or website you can refer CRM assemblies (“Xrm.SDK”) that will resolve your Xrm issue.

    For CRM version earlier than 2016 you can refer SDK assembly Microsoft.Xrm.Client.dll, which will provide you functions to retrieve records using relationship. From 2016 you need to write a function which will retrieve records using Linq.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Get related entities programmatically by 1:N relationship name (CRM 4.0)

    Thanks Mike,

    however it seems that iOrganizationServiceContext requires references to *.Xrm.* which I don't have. I need to rely on CrmService calls.

  • st738407 Profile Picture
    st738407 225 on at
    RE: Get related entities programmatically by 1:N relationship name (CRM 4.0)

    In a plugin we use something like the below, where we use LINQ.  dds_opportunity_product_line is our child entity (B in your example).  Hope this helps.            

    using (OrganizationServiceContext orgContext = new OrganizationServiceContext(orgService))

               {

                   var oppEF = opportunity.GetAttributeValue<EntityReference>("opportunityid");

                   var OPLs = orgContext.CreateQuery("dds_opportunity_product_line").Where(p => p.GetAttributeValue<EntityReference>("dds_opportunity_name") ==

                       oppEF && p.GetAttributeValue<OptionSetValue>("statecode").Value == Constants.OptionSets.EntityState.Active).ToList();

                   if (OPLs.Count() == 0)

                   {

                       return;

                   }

                   foreach (var OppProd in OPLs)

                   {

                   }

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans