How to override GetToolParts()

Coordinator
Mar 19, 2010 at 10:27 PM

If you want to add Tool Part properties to an existing web part you need to override GetToolParts(). However, most of the examples out there do not take into account that existing web parts have additional ToolParts.  Use the following code to make sure you get all the existing tool parts

 

 

 public override ToolPart[] GetToolParts()
        {
            List<ToolPart> tools = new List<ToolPart>(base.GetToolParts());
            tools.Add(new CustomToolPart());
            return tools.ToArray();
        }

 public override ToolPart[] GetToolParts()

        {

            List<ToolPart> tools = new List<ToolPart>(base.GetToolParts());

            tools.Add(new CustomToolPart());

            return tools.ToArray();

        }

Whats happening here is that I am getting all the existing tool parts and then I am creating one toolpart of my own with 'new CustomToolPart()':

    class CustomToolPart : Microsoft.SharePoint.WebPartPages.ToolPart

    {

 

        Panel toolPartPanel;

        TextBox tb;

        Label Label1;

 

        protected override void CreateChildControls()

        {

            toolPartPanel = new Panel();

            Label1 = new Label();

            Label1.Text = "CAML:<br/>";

            Label1.CssClass = "cursor:hand";

 

 

            tb = new TextBox();

            tb.ID = "tb";

            tb.TextMode = TextBoxMode.MultiLine;

            tb.Rows = 7;

            tb.Text = ((ContentQueryExtended)this.ParentToolPane.SelectedWebPart).QueryOverride;

 

            toolPartPanel.Controls.Add(Label1);

            toolPartPanel.Controls.Add(tb);

            Controls.Add(toolPartPanel);

            base.CreateChildControls();

        }

 

        public override void ApplyChanges()

        {

            ContentQueryExtended wp = (ContentQueryExtended)this.ParentToolPane.SelectedWebPart;

            wp.QueryOverride = tb.Text;

 

        }

    }