Update 26-11-2008
Do not disable the textbox, otherwise the value you selected will never end up in the text box. (see code below).
In a lot of places in SharePoint you can select a list (or other objects like a site) in a nice popup dialog. It turns out it is really easy to do that yourself. In this example I will show how to let your users select a list in the webpart toolpart. The code is attached to this post (see link at the bottom of the post).
The webpart itself is nothing special, it just renders the relative url to the list that the user has selected:
In the CreateChildControls of the EditorPart, we will add a readonly textbox and a button to the controls collection. When clicked, the button calls the javascript function that we will add later.
private TextBox _listUrl;
private Button _selectList;
public ListSelectEditorPart(string webPartID)
{
this.ID = "ListSelectEditorPart" + webPartID;
this.Title = "Select a list";
}
protected override void CreateChildControls()
{
base.CreateChildControls();
_listUrl = new TextBox();
//_listUrl.Enabled = false; 26-11-2008 -> Commented this line out!
Controls.Add(_listUrl);
_selectList = new Button();
_selectList.OnClientClick = "javascript:launchPicker();";
_selectList.Text = "...";
Controls.Add(_selectList);
}
Please note the ID that I set in the constructor. This blog post contains an explanation of why I do that. When you create an editorpart you will need to implement SyncChanges and ApplyChanges to read the value to your control and to store your values in the webpart:
public override void SyncChanges()
{
EnsureChildControls();
ListSelectWebPart webPart = WebPartToEdit as ListSelectWebPart;
if (webPart != null)
{
_listUrl.Text = webPart.ListUrl;
}
}
public override bool ApplyChanges()
{
EnsureChildControls();
ListSelectWebPart webPart = WebPartToEdit as ListSelectWebPart;
if (webPart != null)
{
webPart.ListUrl = _listUrl.Text;
}
return true;
}
In the OnLoad for the editorpart, we register the script that will show the popup. You also need to include the PickerTreeDialog.js file (which is localized, so in an en-US site, it is in the 1033 folder).
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
string webLocale = SPContext.Current.Web.Locale.LCID.ToString();
Page.ClientScript.RegisterClientScriptInclude("PickerTreeDialog", string.Format("/_layouts/{0}/PickerTreeDialog.js", webLocale));
RegisterSelectListScript();
}
private void RegisterSelectListScript()
{
StringBuilder launchPicker = new StringBuilder();
launchPicker.Append("<SCRIPT LANGUAGE='JavaScript' >");
launchPicker.Append("function launchPicker()\n");
launchPicker.Append("{\n");
launchPicker.Append(" var listurlfield = document.getElementById(\"" + _listUrl.ClientID + "\");\n");
launchPicker.AppendFormat(" var defaulturl = '{0}';\n", SPContext.Current.Web.ServerRelativeUrl);
launchPicker.Append(" var url = defaulturl;\n");
launchPicker.Append(" if(listurlfield != null && listurlfield.value != '')\n");
launchPicker.Append(" {\n");
launchPicker.Append(" url = listurlfield.value.substring(0,listurlfield.value.lastIndexOf('/'));");
launchPicker.Append(" }\n");
launchPicker.Append(" var callback=function(arr)\n");
launchPicker.Append(" {\n");
launchPicker.Append(" if(arr==null || arr==undefined)\n");
launchPicker.Append(" return;\n");
launchPicker.Append(" var site=arr[1];\n");
launchPicker.Append(" var list=arr[2];\n");
launchPicker.Append(" if(list != '')\n");
launchPicker.Append(" {\n");
launchPicker.Append(" listurlfield.value = site + (site == '/' ? '' : '/') + list;\n");
launchPicker.Append(" }\n");
launchPicker.Append(" " + Page.ClientScript.GetPostBackEventReference(_listUrl, string.Empty) + ";\n");
launchPicker.Append(" }\n");
launchPicker.Append("LaunchPickerTreeDialog(\'CbqPickerSelectListTitle','CbqPickerSelectListTitle','listsOnly',\"\",url,null,\"\",\"\",\"/_layouts/images/generic.png\", 0, callback );\n");
launchPicker.Append("}\n");
launchPicker.Append("</SCRIPT>");
if (!Page.ClientScript.IsClientScriptBlockRegistered("launchPicker"))
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "launchPicker", launchPicker.ToString());
}
After building and installing your webpart and opening the editorpart, your users will see this after they click the button:
You can find the code in this zip file.