Tuesday, August 30, 2011

Removing the OOB categories from the web part tool pane

Often while developing custom web parts for SharePoint 2010 or MOSS, sometimes it is required to hide the OOB tool pane (for e.g. Appearance, Layouts or Advanced) from the end user.

Trying for hours and scratching my head, I could not find any elegant way to achieve this. Finally I decided to use a hack via reflection.

In my custom editor part class on the CreateChildControls() method, I am calling a method HideOtherToolParts. The HideOtherToolParts iterates over the control collection for this editorpart and checks if the type of the child control is Microsoft.SharePoint.WebPartPages.WebPartToolPart and accordingly hiding that child.

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            try
            {
                this.HideOtherToolParts(this.Parent.Controls);
                //other operations
            }
            catch
            {
                throw;
            }
        }

        private void HideOtherToolParts(ControlCollection controls)
        {
            try
            {
                foreach (Control toolPart in controls)
                {
                    if (toolPart.GetType().FullName == "Microsoft.SharePoint.WebPartPages.WebPartToolPart")
                    {
                        toolPart.Visible = false;
                    }
                }
            }
            catch
            {
                throw;
            }
        }

Hope this helps!