Answered by:
Get Picklist value In CRM 4.0

Question
-
Hi,I need a solution regarding Dynamics CRM.I have a attribute in Dynamics CRM 4.0 in Contact entity. That attribute is picklist type. I want to retrive that picklist value in ASP.net and bind in a Dropdown server control.How to access Picklist value please send me some sample code.ThanksMalayThursday, October 16, 2008 2:44 PM
Answers
-
Hi,
You have to use the Metadata service to retrieve the MetadataAttribute object. Ater setting up the Metadata service as described in the sdk (here: http://msdn.microsoft.com/en-us/library/cc151046.aspx), you may retrieve the picklist attribute. Here is the code you could use (replace <your picklist attribute name> with the proper name:
Code SnippetRetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest();
attributeRequest.EntityLogicalName = "contact";
attributeRequest.LogicalName = <your picklist attribute name>;
attributeRequest.RetrieveAsIfPublished = true;RetrieveAttributeResponse response = (RetrieveAttributeResponse)metaService.Execute(attributeRequest);
PicklistAttributeMetadata picklist = (PicklistAttributeMetadata)response.AttributeMetadata;
foreach (Option o in picklist.Options)
{
// do something e.g. take o.ValueValue
}
Let me know whether this is what you wanted to get,Kuba Skalbania
- Marked as answer by Andrii ButenkoMVP, Moderator Monday, June 20, 2011 12:40 PM
Thursday, October 16, 2008 3:49 PM
All replies
-
Hi,
You have to use the Metadata service to retrieve the MetadataAttribute object. Ater setting up the Metadata service as described in the sdk (here: http://msdn.microsoft.com/en-us/library/cc151046.aspx), you may retrieve the picklist attribute. Here is the code you could use (replace <your picklist attribute name> with the proper name:
Code SnippetRetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest();
attributeRequest.EntityLogicalName = "contact";
attributeRequest.LogicalName = <your picklist attribute name>;
attributeRequest.RetrieveAsIfPublished = true;RetrieveAttributeResponse response = (RetrieveAttributeResponse)metaService.Execute(attributeRequest);
PicklistAttributeMetadata picklist = (PicklistAttributeMetadata)response.AttributeMetadata;
foreach (Option o in picklist.Options)
{
// do something e.g. take o.ValueValue
}
Let me know whether this is what you wanted to get,Kuba Skalbania
- Marked as answer by Andrii ButenkoMVP, Moderator Monday, June 20, 2011 12:40 PM
Thursday, October 16, 2008 3:49 PM -
Dear Friend,
I have faced problem in getting request .
Please see my code
// Create an authentication token.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.OrganizationName = "organization name";
// You can use enums.cs from the SDK\Helpers folder to get the enumeration for AD Authentication.
token.AuthenticationType = 0;
// Create the metadata Web service;
CrmSdk.Metadata.MetadataService service = new CrmSdk.Metadata.MetadataService();
service.Url = "https://"organization name"."domain name without port no"/MSCRMServices/2007/MetadataService.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = new System.Net.NetworkCredential("user name", "password");
service.PreAuthenticate = true;
// Create the request
RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest();
attributeRequest.EntityLogicalName = "contact";
attributeRequest.LogicalName = "address1_city";
attributeRequest.RetrieveAsIfPublished = true;
RetrieveAttributeResponse response = (RetrieveAttributeResponse)service.Execute(attributeRequest);
PicklistAttributeMetadata picklist = (PicklistAttributeMetadata)response.AttributeMetadata;
foreach (Option o in picklist.Options)
{
// do something e.g. take o.ValueValue
}
" RetrieveAttributeResponse response = (RetrieveAttributeResponse)service.Execute(attributeRequest);" in this line there is an error message "The request failed with HTTP status 401: Unauthorized."
Noted that I am using DOT net 3.5 version and I have added the web-service of the remote Metadata service in my local machine.
Please give me reply as soon as possibleSaturday, October 18, 2008 11:12 AM -
My Friend,
You can try:
1. Verify if the user have permissions to access this organization
2. Remove the line service.PreAuthenticate = true;
3. Put the domain in service.Credentials = new System.Net.NetworkCredential("user name", "password", "domain");[]s
Saturday, October 18, 2008 11:01 PM -
Wednesday, October 22, 2008 4:47 PM
-
Hi,
I need to retreive the picklist items values using MetaData service.
I am not able to connect to the MetaData Service in my production environment which is an IFD deployment.
It gives me an "401 : UnAuthorised Error." . The same code works well in my development environment which is a VPC environment.
Pls find code below for the same.Also when I look at the log file the error oroiginates from the line
"RetrieveAttributeResponse amRes = (RetrieveAttributeResponse)service.Execute(attribReq);"The metadata service url created is fine and it returns the meta methods when pasted on a web browser.
public static MetadataService GetMetaDataService()
{
MetadataService service = new MetadataService();
try{
string organization = "PROD"; //ConfigurationManager.AppSettings["OrgName"].ToString();
string server = "PRODUCTION";//ConfigurationManager.AppSettings["ServerName"].ToString();
string domain = "HOSTING";//ConfigurationManager.AppSettings["CRMDomain"].ToString();
string username = "Administrator";//ConfigurationManager.AppSettings["CRMUserName"].ToString();
string password = "XXXXXX";//ConfigurationManager.AppSettings["CRMPassword"].ToString();
string VDBAuthenticationType = "SPLA";//ConfigurationManager.AppSettings["AuthenticationType"].ToString();
service.Credentials = new System.Net.NetworkCredential(username, password,domain);string Server = "PRODUCTION"; //ConfigurationManager.AppSettings["ServerName"].ToString();
string Port = "80";//ConfigurationManager.AppSettings["PortNumber"].ToString();
service.CrmAuthenticationTokenValue = new MetaSdk.CrmAuthenticationToken();
service.CrmAuthenticationTokenValue.OrganizationName = organization;
if (Port != "")
service.Url = string.Format("http://{0}:{1}/mscrmservices/2007/metadataservice.asmx", Server, Port);
else
service.Url = string.Format("http://{0}/mscrmservices/2007/metadataservice.asmx", Server);
}
catch(SoapException ex)
{
ErrorWriter(ex,"Meta Sevice Error");
}
}
Also the code for Retreiving picklist Values is here
public static int GetPickListItemValue(string entityName, string pickListName, string itemLabel)
{int itemValue = 0;
try
{MetadataService service = new MetadataService();
service = GetMetaDataService();
RetrieveAttributeRequest attribReq = new RetrieveAttributeRequest();
attribReq.EntityLogicalName = entityName;
attribReq.LogicalName = pickListName;// Get the attribute metadata for the pickListName attribute.
RetrieveAttributeResponse amRes = (RetrieveAttributeResponse)service.Execute(attribReq);AttributeMetadata am = amRes.AttributeMetadata;
PicklistAttributeMetadata listData = (PicklistAttributeMetadata)am;
foreach (Option option in listData.Options)
{
foreach (LocLabel label in option.Label.LocLabels)
{
// Show US English value:label pairs
if (label.LanguageCode.Value == 1033)if (label.Label.Equals(itemLabel))
{
itemValue = option.Value.Value;
}
}
}
}
catch(SoapException ex)
{
ErrorWriter(ex,"Error in Retreiving PickList Values, Metadata Sevice Error");
}
return itemValue;}
Pls help me out.
I am stuck with this code as it works fine in mu[y VPC but gives "401 error" in the production.
Also I have verified that the logged in user has the righta for Entity, Relationship and Attribute as he is the administarator of CRM.I have also added </clear> tag in the web.confog file but no sucess.
Regards
SusanSaturday, January 10, 2009 5:19 AM -
For CRM IFD Authentication, you first need to retrieve a CRM Ticket using CRM Discovery Service. That ticket then needs to be subsequently passed with each request for Metadata Service, by associating it with CRM Authentication Token.
You can check this out at msdn. http://msdn.microsoft.com/en-us/library/cc151010.aspx
Hope this helps.
Wednesday, January 14, 2009 6:47 PM -
Use Following way.....
CRM.CrmAuthenticationToken token = new CRM.CrmAuthenticationToken(); token.AuthenticationType = 0; token.OrganizationName = "indcrm"; CrmService service = new CrmService(); service.Url = "http://191.168.100.197:5555/MSCrmServices/2007/CrmService.asmx"; service.CrmAuthenticationTokenValue = token; service.Credentials = new System.Net.NetworkCredential("administrator", "Password", "domain");
StringProperty name = new StringProperty(); name.Name = "new_name"; name.Value = TxtFGProjectName.Text.ToString(); LookupProperty companyId = new LookupProperty(); companyId.Name = "new_companyid"; companyId.Value = new Lookup(); companyId.Value.name = DdlCompany.SelectedItem.Text; companyId.Value.Value = new Guid(DdlCompany.SelectedValue);//new Guid(DdlCompany.Items[DdlCompany.SelectedIndex].Value); StringProperty projectStatus = new StringProperty(); projectStatus.Name = "new_projectstatus"; projectStatus.Value = DdlProjectStatus.SelectedValue; StringProperty currentBudget = new StringProperty(); currentBudget.Name = "new_budgetcurrentcapexrequirement"; currentBudget.Value = TxtBudgetCurrent.Text.ToString(); //StringProperty projectType = new StringProperty(); //projectType.Name = "new_projecttype"; //projectType.Value = TxtProjectType.Text.ToString(); StringProperty annualBudget = new StringProperty(); annualBudget.Name = "new_budgetannualcapexrequirement"; annualBudget.Value = TxtBudgetAnnual.Text.ToString(); CrmDateTimeProperty estimatedStartDate = new CrmDateTimeProperty(); estimatedStartDate.Name = "new_estimatedstartdate"; estimatedStartDate.Value = new CRM.CrmDateTime(); estimatedStartDate.Value.Value = Convert.ToString(CalEstimatedStartDate.SelectedDate); CrmDateTimeProperty estimatedDeadlineDate = new CrmDateTimeProperty(); estimatedDeadlineDate.Name = "new_estimateddeadlinedate"; estimatedDeadlineDate.Value = new CRM.CrmDateTime(); estimatedDeadlineDate.Value.Value = Convert.ToString(CalEstimatedDeadlineDate.SelectedDate); StringProperty projectName = new StringProperty(); projectName.Name = "new_projectname"; projectName.Value = TxtProjectName.Text.ToString(); //StringProperty typeofProject = new StringProperty(); //typeofProject.Name = "new_typeofproject"; //typeofProject.Value = TxtTypeofProject.Text.ToString(); PicklistProperty projectTypepicklist = new PicklistProperty(); projectTypepicklist.Name = "new_projecttypepicklist"; projectTypepicklist.Value = new Picklist(); projectTypepicklist.Value.Value = DdlProjectType.SelectedIndex + 1; PicklistProperty typeofProjectpicklist = new PicklistProperty(); typeofProjectpicklist.Name = "new_typeofprojectpicklist"; typeofProjectpicklist.Value = new Picklist(); typeofProjectpicklist.Value.Value = DdlTypeofProject.SelectedIndex + 1; StringProperty companyName = new StringProperty(); companyName.Name = "new_companyname"; companyName.Value = TxtCompanyName.Text.ToString(); StringProperty firstName = new StringProperty(); firstName.Name = "new_firstname"; firstName.Value = TxtFirstName.Text.ToString(); StringProperty lastName = new StringProperty(); lastName.Name = "new_lastname"; lastName.Value = TxtLastName.Text.ToString(); StringProperty emailId = new StringProperty(); emailId.Name = "new_emailid"; emailId.Value = TxtEmailId.Text.ToString(); StringProperty advancedDeposit = new StringProperty(); advancedDeposit.Name = "new_advanceddepositamount"; advancedDeposit.Value = TxtAdvancedDepositAmount.Text.ToString(); StringProperty progressPayments = new StringProperty(); progressPayments.Name = "new_progresspaymentschedule"; progressPayments.Value = TxtProgressPaymentSchedule.Text.ToString(); StringProperty projectInformationStatus = new StringProperty(); projectInformationStatus.Name = "new_projectinformationstatus"; projectInformationStatus.Value = DdlProjectInformationStatus.SelectedValue; DynamicEntity testFg = new DynamicEntity(); testFg.Name = "new_fgproject"; PropertyCollection properties = new PropertyCollection(); properties.Add(name.Name, name); properties.Add(companyId.Name, companyId); testFg.Properties = new Property[] { name, companyId, projectStatus, currentBudget, annualBudget, estimatedStartDate, estimatedDeadlineDate, projectName, companyName, firstName, lastName, emailId, advancedDeposit, progressPayments, projectInformationStatus, projectTypepicklist, typeofProjectpicklist }; TargetCreateDynamic targetCreate = new TargetCreateDynamic(); targetCreate.Entity = testFg; CreateRequest create = new CreateRequest(); create.Target = targetCreate; try { CreateResponse created = (CreateResponse)service.Execute(create); Guid Projectid = created.id;
I hope it will help you. !!
- Proposed as answer by DynamicsCRM31 Thursday, April 5, 2012 9:17 AM
Thursday, April 5, 2012 9:17 AM -
I could never get this snippit to work correctly.
For the line of code:
RetrieveAttributeResponse response = (RetrieveAttributeResponse)metaService.Execute(attributeRequest);
It would always complain that it was a bad argument. It wants an argument of CrmSdk.Response but instead we are giving it MetaSvc.RetrieveAttributeRequest & so the compiler pukes.
Thursday, June 21, 2012 5:54 PM