Hide Web Part's Default Tool Part or Hiding Appearance from Web Part Properties


If the user wants to hide the default properties of a web part or some sections in the default tool part of a web part. How to do this? Here are the approaches.

I created a simple web part inheriting from SharePoint Web Part class (Microsoft.SharePoint.WebPartPages.WebPart) which has only a text box as user interface.

So according to me, there can be two ways to do this.

1.    Hide entire default tool part of a web part - this is pretty simple

2.    Hide specific sections in the default tool part. Example: hide only the appearance section in the default tool part.

And here are my approaches -

1.    Hiding the entire default tool part is very easy thing to achieve. When you override the GetToolParts method of the web part then you don't have to add the default tool part in the array of ToolPart.

So sample can be written like:

Note that WebPartToolPart object is commented and not added in the array.

With this approach, you can only show the custom tool part for a web part.

public override Microsoft.SharePoint.WebPartPages.ToolPart[] GetToolParts()

{

  ToolPart[] _toolparts = new ToolPart[1];

  try

  {

    //WebPartToolPart _wptp = new WebPartToolPart();

    CustomToolPart _custom = new CustomToolPart();

     //_toolparts[0] = _wptp;

     _toolparts[0] = _custom;

 

  }

  catch (Exception ex)

  {

               

  }

  return _toolparts;

}

 

2.  Hiding the Specific Section of default tool part
 

Suppose we don't want to hide the default tool part entrirely but need to hide specific sections like appearance.

so this approach shows that how we can achieve this.
 

When you are in your custom TooPart class and inside then this.Parent represents the ToolPart of a web part. If you try to do this.Parent.Controls then you can see that you get collection of controls inside the ToolPart pane. so we can easily set the visibility of those controls like
 

Note that you can add this code in CreateChildControls of your tool part class

 

if (Parent != null && Parent.Controls != null && Parent.Controls[1] != null)

{

  if (Parent.Controls[1].Controls != null)

  {

     Parent.Controls[1].Controls[0].Visible = false;

   }

}

 

Please Note that I have not tried these methods when web part is derived from ASP.NET Web Part class i.e. System.Web.UI.WebControls.WebParts.WebPart , If I get to know some update on that then I will update this post.

 

Here is my entire sample code

 

public class HiddenPropertiesWebPart : Microsoft.SharePoint.WebPartPages.WebPart

{

 

  public override Microsoft.SharePoint.WebPartPages.ToolPart[] GetToolParts()

  {

    ToolPart[] _toolparts = new ToolPart[2];

    try

    {

      WebPartToolPart _wptp = new WebPartToolPart();

      CustomToolPart _custom = new CustomToolPart();

      _toolparts[0] = _wptp;

      _toolparts[1] = _custom;

 

    }

    catch (Exception ex)

    {

               

    }

    return _toolparts;

 }

 

 protected override void CreateChildControls()

 {

   try

   {

     base.CreateChildControls();

     TextBox _txtBox = new TextBox();

     this.Controls.Add(_txtBox);

   }

   catch (Exception ex)

   {

     this.Controls.Add(new LiteralControl(string.Format("{0}-{1}", "Error", ex.Message)));

   }

  }

}

 

public class CustomToolPart : ToolPart

{

  protected override void CreateChildControls()

  {

    try

    {

      base.CreateChildControls();

      DropDownList _ddl = new DropDownList();

      _ddl.Items.Add("Item1");

      _ddl.Items.Add("Item2");

      this.Controls.Add(_ddl);

 

      //Hiding the Desired Section in the Default ToolPart

 

      // Parent.Controls[1] shows - default tool part

      // Parent.Controls[1].Controls[0] shows - default tool part's first control - that is Appearance

      if (Parent != null && Parent.Controls != null && Parent.Controls[1] != null)

      {

        if (Parent.Controls[1].Controls != null)

        {

          Parent.Controls[1].Controls[0].Visible = false;

        }

      }

     }

     catch (Exception ex)

     {

       this.Controls.Add(new LiteralControl(string.Format("{0}-{1}", "Error", ex.Message)));

     }

    }

}