Find out the names of all the attributes in an entity returned from CRM Dynamics
Asked Answered
T

2

8

I got myself into the server and I have retrieved (hopefully) the correct set of data. Then, I tried to list all the companies by the following code.

EntityCollection result = proxy.RetrieveMultiple(expression);
foreach (var entity in result.Entities)
{
  String output = String.Empty;
  if (entity.Attributes.Contains("account"))
    output = entity.Attributes["account"].ToString();
}

However, it'd be nice to run an inner loop that goes through all the available attributes in result.Entities. Should I use entity.Attributes.Keys or is there a better method?

Tantalus answered 13/9, 2012 at 9:33 Comment(0)
A
14

I think this should do the trick.

foreach (Entity entity in result.Entities)
{
    foreach (KeyValuePair<String, Object> attribute in entity.Attributes)
    {
        Console.WriteLine(attribute.Key + ": " + attribute.Value);
    }
} 
Alida answered 13/9, 2012 at 10:11 Comment(1)
OK, so I was on the right track. I was afraid that the recommendation for entities gotten from Dynamics were to be handled in a different way. I also made a small change to your example.Tantalus
S
0

This carries out the task using a Lambda expression.

EntityCollection result = proxy.RetrieveMultiple(expression);
foreach (var entity in result.Entities)
{
    var vsHeaders = entity.Attributes.Select(kvp => string.Format("{0}", kvp.Key));
    string sHeaders = string.Join(",", vsHeaders);
}
Skippet answered 4/10, 2016 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.