locked
Populate ASP.NET Drop Down List from SharePoint Liast

    Question

  • OK. Is this as simple as it sounds?

    I am looking at populating a ASP.NET Drop Down List from a SharePoint List using the WSI?

    Can anyone please point me in the correct direction?  I see lots of examples on Google in InfoPath, but not C# or VB.

    Thanks
    Monday, February 01, 2010 2:07 AM

Answers

  • Hi Daren, You can used these values to populated either Dropdown or Listbox as per you requirement.
    SPList list = oWebsite.Lists["SPList_Name"];
    SPFieldChoice choice = (SPFieldChoice)list.Fields["Event Name"];
    
    foreach(string s in choice.Choices)
     Response.Write(s);


    Regards, Avinash | avinashkt.blogspot.com
    • Marked as answer by Chengyi Wu Friday, February 12, 2010 2:10 AM
    Monday, February 01, 2010 7:02 PM
  • Hi Daren,

    I'm not sure what does WSI mean, but as per populating the drop-down list in ASP .NET from a SharePoint list using C# here's how you can do it:

    SPList list = ...;
    SPListItemCollection items = list.Items; // or some other logic like list.GetItems(SPQuery) if you use a CAML query
    DropDownList drp = ...; // or drp.Items.Clear();
    foreach (SPListItem item in items)
    {
        drp.Items.Add(item.Title); // or any other field you like
    }

    Hope that helps.

    Best wishes,
    Slava G
    http://wyldesolutions.com
    http://wyldesharepoint.blogspot.com
    • Marked as answer by Chengyi Wu Friday, February 12, 2010 2:11 AM
    Monday, February 01, 2010 7:49 AM
  • Hello,

    You can connect to your sharepoint lists through the lists.asmx webservice.
    More information is to be found here:
    http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems.aspx

    An example of how to access this through code is here (this is to create a doclib, but the same principle for reading items):
    http://www.howtosharepoint.net/tag/lists-asmx/

    Regards,
    Jeroen
    My blog on WSS / MOSS development is found at http://jebass.blogspot.com
    • Marked as answer by Chengyi Wu Friday, February 12, 2010 2:11 AM
    Monday, February 01, 2010 8:02 AM
  • hi,

    if u want to use web services u must first add a web reference to lists.asmx ...then is pretty easy ..i've named the web reference RCPILists

     

     

    Dim RCPILists As New RCPILists.Lists
    RCPILists.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials

    Dim ds As New DataSet
    Dim node As XmlNode = RCPILists.GetListItems("your list name", String.Empty, Nothing, Nothing, 0, Nothing, Nothing)
    Dim xr As New XmlTextReader(node2.OuterXml, XmlNodeType.Element, Nothing)
    ds.ReadXml(xr)
    Dim i As Integer

    For i = 0 To ds.Tables(1).Rows.Count() - 1
    DropDownList1.Items.Add(ds.Tables(1).Rows(i).Item("the index of the column u want to use"))
    Next

    • Marked as answer by Chengyi Wu Friday, February 12, 2010 2:11 AM
    Monday, February 01, 2010 9:27 AM

All replies

  • Hi Daren,

    I'm not sure what does WSI mean, but as per populating the drop-down list in ASP .NET from a SharePoint list using C# here's how you can do it:

    SPList list = ...;
    SPListItemCollection items = list.Items; // or some other logic like list.GetItems(SPQuery) if you use a CAML query
    DropDownList drp = ...; // or drp.Items.Clear();
    foreach (SPListItem item in items)
    {
        drp.Items.Add(item.Title); // or any other field you like
    }

    Hope that helps.

    Best wishes,
    Slava G
    http://wyldesolutions.com
    http://wyldesharepoint.blogspot.com
    • Marked as answer by Chengyi Wu Friday, February 12, 2010 2:11 AM
    Monday, February 01, 2010 7:49 AM
  • Hello,

    You can connect to your sharepoint lists through the lists.asmx webservice.
    More information is to be found here:
    http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems.aspx

    An example of how to access this through code is here (this is to create a doclib, but the same principle for reading items):
    http://www.howtosharepoint.net/tag/lists-asmx/

    Regards,
    Jeroen
    My blog on WSS / MOSS development is found at http://jebass.blogspot.com
    • Marked as answer by Chengyi Wu Friday, February 12, 2010 2:11 AM
    Monday, February 01, 2010 8:02 AM
  • hi,

    if u want to use web services u must first add a web reference to lists.asmx ...then is pretty easy ..i've named the web reference RCPILists

     

     

    Dim RCPILists As New RCPILists.Lists
    RCPILists.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials

    Dim ds As New DataSet
    Dim node As XmlNode = RCPILists.GetListItems("your list name", String.Empty, Nothing, Nothing, 0, Nothing, Nothing)
    Dim xr As New XmlTextReader(node2.OuterXml, XmlNodeType.Element, Nothing)
    ds.ReadXml(xr)
    Dim i As Integer

    For i = 0 To ds.Tables(1).Rows.Count() - 1
    DropDownList1.Items.Add(ds.Tables(1).Rows(i).Item("the index of the column u want to use"))
    Next

    • Marked as answer by Chengyi Wu Friday, February 12, 2010 2:11 AM
    Monday, February 01, 2010 9:27 AM
  • Hi Daren, You can used these values to populated either Dropdown or Listbox as per you requirement.
    SPList list = oWebsite.Lists["SPList_Name"];
    SPFieldChoice choice = (SPFieldChoice)list.Fields["Event Name"];
    
    foreach(string s in choice.Choices)
     Response.Write(s);


    Regards, Avinash | avinashkt.blogspot.com
    • Marked as answer by Chengyi Wu Friday, February 12, 2010 2:10 AM
    Monday, February 01, 2010 7:02 PM
  • Hi Daren_Hawes,

    I'm not sure about WSI, but here's how you use ASP.NET to populate a dropdownlist with values from a SharePoint List.

    Assuming you have a SharePoint Site you want to use, a List name, and a dropdownlist called droSite, add this code to your Page_Load event:

    if (!Page.IsPostBack)
            {
                using (SPSite site = new ("http://yoursharepointsite"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists["NameOfYourList"];
                        dropSite.DataSource = list.Items;
                        dropSite.DataValueField = "Title"; // List field holding value - first column is called Title anyway!
                        dropSite.DataTextField = "Title"; // List field holding name to be displayed on page 
                        dropSite.DataBind();
                    }
                }
            }
    Hope that helps mate!,

    Ashok Jingar
    Friday, February 19, 2010 2:33 PM