Answered by:
Dynamics CRM 2013 : Populating a asp.net dropdown list with Account details

Question
-
Hi all,
I have a scenario where I have to populate a asp.net dropdown list with Account details(Account ID, Account Title).
The dropdown value=Account ID & dropdown texname=Account Title.
Any help/guide would be greatly appreciated.
Many Thanks & Regards
Vinay
Monday, February 16, 2015 2:18 PM
Answers
-
Hi Vinay,
Following example code for this, please provide CRM URL, username, password, domain as per your required in following code..
// Create a proxy connection to the CRM // Obtain the target organization's Web address and client log-on var crmUrl = new Uri(crmURL); var oCredentials = new ClientCredentials(); oCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(username, password, domain); var serviceProxy = new OrganizationServiceProxy(crmUrl, null, oCredentials, null); // This statement is required to enable early-bound type support. serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior()); serviceProxy.EnableProxyTypes(); // create query against account var query = new QueryExpression { EntityName = "account", ColumnSet = new ColumnSet(new string[] {"accountid", "name"}) }; query.AddOrder("name", new OrderType()); query.Criteria.FilterOperator = LogicalOperator.And; EntityCollection accounts = serviceProxy.RetrieveMultiple(query); var dropDownListCollection = new ListItemCollection(); // Create list items from entity foreach (Entity tempEntity in accounts.Entities) { if (tempEntity.Contains("name")) { dropDownListCollection.Add(new ListItem(tempEntity["name"].ToString(), tempEntity.Id.ToString())); } } //Bind the drop down to List DropDownList1.DataSource = dropDownListCollection; DropDownList1.DataTextField = "Text"; DropDownList1.DataValueField = "Value"; DropDownList1.DataBind();
Hope this helps..
MayankP
My Blog
Follow Me on Twitter- Proposed as answer by Mayank Pujara Monday, February 16, 2015 3:19 PM
- Marked as answer by HIMBAPModerator Wednesday, March 11, 2015 5:37 AM
Monday, February 16, 2015 3:19 PM -
Hi Vinay,
I have modified my above code to return drop down list from function, hope this helps..
protected void Page_Load(object sender, EventArgs e) { const string username = ""; const string password = ""; const string domain = ""; const string crmURL = ""; // Create a proxy connection to the CRM // Obtain the target organization's Web address and client log-on var crmUrl = new Uri(crmURL); var oCredentials = new ClientCredentials(); oCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(username, password, domain); var serviceProxy = new OrganizationServiceProxy(crmUrl, null, oCredentials, null); // This statement is required to enable early-bound type support. serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior()); serviceProxy.EnableProxyTypes(); var dropDownListCollection = GetDropDownList(serviceProxy); //Bind the drop down to List DropDownList1.DataSource = dropDownListCollection; DropDownList1.DataTextField = "Text"; DropDownList1.DataValueField = "Value"; DropDownList1.DataBind(); } public ListItemCollection GetDropDownList(OrganizationServiceProxy serviceProxy) { // create query against account var query = new QueryExpression { EntityName = "account", ColumnSet = new ColumnSet(new string[] { "accountid", "name" }) }; query.AddOrder("name", new OrderType()); query.Criteria.FilterOperator = LogicalOperator.And; EntityCollection accounts = serviceProxy.RetrieveMultiple(query); var dropDownListCollection = new ListItemCollection(); // Create list items from entity foreach (Entity tempEntity in accounts.Entities) { if (tempEntity.Contains("name")) { dropDownListCollection.Add(new ListItem(tempEntity["name"].ToString(), tempEntity.Id.ToString())); } } return dropDownListCollection; }
MayankP
My Blog
Follow Me on Twitter- Proposed as answer by Karishma Harjani Thursday, February 19, 2015 7:06 AM
- Marked as answer by HIMBAPModerator Wednesday, March 11, 2015 5:37 AM
Wednesday, February 18, 2015 4:28 PM
All replies
-
Hi Vinay,
Following example code for this, please provide CRM URL, username, password, domain as per your required in following code..
// Create a proxy connection to the CRM // Obtain the target organization's Web address and client log-on var crmUrl = new Uri(crmURL); var oCredentials = new ClientCredentials(); oCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(username, password, domain); var serviceProxy = new OrganizationServiceProxy(crmUrl, null, oCredentials, null); // This statement is required to enable early-bound type support. serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior()); serviceProxy.EnableProxyTypes(); // create query against account var query = new QueryExpression { EntityName = "account", ColumnSet = new ColumnSet(new string[] {"accountid", "name"}) }; query.AddOrder("name", new OrderType()); query.Criteria.FilterOperator = LogicalOperator.And; EntityCollection accounts = serviceProxy.RetrieveMultiple(query); var dropDownListCollection = new ListItemCollection(); // Create list items from entity foreach (Entity tempEntity in accounts.Entities) { if (tempEntity.Contains("name")) { dropDownListCollection.Add(new ListItem(tempEntity["name"].ToString(), tempEntity.Id.ToString())); } } //Bind the drop down to List DropDownList1.DataSource = dropDownListCollection; DropDownList1.DataTextField = "Text"; DropDownList1.DataValueField = "Value"; DropDownList1.DataBind();
Hope this helps..
MayankP
My Blog
Follow Me on Twitter- Proposed as answer by Mayank Pujara Monday, February 16, 2015 3:19 PM
- Marked as answer by HIMBAPModerator Wednesday, March 11, 2015 5:37 AM
Monday, February 16, 2015 3:19 PM -
Hi Mayank,
Thanks for the reply.....
Can you guide me how can i return the list in a function.
Many Thanks & Regards
Vinay
Wednesday, February 18, 2015 6:29 AM -
Hi Vinay,
I have modified my above code to return drop down list from function, hope this helps..
protected void Page_Load(object sender, EventArgs e) { const string username = ""; const string password = ""; const string domain = ""; const string crmURL = ""; // Create a proxy connection to the CRM // Obtain the target organization's Web address and client log-on var crmUrl = new Uri(crmURL); var oCredentials = new ClientCredentials(); oCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(username, password, domain); var serviceProxy = new OrganizationServiceProxy(crmUrl, null, oCredentials, null); // This statement is required to enable early-bound type support. serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior()); serviceProxy.EnableProxyTypes(); var dropDownListCollection = GetDropDownList(serviceProxy); //Bind the drop down to List DropDownList1.DataSource = dropDownListCollection; DropDownList1.DataTextField = "Text"; DropDownList1.DataValueField = "Value"; DropDownList1.DataBind(); } public ListItemCollection GetDropDownList(OrganizationServiceProxy serviceProxy) { // create query against account var query = new QueryExpression { EntityName = "account", ColumnSet = new ColumnSet(new string[] { "accountid", "name" }) }; query.AddOrder("name", new OrderType()); query.Criteria.FilterOperator = LogicalOperator.And; EntityCollection accounts = serviceProxy.RetrieveMultiple(query); var dropDownListCollection = new ListItemCollection(); // Create list items from entity foreach (Entity tempEntity in accounts.Entities) { if (tempEntity.Contains("name")) { dropDownListCollection.Add(new ListItem(tempEntity["name"].ToString(), tempEntity.Id.ToString())); } } return dropDownListCollection; }
MayankP
My Blog
Follow Me on Twitter- Proposed as answer by Karishma Harjani Thursday, February 19, 2015 7:06 AM
- Marked as answer by HIMBAPModerator Wednesday, March 11, 2015 5:37 AM
Wednesday, February 18, 2015 4:28 PM