Using Metadata WebService of CRM bind Dropdownlist with particular Picklist attribute of CRM 4 on another application.
In this article the code snipet will demonstrate that how anyone can have DropDownList exposing different information from CRM Picklist attribute.
Using Metadata Web Service anyone can show all the options from a particular Picklist attribute on another application.
The following code illustrates to retrieve all possible values for the Trainee Status property for Trainee Entity. Because the Trainee Status is a Picklist, anyone would have to query the MetaData to get the values.
aspx Page :
Code-Behind Page : protected void Page_Load(object sender, EventArgs e){ try{ if (!IsPostBack){ ListItemCollection listColl = new ListItemCollection(); listColl = GetTraineeStatus(“mfsbbsr”); drpStatus.DataSource = listColl; drpStatus.DataTextField = “Text”; drpStatus.DataValueField = “Value”; drpStatus.DataBind(); } }catch (Exception ex){ Response.Write(“Page_Load :: General exception: ” + ex.ToString());} }
The following code will generate a common method to retrieve the Metadata Web Service URL for the organization by using the Discovery Web Service.
private string GetCrmMetadataServiceForOrganization(string organizationName) { string urlResult = ""; try{ CrmSdk.Discovery.CrmDiscoveryService myCrm = new CrmSdk.Discovery.CrmDiscoveryService(); myCrm.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; CrmSdk.Discovery.RetrieveOrganizationsRequest myRequest = new CrmSdk.Discovery.RetrieveOrganizationsRequest(); CrmSdk.Discovery.RetrieveOrganizationsResponse myResponse = (CrmSdk.Discovery.RetrieveOrganizationsResponse)myCrm.Execute(myRequest); foreach (CrmSdk.Discovery.OrganizationDetail tDetail in myResponse.OrganizationDetails) { if (String.Compare(tDetail.OrganizationName, organizationName, true) == 0){ urlResult = tDetail.CrmMetadataServiceUrl;} } }catch (System.Web.Services.Protocols.SoapException soapEx) { Response.Write("GetCrmMetadataServiceForOrganization :: SOAP exception: " + soapEx.Detail.InnerText + " " + soapEx.ToString()); }catch (Exception ex) { Response.Write("GetCrmMetadataServiceForOrganization :: General exception: " + ex.ToString()); } return urlResult; }
The following code will retrieve all possible values for the Trainee Status property for custom entity Trainee and return ListItemCollection object to bind with Dropdownlist in Page_Load event of form.
private ListItemCollection GetTraineeStatus(string organizationName){ try{ CrmSdk.Metadata.MetadataService myCrm = new CrmSdk.Metadata.MetadataService(); myCrm.Url = GetCrmMetadataServiceForOrganization(organizationName);//"http://dycrm:5555/MSCrmServices/2007/MetadataService.asmx"; CrmSdk.Metadata.CrmAuthenticationToken myToken = new CrmSdk.Metadata.CrmAuthenticationToken(); myToken.AuthenticationType = 0; myToken.OrganizationName = organizationName; myCrm.CrmAuthenticationTokenValue = myToken; myCrm.Credentials = System.Net.CredentialCache.DefaultCredentials; CrmSdk.Metadata.RetrieveAttributeRequest myRequest = new CrmSdk.Metadata.RetrieveAttributeRequest(); myRequest.EntityLogicalName = CrmSdk.EntityName.new_trainee.ToString(); myRequest.LogicalName = "new_traineestatus"; CrmSdk.Metadata.RetrieveAttributeResponse myResponse; myResponse = (CrmSdk.Metadata.RetrieveAttributeResponse) myCrm.Execute(myRequest); ListItemCollection listColl = new ListItemCollection(); ListItem lit; foreach (CrmSdk.Metadata.Option myOption in ((CrmSdk.Metadata.PicklistAttributeMetadata)(myResponse.AttributeMetadata)).Options){ lit = new ListItem(); lit.Text = myOption.Label.LocLabels[0].Label; lit.Value = myOption.Value.formattedvalue; listColl.Add(lit); }return listColl; }catch (System.Web.Services.Protocols.SoapException soapEx){ Response.Write("GetTraineeStatus :: SOAP exception: " + soapEx.Detail.InnerText + " " + soapEx.ToString()); return null; }catch (Exception ex){ Response.Write("GetTraineeStatus :: General exception: " + ex.ToString()); return null; } }