locked
Retrieving Attribute names of an Entity in MS CRM 4.0 RRS feed

  • Question

  • I am back again with another question on ms crm.

     

    The latest thing i am trying to do is retrieve the attribute name and type that exist in an entity, Dynamic Entity to be precise. I have the following code.

     

    Code Snippet
    DynamicEntity contactEntity = new DynamicEntity();
    contactEntity
    .Name = EntityName.contact.ToString();
    Property t = null;
    foreach (Property prop_Test in contactEntity.Properties)
    {
       Response.Write("
    Name : "
    + prop_Test.Name.ToString());  
    }

     

     

    I am getting the properties count as 0. Is it mandatory for me to pass an id to the contact entity. Because i am trying to map attributes from the entity to the attributes i get from an excel file. The end user themselves would be doing the mapping so all i need are the attribute name and type and nothing else. For instance in SQL we have the query

    Code Snippet
    SELECT * FROM TABLE_NAME WHERE 1 <> 1

     

     


    This query basically returns an empty resultset with only the fieldnames. That is what i am looking for here. Is it even possible ?

    Friday, October 17, 2008 9:11 PM

Answers

  • What you are attempting will not work - first of all you'd have to retrieve an instance of your entity, and even then the Properties collection only includes those fields that contain data.

     

    Instead you'll have to use the MetadataService to get the attributes of an entity

    Saturday, October 18, 2008 8:50 AM
    Moderator

All replies

  • Hi,

     

    Here is the code sample that may be helpful for you - it shows how to retrieve a dynamic entity and its properties: http://msdn.microsoft.com/en-us/library/bb928422.aspx.

     

    Kind regards,

    Kuba

     

    Friday, October 17, 2008 10:26 PM
  • What you are attempting will not work - first of all you'd have to retrieve an instance of your entity, and even then the Properties collection only includes those fields that contain data.

     

    Instead you'll have to use the MetadataService to get the attributes of an entity

    Saturday, October 18, 2008 8:50 AM
    Moderator
  • Hi david,

     

    Thanks for the reply. I am trying to use a MetadataService to get the same. I saw an example in this URL

     

    http://msdn.microsoft.com/en-us/library/cc156306.aspx

     

    But it is working only if i specify an attribute name which already exists. EWhat I am trying to do is to get a list of all attributes that exist for a particular entity. Do you have any code samples ? Most of the samples in the SDK deal with specifying the attribute name and then querying it. Any help on this would be appreciated. Thanks again Smile.

    Saturday, October 18, 2008 9:07 AM
  •  

    Thanks David

     

    I found the answer in your blog.

     

    http://mscrmuk.blogspot.com/2007/11/changes-to-code-to-use-crm-40-web.html

    Saturday, October 18, 2008 10:04 AM
  •  

    Hi Vikram,

     

    The following code helps you.

     

    The follwoing code retrieves all the attributes for an entity and adds to a List box.

     

    First create a metadata service object (_MS).

     

    // creating RetrieveEntityRequest

    MetadataService.RetrieveEntityRequest RERequest = new MetadataService.RetrieveEntityRequest();

    RERequest.LogicalName = strEntity;

    RERequest.EntityItems = EntityItems.IncludeAttributes;

    // Executing the request

    MetadataService.RetrieveEntityResponse REResponse = (MetadataService.RetrieveEntityResponse)_MS.Execute(RERequest);

    ListBox lstBox = new ListBox();

    // Displaying the attributes in the list box

    foreach(MetadataService.AttributeMetadata attmetadata in REResponse.EntityMetadata.Attributes)

    {

    if (attmetadata.ValidForCreate.Value == true && attmetadata.ValidForUpdate.Value == true)

    {

    lstBox.Items.Add(attmetadata.SchemaName);

    }

    }

    lstBox.Sorted = true;

     

    Thanks and Regards

    Ramu

    • Proposed as answer by Kneo1611 Thursday, April 7, 2011 2:56 PM
    Saturday, October 18, 2008 10:09 AM
  • Hi, Ramu

     in ur code, 'strEntity' stands for what?..How it is recognized by the .cs page

    RERequest.LogicalName = strEntity;

     

              

    Wednesday, June 30, 2010 1:26 PM
  •    Hi,

         Can you tell me the process of get list of all attributes by passing an entity name in sdk 4.0 mscrm.

     

     

    Wednesday, February 2, 2011 5:36 AM