Wednesday, October 13, 2010

Creating Custom Web Part ToolPane

To create a custom toolpane is pretty simple, it's quite simple if you think about. All you need is an extra class that will create your toolpane.

I created a web part that had a custom toolpane that was used to display list in a site with checkboxes.

This toolpane allows users to select which list they wish to use on the site. Please see code below, and if you know of any better ways to do this please let me know:

WEB PART CODE EXAMPLE

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using SMC.ClassLibrary;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;


namespace OM.FILTER.JQUERY
{
[Guid("67e99305-67bc-444d-9f9d-0c3c53d3dfe8")]
[CLSCompliant(false)]
public class FilterJQuery : Microsoft.SharePoint.WebPartPages.WebPart
{

public FilterJQuery()
{

}

protected override void CreateChildControls()
{
base.CreateChildControls();
}

public override ToolPart[] GetToolParts()
{
List _toolParts = null;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite _spSite = new SPSite("url"))
{
using (SPWeb _spWeb = _spSite.OpenWeb())
{
CustomToolPart _customPart = new CustomToolPart("url");
_customPart.Title = "All Lists";

_toolParts = new List(base.GetToolParts());
_toolParts.Insert(0, _customPart);
}
}
});

return _toolParts.ToArray();
}


[CLSCompliant(false)]
[WebBrowsable(false),
Personalizable(PersonalizationScope.Shared),
Category("Custom Settings")]
public string _collectionSelected
{
get;
set;
}
}
}

TOOL PANE CODE EXAMPLE

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using System.Data;
using SMC.ClassLibrary;

namespace OM.FILTER.JQUERY
{
class CustomToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
{
private CheckBoxList _spLists = new CheckBoxList();

private CheckBoxList chkBox
{
get { return _spLists; }
set { _spLists = value; }
}


private String _SiteURL;

public CustomToolPart(String _webURL)
{
this._SiteURL = _webURL;
}

protected override void CreateChildControls()
{
base.CreateChildControls();

BindToolPart();
}

private void BindToolPart()
{
chkBox.DataSource = Helper.GetAllListTitles(_SiteURL);
chkBox.DataTextField = "Title";
chkBox.DataValueField = "ID";
chkBox.DataBind();

FilterJQuery _jQuery = (FilterJQuery)this.ParentToolPane.SelectedWebPart;

if (_jQuery._collectionSelected != null)
{
List arrayList = Helper.SplitString(_jQuery._collectionSelected);

foreach (ListItem _item in chkBox.Items)
{
for (int i = 0; i < arrayList.Count; i++)
{
if (_item.Value == arrayList[i])
{
_item.Selected = true;
}
}
}
}
this.Controls.Add(chkBox);
}

public override void ApplyChanges()
{
base.ApplyChanges();

FilterJQuery parentWebPart = (FilterJQuery)this.ParentToolPane.SelectedWebPart;

string _iList = "";

foreach (ListItem _item in chkBox.Items)
{
if (_item.Selected)
{
_iList += _item.Value + ";";
}
}
parentWebPart._collectionSelected = _iList;

chkBox.EnableViewState = true;
EnableViewState = true;
this.SaveViewState();
this.SaveControlState();
}
}
}

Hope this helped you out as it sure helped me out a whole lot, let me know if you have any questions and I will try and respond to it asap.

No comments:

Post a Comment