Accessing metadata in Microsoft CRM 2011

18Feb11

I have been porting a lot of code over from CRM4 to the new CRM 2011 recently. I was investigating how to get some code converted that uses the MetadataService in CRM4 but I wasn’t finding out much about it on the web.

If you are looking for the metadata service in 2011 you can stop now, because there isn’t one. The new way of doing things is just to make your metadata requests to the main Web service.

The message classes are named the same, so the code will be mostly compatible once you set things up to use the 2011 Web service.

The namespaces you’ll need in most cases will be:

// for service proxy type, IOrganizationService/OrganizationServiceProxy
using Microsoft.Xrm.Sdk.Client;

// needed for 'RetrieveXXXRequest' types
using Microsoft.Xrm.Sdk.Messages;

// needed for actual metadata descriptor types
using Microsoft.Xrm.Sdk.Metadata;

For example, one of the things that I use metadata for is to get the text values that are in a CRM picklist. The following code snippet will query CRM 2011 metadata for the gendercode field on the contact entity and print out the values.

IOrganizationService serviceProxy = new OrganizationServiceProxy(
	Config.OrganizationUri,
	null,
	(System.Net.NetworkCredential)System.Net.CredentialCache.DefaultCredentials,
	null
);

RetrieveAttributeRequest request = new RetrieveAttributeRequest();
request.EntityLogicalName = "contact";
request.LogicalName = "gendercode";

RetrieveAttributeResponse response = ( RetrieveAttributeResponse )serviceProxy.Execute( request );
PicklistAttributeMetadata picklist = ( PicklistAttributeMetadata )response.AttributeMetadata;

foreach( OptionMetadata option in picklist.OptionSet.Options ) {
	Console.WriteLine( option.Label.UserLocalizedLabel.Label );
}

Advertisements


2 Responses to “Accessing metadata in Microsoft CRM 2011”

  1. Hey thanks very much. I was running into an error while trying to run the execute command from RetrieveAttributeResponse. Your commented namespace references helped me a lot.

    // needed for ‘RetrieveXXXRequest’ types

    using Microsoft.Xrm.Sdk.Messages;

    Thanks again
    Shweta


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: