Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Querying CRM metadata to find field name

Posted on by 300

Hi,

I have an entity in a CRM plugin and I need to find any field names that are lookups to a given entity. So I might want to say.. "what are the field names on this entity that are lookups to Contact?". I think this might be possible using the metadata but cannot find a way to do it.

Any pointers on how this might be done would be appreciated.
Thanks,

*This post is locked for comments

  • mluce Profile Picture
    mluce 300 on at
    RE: Querying CRM metadata to find field name

    There may be lookups to multiple entities.  Do you know how to check the lookup to see if it is relating to Contact?

  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Querying CRM metadata to find field name

    It depends on whether you want to use metadata (then what Aric wrote), or whether you want to find all value which are actually referencing Contacts.. In the latter case, you might just go over all the attributes in the post image (or, depending on what it's about, in the Target), check if any of those are entity references, and, for those which are entity references, check the logicalName property. That way you can avoid metadata calls, but it'll only work for populated fields

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Querying CRM metadata to find field name

    Not sure if you are planning to use sdk, but you can use the following function to get all attributes, and then check if the attribute type is Lookup:

               RetrieveEntityRequest request = new RetrieveEntityRequest()

               {

                   LogicalName = entityName

                   EntityFilters = EntityFilters.All,

                   RetrieveAsIfPublished = true

               };

               RetrieveEntityResponse response = (RetrieveEntityResponse)service.Execute(request);

               foreach (AttributeMetadata attr in response.EntityMetadata.Attributes)

               {

                   string logicalName = attr.LogicalName;

                   string attrType = attr.AttributeType.ToString();

                   string displayName = attr.DisplayName.LocalizedLabels.Count > 0 ? attr.DisplayName.LocalizedLabels[0].Label : string.Empty;

                   if (attrType.ToLower() == "lookup")

                   {

                     // Your logic here

                   }

               }

           }

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans