asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search










Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Develop and Customize Web Parts with Custom Tool Parts
By Gayan Peiris
Rating: 3.7 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

    There are several ways a developer can introduce customization to a SharePoint Web Part. The default properties and custom properties developers create are available in the default property pane, making their use transparent to the user. If the default property pane functionality is not rich enough, developers can create a custom user interface called a Tool Part. This adds a sophisticated interface for manipulating the appearance and functionality of the custom property. Tool Parts provide an interface for Web Part properties well beyond the capabilities of the default property pane.

    The GetToolParts Method

    To add custom Tool Parts to the property pane of the Web Part, the developer must overwrite the GetToolParts method in the Web Part base class. The GetToolParts method returns an array of references to the ToolPart objects that will be included in the property pane for the Web Part. The ToolPart objects are rendered according to the order in the array. There are two standard ToolPart objects: the WebPartToolPart and CustomPropertyToolPart normally render at runtime by default. If the developer overwrites the GetToolParts method with the custom Tool Part, the standard ToolPart objects should manually be added back to the array of references.

    The CustomPropertyToolPart Object

    This object displays the custom properties developed for the Web Part. The developer has to include a reference to the CustomPropertyToolPart object in the array of references returned by the GetToolParts method.

    The WebPartToolPart Object

    This object will display all the base class properties of the Web Part.

    The Visual Studio .NET environment supports the development of Web Parts for SharePoint Portal Server. The Web Part Template provided on MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_sp2003_ta/html/sharepoint_webparttemplates.asp) provides an excellent starting point for the structure of GetToolParts method and template of a custom Tool Part class.

    ///<summary>

    ///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.

    ///</summary>

    ///<returns>An array of references to ToolPart objects.</returns>

    //public override ToolPart[] GetToolParts()

    //{

    //    ToolPart[] toolparts = new ToolPart[2];

    //    WebPartToolPart wptp = new WebPartToolPart();

    //    CustomPropertyToolPart custom = new CustomPropertyToolPart();

    //    toolparts[0] = wptp;

    //    toolparts[1] = custom;

    //    return toolparts;

    //}

    Figure 1: The GetToolParts method available by default in the Web Part template.

    As you can see, the GetToolParts method appears fully commented without having any impact on the Web Part. The default ToolPart objects, WebPartToolPart and CustomPropertyToolPart are displayed automatically.

    It is important to understand how to develop custom Web Parts before commencing any coding. Please refer to the article "Developing Web Parts for SharePoint 2003 in VS.NET" for more information regarding developing Web Parts (http://www.devx.com/dotnet/Article/17518).

    Code Example

    In this example, a simple Tool Part class will be created that exposes a Web Part property. The custom Tool Part will display in the default property pane. The user will be able to update the Web Part property through the custom Tool Part.

    It is a two step process to develop a custom Tool Part.

    1. Create the custom Tool Part class
    2. Add a GetToolParts method to the Web Part class

    1. Create the custom Tool Part class.

    First create a Web Part Library solution using the Web Part Library template.

    Figure 2: Web Part Library project available from the template.

    This will create a Web Part class. In the Web Part class then create a custom property. Please refer to the article "Create Custom Properties for a Web Part" (http://www.15seconds.com/issue/040219.htm).

    An example of a simple string custom property where you can enter a string value is displayed below,

    ///<summary>

    /// This property will get and set a string value as a customer name

    /// This will be available in a text box.

    ///</summary>

    [Browsable(false),//Display the property in property pane

    Category("CustomToolPartEx1"),//Create a Customer Details category in property pane

    DefaultValue(""),//Assign a default value

    WebPartStorage(Storage.Personal),//Make available in both personal and shared mode

    FriendlyName("Customer Name"),//The caption display in property pane

    Description("The name of the customer")]//The tool tip

    publicstring CustomerName

    {

          get

          {

                return strCustomerName;

          }

          set

          {

                strCustomerName = value;

          }

    }

    Figure 3: Custom property created in the Web Part.

    Next you need to add a new Tool Part class to the Web Part project. In Solution Explorer, right click on the project file, select Add and click Add New Item. Select the Tool Part template as displayed below. Provide a name for the Tool Part template and click Open.

    Figure 4: Selecting the Tool Part template.

    Make sure the namespace of the both Web Part class and Tool Part class are the same for easy access. The Tool Part class will display as follows:

    Figure 5: Tool Part class and namespace.

    The CustomToolPart1 class inherits from ToolPart class using the Microsoft.SharePoint.WebPartPages namespace.

    To display the Web Part custom property value in Tool Part class, the code to the RenderToolPart method in the Tool Part class must be added. This method renders the Tool Part to the Web page.

    To obtain a reference to the Web Part to access or modify the properties, the ToolPart class has an accessor property called ParentToolPane that returns a reference to the ToolPane object. The ToolPane object class determines which Web Part is selected by an accessor property called SelectedWebPart.

    The following example demonstrates how to simply create an HTML input control and assign the Web Part custom property value to it.

          ///<summary>

          /// Render this Tool part to the output parameter specified.

          ///</summary>

    ///<param name="output"> The HTML writer to write out to ///</param>

          protectedoverridevoid RenderToolPart(HtmlTextWriter output)

          {

                // Establish a reference to the Web Part.

                // CustomWebPart is the web part class name.

                CustomWebPart customWebPartEx1 =

    (CustomWebPart)this.ParentToolPane.SelectedWebPart;

                     

                //Create the input control

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

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

     

                //Assign the web part custom property value to the input

    //control.

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

    SPEncode.HtmlEncode(customWebPartEx1.CustomerName) + "'>

    <br>");                

          }

    Figure 6: RenderToolPart method in Tool Part class.

    Overwrite the ApplyChanges Method

    This method applies the changes made in the Tool Part back to the associated Web Part. ApplyChanges is a virtual method the developer needs to override inside the Tool Part. This method will be called when the user clicks OK or Apply in the default property pane.

     

     

    ///<summary>

    ///Called by the tool pane to apply property changes to the selected Web Part.

    ///</summary>

    publicoverridevoid ApplyChanges()

    {

          // apply property values here

          //Get a reference to the web part class

          CustomWebPart cw1 =

    (CustomWebPart)this.ParentToolPane.SelectedWebPart ;

     

          //Pass the custom text to web part custom property

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

    }

    Figure 7: ApplyChanges method in Tool Part class.

    CustomWebPart is the Web Part class name. A reference is obtained for the Web Part using this.ParentToolPane.SelectedWebPart. The custom text is then passed back to the Web Part custom property.

    2) Add a GetToolParts method to the Web Part class

    Navigate back to the Web Part class and remove the comments around the GetToolParts method provided by the template. Now include the new Tool Part object to the ToolPart object array as displayed below.

    ///<summary>

    ///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.

    ///</summary>

    ///<returns>An array of references to ToolPart objects.</returns>

    publicoverride ToolPart[] GetToolParts()

    {

          ToolPart[] toolparts = new ToolPart[3];

          //WebPartToolPart Object

          WebPartToolPart wptp = new WebPartToolPart();

     

          //CustomPropertyToolPart Object

          CustomPropertyToolPart custom = new CustomPropertyToolPart();

          toolparts[0] = custom; //Add CustomPropertyToolPart object to

    the ToolParts array

          toolparts[1] = wptp;//Add WebPartToolPart object to the

    ToolParts array

          toolparts[2] = new CustomWebPartEx1.CustomToolPart1();//Add

    custom tool part object to the ToolParts array

     

          return toolparts;

    }

    Figure 8: GetToolParts method in Web Part class.

    The method contains an array of ToolPart objects. Because this method is being overwritten, the WebPartToolPart and CustomPropertyToolPart object need to be added manually to the ToolPart object array. Then the custom Tool Part is added to the ToolPart array object.

    Perform the necessary step to deploy the Web Part to the SharePoint page. Please refer to the article "Developing Web Parts for SharePoint 2003 in VS.NET" for more information regarding deploying Web Parts. (http://www.devx.com/dotnet/Article/17518)

    Figure 9: Upload the Web Part.

    The following Web Part will display in the Web page.

    Figure 10: The Custom Web Part.

    Navigate to the default property pane. A new category called "Custom Tool Part Ex1" is created in the property pane. The category name is provided in the Tool Part class constructor as displayed below.

    ///<summary>

    /// Constructor for the class. A great place to set default values for

    /// additional base class properties here.

    ///<summary>

    public CustomToolPart1()

    {

    //The category name that will display in the default property

    pane

          this.Title = "Custom Tool Part Ex1";

          this.Init += new  EventHandler(CustomToolPart1Init);

    }

    Figure 11: Assigning the category name in the default property pane.

    TIP: To access the property pane, select the Edit mode of the Web Part. Then click the down arrow located at top right hand corner of the Web Part. Select "Modify Shared Web Part" (or personnel depend on which mode the Web Part is running).

    Figure 12: The Custom Tool Part class we created in property pane.

    The default property pane includes the custom Tool Part class created. Now simply enter a customer name and click Ok. The string value entered will display in the Web Part as illustrated below.

    Figure 13: The updated Web Part through Tool Part.

    Conclusion

    Developers can use the Tool Part class to add much more sophisticated functionality to the property pane. For example, imagine an application where the user needs to select a list of columns names to be displayed in a DataGrid in a Web Part. The developer can create a Tool Part class that displays a collection of available columns in a grid. This grid will appear in the default property pane. When the user selects the columns and clicks Ok in the property pane, the column values are passed onto the Web Part to display the DataGrid in the Web Part. This way, the user can customize the DataGrid behaviour in the Web Part through the property pane.

    About the Author

    Gayan Peiris is a Senior Consultant for Unique World Pty Ltd (www.uniqueworld.net) in Canberra, Australia. He is a MCSD with MCAD, MCP, Bcom and MIT certifications. Gayan has designed and developed Microsoft Web and Windows solutions since 1999. His expertise lies in developing scalable, high-performance web and windows solutions. His core skills are ADO.NET, ASP.NET, C#, VB.NET, Web Services, XML, SharePoint Portals, DNA and SQL Server. www.gayanpeiris.com.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Apr 7, 2004 - Reusable Components in ASP.NET 2.0, Object Binding and Precompilation
    This article demonstrates how to create a reusable component in ASP.NET 2.0 and then consume it from an ASP.NET page. Also learn how the ObjectDataSource control can be used to directly bind the output of an object to the controls in an ASP.NET page and how precompilation can be used to increase the performance of the Web application and catch compilation errors.
    [Read This Article]  [Top]
    Mar 31, 2004 - Build a Managed BHO and Plug into the Browser
    Browser Helper Objects (BHOs) are COM components that communicate with Internet Explorer to enrich the browsing experience. Michele Leroux Bustamante returns to the world of COM to show you how to build a managed BHO with the help of the .NET Framework's COM interoperability features.
    [Read This Article]  [Top]
    Feb 18, 2004 - Customizing SharePoint Web Parts with Custom Properties
    In addition to creating custom Web Parts for SharePoint Portal Server, developers can actually create their own custom properties to further enhance Web Part appearance and behavior. Gayan Peiris explains the process and provides all the necessary code.
    [Read This Article]  [Top]
    Sep 26, 2003 - Accessing Shared Resources Using ASP.NET
    Accessing shared resources is a challenge for many ASP.NET developers. Tony Arslan explains how a simple serviced component can solve this infamous problem.
    [Read This Article]  [Top]
    Oct 2, 2002 - Function Pointers and COM
    Using callbacks and function pointers in VB can be risky and complicated. Ben Garcia explains his work-around for the function pointer issue he encountered while creating the VB version of his SNMP component.
    [Read This Article]  [Top]
    Sep 4, 2002 - Creating an SNMP Component - Part 2
    In part two of this intriguing article series, Ben Garcia shows how to build an updated and improved SNMP component in VC++ AND VB, and he briefly explains why limitations in VB make VC++ a better language for developing this type of application.
    [Read This Article]  [Top]
    Jul 23, 2002 - Creating an SNMP Component
    Ben Garcia sheds some light on the Simple Network Management Protocol (SNMP). First he provides a history of SNMP, then he dives right into its architecture. Finally, he shows how to build a COM component that communicates with SNMP-enabled devices.
    [Read This Article]  [Top]
    Jun 26, 2002 - Accessing Caller ID from the Web - Part 1
    Paul Apostolos begins his series on using Web services and the MSComm32.OCX component to access caller id information from a Web page. In part 1, learn how to write the Visual Basic program that runs on the server and updates a database with incoming callers.
    [Read This Article]  [Top]
    Nov 20, 2001 - Creating a Server Component with VB - Redesigned - Part 2
    Doug Dean explains different methods of retrieving and manipulating data from a database in a VB DLL so that it is ready to be rendered in a browser.
    [Read This Article]  [Top]
    Aug 28, 2001 - Create Your Own Visual Basic Add-Ins
    In this article, S.S. Ahmed shows you how to create VB add-ins. Programmers always feel that they are short of several features while working with development tools. Since Microsoft made Visual Basic an extensible product, VB developers can create their own features in VB.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers