Skip to main content

Weblog Ton Stegeman [MVP]

Go Search
Home
  

ODC 2008
If you have a question or suggestion, please contact me through Windows Live Messenger.
My status: .

If I am not online, please send me an e-mail.
Weblog Ton Stegeman [MVP] > Posts > Selecting a SharePoint list in a webpart toolpart
Selecting a SharePoint list in a webpart toolpart

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:

image

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:

image

You can find the code in this zip file.

Comments

5 star post!

Thanks Ton, this is just what I was looking for!
at 8/14/2009 2:23 AM

thx

Just wondering is there any optsion where I can select particular/image or document instead of the name of the list
at 11/24/2009 10:12 AM

Selecting image/document

I think you can do this by playing with the options of the LaunchPickerTreeDialog javascript function. Probably the 3rd argument is the one you're looking for. Don't know the correct value though.
Ton Stegeman at 12/4/2009 8:11 AM

Great Post

Helped me a lot!
at 1/22/2010 7:53 AM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Your city *


Type the name of the city you live in (making it easier to handle spam...)

CurrentDate *

Select the current date (see if this gives me fewer spam...)
Attachments

 Links

  SharePoint Object on CodePlex
  Screencast introducing SharePoint Objects
  Content by Type and Filter Web Parts on CodePlex
  Archive
  Archive (Calendar)

 My Latest Blog Posts

Scripting SharePoint 2007 setup: choices and conceptsUse SHIFT+ENTER to open the menu (new window).
Adventures in Visual Studio 2010: Migrate the Content By Type web part to SharePoint 2010Use SHIFT+ENTER to open the menu (new window).
Register SharePoint themes by using a featureUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010 development on Windows 2008 Server R2 – Getting StartedUse SHIFT+ENTER to open the menu (new window).
New release SharePoint Objects: features and groupsUse SHIFT+ENTER to open the menu (new window).
Constructing the url to the SharePoint Edit Permissions pageUse SHIFT+ENTER to open the menu (new window).
Screencast: introduction to SharePoint ObjectsUse SHIFT+ENTER to open the menu (new window).
SharePoint 2007 and Reporting ServicesUse SHIFT+ENTER to open the menu (new window).
SharePoint Objects – Insight in usage of your SharePoint artifactsUse SHIFT+ENTER to open the menu (new window).
SharePoint 2007 Custom list schema and the Content Query Web PartUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010 Silverlight Client Object Model – ExecuteQuery vs ExecuteQueryAsyncUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010, the Client Object Models and Bing MapsUse SHIFT+ENTER to open the menu (new window).
Having fun with SharePoint 2010, Silverlight 3 and Bing MapsUse SHIFT+ENTER to open the menu (new window).
Connecting TFS 2010 projects to SharePoint sitesUse SHIFT+ENTER to open the menu (new window).
Adding a database to the SharePoint database Server using SPDatabaseUse SHIFT+ENTER to open the menu (new window).