Thursday, August 1, 2013

Retrieve Entity Metadata in CRM 2011 using C#

string entityName ="contact";

// Get the metadata for the currently list's entity
// This metadata is used to create a "Property Descriptor Collection"
RetrieveEntityRequest mdRequest = new RetrieveEntityRequest ( )
{ EntityFilters = EntityFilters.All,
LogicalName = entityName,
RetrieveAsIfPublished = false
};

// Execute the request
RetrieveEntityResponse entityResponse = ( RetrieveEntityResponse ) this.ServiceProxy.Execute ( mdRequest );

EntityMetadata entityData = entityResponse.EntityMetadata;



//To Get Option Set Data
var preferdList= ( entityData.Attributes.Where ( p => p.LogicalName == "preferredcontactmethodcode" ) ).ToList ( ).FirstOrDefault ( ); ;

if ( preferdList != null ) {

PicklistAttributeMetadata optionList= preferdList as PicklistAttributeMetadata;
OptionSetMetadata opValues= optionList.OptionSet;
foreach ( var op in opValues.Options ) {

preferedMethod.Add ( new OptionSet { Value = op.Value.Value, Text = op.Label.LocalizedLabels[0].Label.ToString() } );
}

}

2 comments:

Unknown said...

The more we exploit nature, The more our options are reduced, until we have only one: to fight for survival. See the link below for more info.


#options
www.ufgop.org

moshrabbi121 said...

Youre so cool! I dont suppose Ive learn something like this before. So good to seek out someone with some unique thoughts on this subject. realy thanks for beginning this up. this web site is one thing that is wanted on the internet, somebody with just a little originality. useful job for bringing one thing new to the internet! play casino