Webpart comes with a default tool part which contains the configuration settings for the webpart. But often we have requirement to create the custom toolpart for the webpart having certain .net controls like dropdown list ,treeview etc which fetches data from the list or any other source and performs certain functionality based on the logic.
Webpart will look like this
Here is step by step process for creating webpart with custom toolpart:
1) Open visual studio
2) Create a new project with type class library
3) Inherit the class with Microsoft.SharePoint.WebPartPages.WebPart class
Below is the code for the webpart class
----------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
namespace CustomToolPartWP
{
public class CustomWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
private string ddlvalue = string.Empty;
[Browsable(false), Category("Miscellaneous"),
WebPartStorage(Storage.Personal),
FriendlyName("ListValue"), Description("Text Property")]
public string DropDownListValue
{
get
{
return ddlvalue;
}
set
{
ddlvalue = value;
}
}
// Gets the custom tool parts for this Web Part by
/// overriding the GetToolParts method of the WebPart
/// base class. You must implement custom tool parts in
/// a separate class that derives from
/// Microsoft.SharePoint.WebPartPages.ToolPart.
///
///
/// An array of references to ToolPart objects.
///
public override ToolPart[] GetToolParts()
{
ToolPart[] toolparts = new ToolPart[3];
WebPartToolPart wptp = new WebPartToolPart();
CustomPropertyToolPart custom = new CustomPropertyToolPart();
toolparts[0] = custom;
toolparts[1] = wptp;
// This is the custom ToolPart.
toolparts[2] = new CustomToolPartWP.CustomToolPart();
return toolparts;
}
/// Render this Web Part to the output parameter specified.
/// The HTML writer to write out to
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
writer.Write("Value Selected from dropdown is " + this.ddlvalue +
"); }
}
}
------------------------------------------------------------------
Lets discuss some important features of the webpart .Here we have declared one “DropDownListValue” ,it has some attributes like
a) Browsable : To appear this property in the wepart
b) Category: It shows the label name under which our custom control should render
c) WebPartStorage : There are two types of storage .One is personal ,this means that the value stored are specific to the logged in user ,other is shared ,in this saved value will be visible to all the users.
Once the webpart is done we need to add the class for toolpart which contains the code for custom toolpart. Follow these steps:
1) Add class to the solution
2) Inherit this class with Microsoft.SharePoint.WebPartPages.ToolPart
This is the sample code for the Toolpart which renders once dropdown list and selecting some value and clicking on Apply or Ok sets the value in the webpart.
------------------------------------------------------------------------
using System;------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
namespace CustomToolPartWP
{
///
/// Description of the toolpart. Override the GetToolParts method
/// in your Web Part class to invoke this toolpart. To establish
/// a reference to the Web Part the user has selected, use the
/// ParentToolPane.SelectedWebPart property.
class CustomToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
{
private string inputname;
DropDownList dropdownList;
// an event handler for the Init event
private void CustomToolPart_Init(object sender,
System.EventArgs e )
{
inputname = this.UniqueID + "message";
}
///
/// Constructor for the class. A great place to set Set
/// default values for additional base class properties
/// here.
///
public CustomToolPart()
{
// Set default properties
this.Title="Select Value";
this.Init += new EventHandler(CustomToolPart_Init);
}
/// Called by the tool pane to apply property changes to
/// the selected Web Part.
public override void ApplyChanges()
{
CustomWebPart customWP = (CustomWebPart)
this.ParentToolPane.SelectedWebPart;
// Send the custom text to the Web Part.
customWP.DropDownListValue = dropdownList.SelectedItem.Value;
}
protected override void CreateChildControls()
{
//base.CreateChildControls();
dropdownList = new DropDownList();
dropdownList.Items.Add("item1");
dropdownList.Items.Add("item2");
dropdownList.Items.Add("item3");
dropdownList.Items.Add("item4");
CustomWebPart customWP = (CustomWebPart)
this.ParentToolPane.SelectedWebPart;
dropdownList.Items.FindByText(customWP.DropDownListValue).Selected = true;
this.Controls.Add(dropdownList);
}
/// If the ApplyChanges method succeeds, this method is
/// called by the tool pane to refresh the specified
/// property values in the toolpart user interface.
public override void SyncChanges()
{
// sync with the new property changes here
CustomWebPart customWP = (CustomWebPart)
this.ParentToolPane.SelectedWebPart;
customWP.DropDownListValue = dropdownList.SelectedItem.Value;
}
/// Called by the tool pane if the user discards changes
/// to the selected Web Part.
public override void CancelChanges()
{
}
/// Render this Tool part to the output parameter
/// specified.
/// The HTML writer to write out to
protected override void RenderToolPart(HtmlTextWriter output)
{
// Establish a reference to the Web Part.
CustomWebPart customWebPart = (CustomWebPart)
this.ParentToolPane.SelectedWebPart;
output.Write("Select value from drop down: ");
dropdownList.RenderControl(output);
}
}
}
After this compile the solution and deploy the webpart and put the dll in GAC or the bin folder of the webapplication
And your webpart is ready to use with custom toolpart.
If you have queries than do post.
Your job is done J
Enjoy!!!
No comments:
Post a Comment