web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Microsoft.Xrm.Sdk.Metadata.EntityMetadata Contains method does not work

(0) ShareShare
ReportReport
Posted on by 82

This may be more of a .NET question potentially but it is certainly strange.

I have some old code in a Command Line App that I have dusted off which retrieves an Microsoft.Xrm.Sdk.Metadata.EntityMetadata object from Dynamics.  In my case v8.2.

Basically a list of Entities and their Settings, Attributes etc.

I have some logic which applies the "Contains" method to the resulting object (currentEntity).

if (currentEntity.Contains("PrimaryNameAttribute"))
{
    metadataWriter.WriteElementString("PrimaryNameAttribute", currentEntity.PrimaryNameAttribute);
}

The thing is, it is always false.  This worked in the past.  I can see the "PrimaryNameAttribute when I debug and the spelling/case looks correct.

The SDK version is 9.0.0.0 so it is later than the CRM Instance but I believe this worked maybe 2 years ago.

Bemused and annoying.

I can comment out the IF statements but this did work and I can see the attribute so it would be great to know what has gone wrong.

*This post is locked for comments

I have the same question (0)
  • Martin Donnelly Profile Picture
    1,030 on at

    what happens if you try "primarynameattribute"?  I've only ever seen the proper cased versions used on the client jscript side.  Always "ToLower" in .NET.  I have no guess how this worked then but not now -- and I go back to v 4.0.

  • Kokulan Profile Picture
    18,054 on at

    Could you share bit more of your code, to see how you are getting the currentEntity. And could you also share the details on .NET version you are  using.

  • GordonJohnston Profile Picture
    82 on at

    Just tried and lower case does not work.

  • GordonJohnston Profile Picture
    82 on at

    .NET 4.6.1

    I have chopped out a few lines from throughout my code since there are numerous classes.  Going Backwards!!

    foreach (EntityMetadata currentEntity in entityMetadataList) { ... }

    public List<EntityMetadata> RetrieveEntityMetadata()

           {

               Trace.WriteLineIf(appTraceSwitch.TraceInfo, "Retrieving entity metadata from Dynamics...");

               RetrieveAllEntitiesRequest retrieveAllEntityRequest = new RetrieveAllEntitiesRequest

               {

                   RetrieveAsIfPublished = true,

                   EntityFilters = EntityFilters.Attributes

               };

               RetrieveAllEntitiesResponse retrieveAllEntityResponse = (RetrieveAllEntitiesResponse)Service.Execute(retrieveAllEntityRequest);

               List<EntityMetadata> entityMetadataList = retrieveAllEntityResponse.EntityMetadata.OfType<EntityMetadata>().ToList();

               entityMetadataList.Sort((x, y) => x.LogicalName.CompareTo(y.LogicalName));

               Trace.WriteLineIf(appTraceSwitch.TraceInfo, $"{entityMetadataList.Count} entities found.");

               if (appTraceSwitch.TraceVerbose)

               {

                   entityMetadataList.ForEach(e => Trace.WriteLine(e.LogicalName));

               }

               return entityMetadataList;

           }

  • GordonJohnston Profile Picture
    82 on at

    I did successfully execute the program by commenting out the IF statements.  So I know the field works.

    The check were only there because I originally wrote this to move data between a very old version of CRM and a newer version.  The field schema had changed across these versions (although they are much more stable now).

    It is a minor thing but I am always resistant to using try-catch for these situation because it is bad for performance.  Though I had a sensible solution.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans