Sunday 18 September 2011

ToolPart ; How to create a tool part inside of a WebPart

We are going to introduce toolparts. This little “tools” can make the PROPERTIES of you web part quite interesting, as you can add any type of control without limitations. It is what we call, going to the extra mille.

Let’s go to start defining what a ToolPart is.
ToolPart: Defines custom tool parts that display a customized user interface for defining the properties of a Web Part inside of the tool pane.

This is the syntax for the ToolPart class:

[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public abstract class ToolPart : EditorPartAdapter

This is how it works. Imagine you need to display a choice control or a dropdown menu in the Properties of the Web Part, what do you do? you only have boring text controls. You create a class that inherits the ToolPart class, add the control and after that you add that control into your webpart.


It will probably better do a step by step so you can understand how this thing works.


What do you need?



  1. Visual Studio 2010
  2. Sharepoint 2010
  3. 20 minutes of your time

Step 1
Let’s go to create the project. Go to Visual Studio 2010->New Project->Sharepoint->2010->Empty Sharepoint Project and call it netsourcecodeToolPart.
image


Step 2
Let’s go to add a webpart to test the project. Go to your project->Right Click->Add-> New Item…->Web Part and call it NSCWebPart.
image



Step 3
We are going to add the class where the ToolPart will be added. Right Click in your project->New->Class. Call the class NSCToolPart.
image


Step 4
Now remove all the code from NSCToolPart.cs ,copy and paste this code into the file (this code is from sandeep’s blog).

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Security.Permissions;
namespace netsourcecodeToolsPart
{
    [AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class NSCToolPart: Microsoft.SharePoint.WebPartPages.ToolPart
    {
        // First, override the CreateChildControls method. This is where we create the controls.
        protected override void CreateChildControls()
        {
            // create a panel that will hold all of our controls
            Panel toolPartPanel = new Panel();
            // create the actual control
            DropDownList sampleDropDown1 = new DropDownList();
            sampleDropDown1.ID = "sampleDropDown1";
            sampleDropDown1.Items.Add("Item 1");
            sampleDropDown1.Items.Add("Item 2");
            sampleDropDown1.Items.Add("Item 3");
            toolPartPanel.Controls.Add(sampleDropDown1);
            // finally add the panel to the controls collection of the tool part
            Controls.Add(toolPartPanel);
            base.CreateChildControls();
        }
        // Next, override the ApplyChanges method.
        // This method is where we will persist the values that the user selects.
        public override void ApplyChanges()
        {
            // get the parent webpart
            netsourcecodeToolsPart.NSCWebPart parentWebPart = (netsourcecodeToolsPart.NSCWebPart)this.ParentToolPane.SelectedWebPart;
            // loop thru this control's controls until we find the ones that we need to persist.
            RetrievePropertyValues(this.Controls, parentWebPart);
        }
        // Recursive function that tries to locate the values set in the toolpart
        private void RetrievePropertyValues(ControlCollection controls, netsourcecodeToolsPart.NSCWebPart parentWebPart)
        {
            foreach (Control ctl in controls)
            {
                RetrievePropertyValue(ctl, parentWebPart);
                if (ctl.HasControls())
                {
                    RetrievePropertyValues(ctl.Controls, parentWebPart);
                }
            }
        }
        // Method for retrieving the values set by the user.
        private void RetrievePropertyValue(Control ctl, netsourcecodeToolsPart.NSCWebPart parentWebPart)
        {
            if (ctl is DropDownList)
            {
                if (ctl.ID.Equals("sampleDropDown1"))
                {
                    DropDownList drp = (DropDownList)ctl;
                    if (drp.SelectedItem.Value != "")
                    {
                        parentWebPart.myProperty = drp.SelectedItem.Value;
                    }
                }
            }
        }
    }
}

Step 5
Open NSCWebPart and copy and paste this code (this code is from sandeep’s blog).

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace netsourcecodeToolsPart
{
    [ToolboxItemAttribute(false)]
    public class NSCWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private string _property1 = "Default Value";
        public NSCWebPart()
        {
            this.ExportMode = WebPartExportMode.All;
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Label lbl = new Label();
            lbl.ID = "toolpart_webpart_lbl1";
            lbl.Text = _property1;
            Controls.Add(lbl);
        }
        public string myProperty
        {
            get
            {
                return _property1;
            }
            set
            {
                _property1 = value;
            }
        }
        public override ToolPart[] GetToolParts()
        {
            // resize the tool part array
            ToolPart[] toolparts = new ToolPart[3];
            // instantiate the standard SharePopint tool part
            WebPartToolPart wptp = new WebPartToolPart();
            // instantiate the custom property toolpart if needed.
            // this object is what renders our regular properties.
            CustomPropertyToolPart custom = new CustomPropertyToolPart();
            // instantiate and add our tool part to the array.
            // tool parts will render in the order they are added to this array.
            toolparts[0] = new NSCToolPart();
            toolparts[1] = custom;
            toolparts[2] = wptp;
            return toolparts;
        }
        protected override void CreateChildControls()
        {
        }
    }
}

Step 6
Deploy the solution. If you want to download the code, click on Download.
image

No comments: