Pages

Search This Blog

Tuesday, November 8, 2011

Add Custom Property Panel in Custom WebPart like OOB SharePoint Property Panel via Reflection


Sometime we have a requirement to create custom property in WebPart. But the custom Property will add in “Miscellaneous” section or added in blank section if we add property via custom tool part. Now if you want to create panel like OOB Panel “Layout, Appearance etc.” then use below method.
Below class is static class for enabling extension method on ToolPart class. You need to add below class in your solution and add namespace in your toolpart class where you want to use this method
namespace ExtensionMethods
{
    public static class CustomExtensions
    {
        public static Panel GetPropertyPanel(this ToolPart currentToolPart, Table table, StringsTitle)
        {
            Panel controlPanel = new Panel();
            controlPanel.ID = "propertyPanelHideDisplay";
            controlPanel.Attributes.Add("id", currentToolPart.ClientID + "_" + controlPanel.ID);
            controlPanel.Controls.Add(table);

            Literal lt = new Literal();
            String sScript = "<script language='javascript'>\n" +
                            " var objDiv = document.getElementById('" + currentToolPart.ClientID + "_" + controlPanel.ID + "');\n" +
                            " objDiv.parentNode.parentNode.parentNode.attributes.removeNamedItem('colspan');\n" +
                            " objDiv.parentNode.parentNode.parentNode.attributes.removeNamedItem('class'); \n" +
                            "</script>";
            lt.Text = sScript;
            controlPanel.Controls.Add(lt);

            Type type = typeof(SPSite);
            Assembly assembly = type.Assembly;

            var bindingFlags = BindingFlags.Instance | BindingFlags.Public |BindingFlags.NonPublic;

            Panel propertyPanel = (Panel)assembly.CreateInstance("Microsoft.SharePoint.WebPartPages.TPPanel"false, bindingFlags,null,
               new object[] { sTitle, controlPanel, true }, nullnull);

            return propertyPanel;
        }
  
 }
}

Below is the sample class for ToolPart for adding Panel by using above class

using ExtensionMethods;
namespace CustomNamespace
{
    class CustomToolpart : ToolPart
    {
        private DropDownList ddlDisplayType;
     
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            CreateControls();
        }

        public override void ApplyChanges()
        {
            EnsureChildControls();
            SendDataToWebPart();
        }

        private void SendDataToWebPart()
        {
            EnsureChildControls();
            CustomWebPart customWebPart = (CustomWebPart)this.ParentToolPane.SelectedWebPart;

            // Send the custom text to the Web Part.
            if (ddlDisplayType != null)
            {
                customWebPart.Property = ddlDisplayType.SelectedValue;
            }

           
        }

        public override void SyncChanges()
        {
            base.SyncChanges();
           
        }

        private void SetValues()
        {
            ListItem item = null;
            CustomWebPart customWebPart = (CustomWebPart)this.ParentToolPane.SelectedWebPart;
            if (!string.IsNullOrEmpty(customWebPart.Property))
            {
                item = ddlDisplayType.Items.FindByValue(customWebPart.Property);
                if (item != null)
                {
                    ddlDisplayType.SelectedValue = item.Value;
                }
            }
        
           
        }

        public override void CancelChanges()
        {
            base.CancelChanges();
        }


        protected override void RenderToolPart(System.Web.UI.HtmlTextWriter output)
        {
            base.RenderToolPart(output);
        }


        public void CreateControls()
        {
          
            ddlDisplayType = new DropDownList();

            ddlDisplayType.Items.Add("Property 1");
            ddlDisplayType.Items.Add("Property 2");
            ddlDisplayType.Items.Add("Property 3");

            AddControls();
            SetValues();
          
        }

        private void AddControls()
        {
            Table table = new Table();
            TableRow tr = new TableRow();
            TableCell td = new TableCell();
            Literal ltStatic = new Literal();

            ltStatic.Text = "Custom Property";
            td.Controls.Add(ltStatic);
            tr.Cells.Add(td);
            table.Rows.Add(tr);
           
            tr = new TableRow();
            td = new TableCell();
            td.Controls.Add(ddlDisplayType);
            tr.Cells.Add(td);
            table.Rows.Add(tr);

            String sTitle = "Panel Title";
            this.Controls.Add(this.GetPropertyPanel(table, sTitle));
        }
    }
}


In above class bold line will return Panel and add the Panel in ToolPart Pane. Below is the screen shot for the above implementation


Hope it help !!!!

No comments:

Post a Comment