Custom Properties on v3 web parts

For the developers that create custom web parts for SharePoint you will come to the time when you want to allow end users to enter extra information to customize the web parts you have created.  Most of the blog posts that I have seen online reference how to create custom properties for v2 (.dwp) web parts, but not v3 (.webpart) web parts.

 

As you know v2 web parts inherit from the Microsoft.SharePoint.WebPartPages.WebPart class and use the following attributes to set properties on the custom categories:

-          Browsable

-          Category

-          WebPartStorage

-          FriendlyName

-          Description

 

Here is an example v2 web part that uses a custom property

 

namespace customWebPart

{

    /// <summary>

    /// Customize a web part

    /// </summary>

    [DefaultProperty("Text"),

     ToolboxData("<{0}:customWebPart runat=server></{0}:customWebPart>"),

     XmlRoot(Namespace="customWebPart")]

    public class CustomWebPart : Microsoft.SharePoint.WebPartPages.WebPart

    {

        private string customProperty = string.Empty;

 

        /// <summary>

        /// Custom Property

        /// </summary>

        [Browsable(true),

         Category("Inetium"),

         WebPartStorage(Storage.Personal),

         FriendlyName("Custom Property"),

         Description("Custom Property Description")]

        public string CustomProperty

        {

            get

            {

                return customProperty;

            }

 

            set

            {

                customProperty = value;

            }

        }

 

        protected override void Render(HtmlTextWriter writer)

        {

            if (CustomProperty == string.Empty)

            {

                writer.WriteLine("No custom property set");

            }

            else

            {

                writer.WriteLine(CustomProperty);

            }

  

            base.Render(writer);

        }

    }

}

 

If you are updating your web parts to the v3 platform which inherits from the System.Web.UI.WebControls.WebParts.WebPart class you will notice that the v2 attributes will not work for your newly created v3 web part.  You will need to adjust your custom property attributes to:

-          WebBrowsasble

-          Category

-          Personalizable

-          WebDisplayName

-          WebDescription

 

Here is an example of a v3 web part that uses a custom property

 

namespace customWebPart

{

    /// <summary>

    /// Customize a web part for redirection

    /// </summary>

    [XmlRoot(Namespace="customWebPart")]

    public class CustomProp : System.Web.UI.WebControls.WebParts.WebPart

    {

        private string customProperty = string.Empty;

 

        /// <summary>

        /// Custom Property

        /// </summary>

        [WebBrowsable(true),

         Category("Inetium"),

         Personalizable(PersonalizationScope.Shared),

         WebDisplayName("Custom Property"),

         WebDescription("Custom Property Description")]

        public string CustomProperty

        {

            get

            {

                return customProperty;

            }

 

            set

            {

                customProperty = value;

            }

        }

 

        protected override void Render(HtmlTextWriter writer)

        {

            if (CustomProperty == string.Empty)

            {

                writer.WriteLine("No custom property set");

            }

            else

            {

                writer.WriteLine(CustomProperty);

            }

  

            base.Render(writer);

        }

    }

}

 

Here is an example of the toolpart that will be displayed when you have a custom property setup

 

Custom Properties toolpart

 

Once you have your custom properties set for your web part, you can then use the information the end users provide to do whatever you need to do.

 

["Brian"]

Leave a Comment

(required) 
(required) 
(optional)
(required)