This is an easy way for retrieving a list name , if you needed in a webpart property.
We need to create an extra class;
using System;
using System.Text;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using ZorgInspectie.Intranet.WebParts;
Namespace Intranet
{
class ListSelectEditorPart : EditorPart
{
private TextBox txtListUrl;
private Button _selectList;
private String WebPartName = String.Empty;
public ListSelectEditorPart(string webPartID, string webpartname)
{
this.ID = "ListSelectEditorPart" + webPartID;
this.Title = "Select a list";
this.WebPartName = webpartname;
}
protected override void CreateChildControls()
{
base.CreateChildControls();
txtListUrl = new TextBox();
//_listUrl.Enabled = false;
Controls.Add(txtListUrl);
_selectList = new Button();
_selectList.OnClientClick = "javascript:launchPicker();";
_selectList.Text = "...";
Controls.Add(_selectList);
}
public override void SyncChanges()
{
EnsureChildControls();
if (WebPartName.Equals("HomePageNews"))
{
HomePageNews webPart = WebPartToEdit as HomePageNews;
if (webPart != null)
{
txtListUrl.Text = webPart.ListUrl;
}
}
else if (WebPartName.Equals("BulletinBoard"))
{
BulletinBoard webPart = WebPartToEdit as BulletinBoard;
if (webPart != null)
{
txtListUrl.Text = webPart.ListUrl;
}
}
}
public override bool ApplyChanges()
{
EnsureChildControls();
if (WebPartName.Equals("HomePageNews"))
{
HomePageNews webPart = WebPartToEdit as HomePageNews;
if (webPart != null)
{
webPart.ListUrl = txtListUrl.Text;
}
}
else if (WebPartName.Equals("BulletinBoard"))
{
BulletinBoard webPart = WebPartToEdit as BulletinBoard;
if (webPart != null)
{
webPart.ListUrl = txtListUrl.Text;
}
}
return true;
}
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(\"" + txtListUrl.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(txtListUrl, 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());
}
}
}
The next lines are the code of my code file for my webpart;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using ZorgInspectie.Intranet.UserControls;
using Microsoft.SharePoint.WebPartPages;
using System.ComponentModel;
namespace ZorgInspectie.Intranet.WebParts
{
[ToolboxItemAttribute(false)]
public class BulletinBoard : Microsoft.SharePoint.WebPartPages.WebPart, IWebEditable
{
private bool _error = false;
public string _listUrl;
private const string _ascxPath = @"~/_controltemplates/ZI_Intranet/BulletinBoard.ascx";
[Browsable(false),
Personalizable(PersonalizationScope.Shared)]
public string ListUrl
{
get { return _listUrl; }
set { _listUrl = value; }
}
/// <summary>
/// Create all your controls here for rendering.
/// Try to avoid using the RenderWebPart() method.
/// </summary>
///
protected override void CreateChildControls()
{
if (!_error)
{
try
{
this.Title = "Bulletin Board";
Control control = Page.LoadControl(_ascxPath);
Controls.Add(control);
//bulletinBoardUserControl = (UserControl)Page.LoadControl(@"~/_controltemplates/ZI_Intranet/BulletinBoard.ascx");
//this.Controls.Add(bulletinBoardUserControl);
}
catch (Exception ex)
{
HandleException(ex);
}
}
}
#region IWebEditable Members
EditorPartCollection IWebEditable.CreateEditorParts()
{
List<EditorPart> editors = new List<EditorPart>();
// Add the base editor parts
EditorPartCollection baseParts = base.CreateEditorParts();
foreach (EditorPart basePart in baseParts)
editors.Add(basePart);
editors.Add(new ListSelectEditorPart(this.ID, "BulletinBoard"));
return new EditorPartCollection(editors);
}
object IWebEditable.WebBrowsableObject
{
get { return this; }
}
#endregion
}
}
It's very easy and very handy.
grtz