Friday, April 10, 2009

Creating Custom Web part in MOSS and add custom properties to the Tool part

You can create a Custom web part for implementing the custom functionalites in SharePoint by deriving the class from Microsoft.SharePoint.WebPartPages.webPart Class.

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;

public class CustomWebPart : WebPart
{
protected override void CreateChildControls()
{
// to do : Create the child controls of the web part and add to the controls collection
Button btncntrol = new Button();
this.Controls.Add(btncntrol);
}
public override void RenderControl(HtmlTextWriter writer)
{
// render control here
}
}

CreateChildControls and RenderControl methods are used to add controls to the web part also apply styles to the web part.

To add custom properties to the Tool Part in Modified Shared Web Part

[Browsable(true), Category("Behavior"), DefaultValue(false),
WebPartStorage(Storage.Shared),
FriendlyName("Enable values "),
Description("To modify values")]
public bool ShowCustomProperty
{
get
{
return showCustomproperty;
}
set
{
showCustomproperty = value;
}
}

To have more customization in the tool part like “on click of image a dialog should open”. Override GetToolPart method in web part class.

When the end-user chooses to modify a Web Part, the framework will call the GetToolParts method of the Web Part class. By overriding this method, I can control the order and appearance of the Tool Parts.

public override Microsoft.SharePoint.WebPartPages.ToolPart[] GetToolParts()
{
// returns array of tool part. Custom propery is also added here...
}

To learn more about creation of web part and connectable web parts in MOSS Click Here

No comments: