Search code examples
dynamics-crmdynamics-crm-2011crmdynamics-crm-4

How to get list of data items list from CRM Metadata Source in code behind?


How to get list of data items list from CRM Metadata Source in code behind?

I have a CRm Metadata Source like

<crm:CrmMetadataDataSource ID="dsquestionOptionset" runat="server" EntityName="contact"
     AttributeName="securityquestion"/>

in the html.

I would like to get list of data items in the code behind from the datasource.

"securityquestion" is an intger value and this is linked to an option set.

I tried like

 var listOfItems=dsquestionOptionset.Items;

But not possible

Any help is appreciated

Vinu


Solution

  • You'll want to query the Metadata. An example (from here: http://msdn.microsoft.com/en-us/library/gg509035.aspx) would be something like:

    RetrieveAttributeRequest retrieveAttributeRequest =
        new RetrieveAttributeRequest
    {
        EntityLogicalName = Contact.EntityLogicalName,
        LogicalName = "new_picklist",
        RetrieveAsIfPublished = true
    };
    
    // Execute the request.
    RetrieveAttributeResponse retrieveAttributeResponse =
        (RetrieveAttributeResponse)_serviceProxy.Execute(
        retrieveAttributeRequest);
    
    // Access the retrieved attribute.
    PicklistAttributeMetadata retrievedPicklistAttributeMetadata =
        (PicklistAttributeMetadata)
        retrieveAttributeResponse.AttributeMetadata;
    
    // Get the current options list for the retrieved attribute.
    OptionMetadata[] optionList =
        retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();