Overview:

The following post will help you in binding the SharePoint people field names to DropDownList using C# code.

List Details:


Code:

  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using System.Web.UI.WebControls.WebParts;
  5. using Microsoft.SharePoint;
  6. using System.Collections.Specialized;
  7. using System.Data;
  8. using System.Collections;
  9. using System.Text;
  10. namespace RK_Solution.VisualWebPart1
  11. {
  12. public partial class VisualWebPart1UserControl: UserControl
  13. {
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. SPSite site = SPContext.Current.Site
  17. using(SPWeb web = site.OpenWeb())
  18. {
  19. SPList issueRepList = web.Lists["Issue Representative"];
  20. SPListItemCollection issueRepItems = issueRepList.Items;
  21. DataTable dtUsers = new DataTable();
  22. dtUsers.Columns.Add("User", typeof(string));
  23. foreach(SPListItem item in issueRepItems)
  24. {
  25. if (item["IssueRepId"].ToString() == "5")
  26. {
  27. SPFieldUserValueCollection userCol = new SPFieldUserValueCollection
  28. (SPContext.Current.Web, item["Representatives"].ToString());
  29. foreach(SPFieldUserValue usrItm in userCol)
  30. {
  31. string temp = usrItm.User.Name.ToString();
  32. dtUsers.Rows.Add(temp);
  33. }
  34. ddlIssueRep2.DataSource = dtUsers;
  35. ddlIssueRep2.DataTextField = "User";
  36. ddlIssueRep2.DataValueField = "User";
  37. ddlIssueRep2.DataBind();
  38. }
  39. }
  40. }
  41. }
  42. }
  43. }

Output: