Thursday, September 23, 2010

How to develop Custom Tool Part?

What is Tool Part?

When we modified webpart present on SharePoint page, at the time what appears on right hand side is panel. We can specify webpart properties there.
If you want to add custom property to that tool part panel, your webpart should be inherits with “Microsoft.SharePoint.WebpartPages.webpart” instead of “System.Web.UI.WebControls.Webparts.Webpart”.

Let us see example:-
You want to add “Customer name” textbox to tool pane.
Step 1: -
Declare variable for “Customer name” property and add the code within your webpart class.
private string strCustomerName = string.Empty;

[Browsable(false),//Display the property in property pane
Category("CustomToolPart"),//Create a Customer Details category in property pane
DefaultValue(""),//Assign a default value
WebPartStorage(Storage.Personal),//Make available in both personal and shared mode
Personalizable(PersonalizationScope.User),
FriendlyName("Customer Name"),//The caption display in property pane
Description("The name of the customer")]//The tool tip

public string CustomerName
{

get
{
return strCustomerName;
}
set
{
strCustomerName = value;
}
}
If you want to add more properties, you can declare in your webpart class.
Now override one method  which is GetToolParts() on ToolPart method.
public override ToolPart[] GetToolParts()
{
ToolPart[] toolparts = new ToolPart[3];

WebPartToolPart wptp = new WebPartToolPart();

CustomPropertyToolPart custom = new CustomPropertyToolPart();

toolparts[0] = wptp;

toolparts[1] = custom;

toolparts[2] = new {toolpartclass}

return toolparts;
}

In above code “WebPartToolPart” having default properties available in sharepoint and “CustomPropertyToolPart” having customproperty that you want to added.

Ok, let’s develop and create Tool Part class which will have RenderToolPart and ApplyChanges method.

Create a class which inherits from Microsoft.Sharepoint.WebpartPages.ToolPart.

Declare variable which will be name of control render in tool pane.

private string strHTMLinputControlName = "Mycontrol";

protected override void RenderToolPart(HtmlTextWriter output)

{

// Establish a reference to the Web Part.

// CustomWebPart is the web part class name.

{webpartclassname} customWebPart =

(webpartclassname)this.ParentToolPane.SelectedWebPart; 

//Create the input control

output.Write("Enter the customer name: ");

output.Write("<input name= '" + strHTMLinputControlName); 

output.Write("' type='text' value='" + 

SPEncode.HtmlEncode(customWebPart.CustomerName) + "'><br>"); 

}

We have first taken the reference of the webpart class for which we are creating this tool part. So parentToolPane.SelectedWebPart will be the webpart for which we are creating tool Part. Add the following method.

public override void ApplyChanges()
{

// apply property values here

//Get a reference to the web part class

{webpartclassname} cw1 =

(webpartclassname)this.ParentToolPane.SelectedWebPart;

//Pass the custom text to web part custom property

cw1.CustomerName = Page.Request.Form[strHTMLinputControlName];

}

The “ApplyChanges” method which will be called when we press OK. The webpart is now completed with your custom tool pane property. 

Deploy the webpart to your site and add webpart on sharepoint page.
When you edit webpart properties, in tool pane you will find property you have added previously.

Happy coding………………..




No comments:

Post a Comment