Sharepoint webpart combobox of lists

孤街醉人 提交于 2019-12-02 04:14:01

What you are looking for is called a Toolpart. Take a look at this example for a tutorial on how to create one.

Overall, your general steps will be:

  1. Create your custom Toolpart class inheriting from Microsoft.SharePoint.WebPartPages.ToolPart
  2. In your custom Toolpart, override CreateChildControls, write the code to iterate over the lists in your SPWeb, and add those to a DropDownList
  3. In your webpart, override GetToolParts and add your custom ToolPart so that it shows up in the right hand side

It sounds like you want to create a custom editor part. In the part you would have one dropdown that shows the names of the lists (you probably want to filter hidden and empty lists) and, when an item is selected from the list, a second dropdown shows the Title column of the items from the selected list.

Here's some code (edited here, so it will need to be cleaned up) to help you get started:

protected Page_Load(...)
{
    if (IsPostBack) return;

    var web = SPContext.Current.Web;
    var query = from list in web.Lists
                where list.Hidden == false && list.ItemCount == 0
                select list;

    DropDownList1.DataSource = query;
    DropDownList1.DataTextField = "Title";
    DropDownList1.DataBind();
}

protected DropDownList1_SelectedIndexChanged(...)
{
    var web = SPContext.Current.Web;
    var listName = DropDownList1.Text;
    var list = web.Lists[listName];
    var table = list.Items.GetDataTable();
    DropDownList2.DataSource = table;
    DropDownList2.DataTextField = "Title";
    DropDownList2.DataValueField = "ID";
    DropDownList2.DataBind();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!