Create Custom Tool Parts for SharePoint Web Parts

What is a custom tool part ?

The Custom tool part is part of the web part infrastructure, that helps us to create a custom user interface for the web part properties that goes beyond the capabilities of the default property pane.

When do we need a custom tool part ?

Let’s say, If we need to create a web part property of type dropdown, we need to create a custom tool part. This is not supported out-of-box in the web part framework. I’ve the similar requirement of creating a custom web part property of type drop-down, So i went ahead and implemented the custom tool parts. Here are the steps to create a custom tool part.  

 public class SampleToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
    {
        //create an instance of the dropdown control to be used in the custom tool part 
        System.Web.UI.WebControls.DropDownList oDropDown = new System.Web.UI.WebControls.DropDownList();

        // Reference to the parent web part
        SampleWebPart oSampleWebPart = null;

On the constructor method of the tool part class, just set the title of the tool part. In this case i'm going to display the 
vendor names to the user
So i naming the title as select a vendor.
        public SampleToolPart()
        {

            // Set the title for the custom tool part
            this.Title = "Select a Vendor";

        }

        protected override void CreateChildControls()
        {
            try
            {

            //Get the instance of the parent web part on which this tool part is to be hosted
            oSampleWebPart = (SampleWebPart)ParentToolPane.SelectedWebPart; 
            //Get the instance of the current site collection
            SPSite oSPSite = SPControl.GetContextSite(HttpContext.Current);
            //Get the instance of the current site (spweb)
            Guid currentsiteid = SPControl.GetContextWeb(HttpContext.Current).ID;

            using (SPWeb oSPWeb = oSPSite.OpenWeb(currentsiteid))   

                {
In the following few lines of code, i'm reading the list of vendors from the sharepoint custom list 'VendorList' and binding it to the sharepoint custom tool part. 
                    SPList oProviderMaster = oSPWeb.Lists["VendorList"];
                    foreach (SPListItem oSPListItem in oProviderMaster.Items)
                    {

                        string sProviderName = oSPListItem["VendorName"].ToString();
                        oDropDown.Items.Add(sProviderName);
                    }

                }

            // Add the dropdown to the actual toolpart controls
            this.Controls.Add(oDropDown);
            base.CreateChildControls();
            }
            catch (Exception ex)
            {

            }

        }

When the user choses the item from the dropdown of the custom tool part and clicks 'Apply Changes', grab the selected item from 
the dropdown list and assign this value to a public property declared in the parent web part, where the tool part is hosted.
        public override void ApplyChanges()
        {
            try
            {
                if (!(oDropDown.SelectedItem == null))
                {
                    oSampleWebPart.SelectVendor = oDropDown.SelectedValue;
                }

            }
            catch (Exception ex)
            {

               //handle exceptions here
            }            

        }   

    }

 Subscribe to my post

One thought on “Create Custom Tool Parts for SharePoint Web Parts

  1. Pingback: Create Custom Tool Parts for SharePoint Web Parts - My experiments with SharePoint, Azure and .NET using Visual Studio

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.