SharePoint Web Part Property Default Value

I’ve been working on the final touches of my SharePoint 2010 taxonomy ‘Game of Life’ web part. I showed this web part during my SharePoint Enterprise Metadata Management (EMM or taxonomy) talk at SharePoint Saturday in New York earlier this year.

One of the final changes is taking the settings out of the ASPX page (and also removing the hard-coded ones), and moving them into the web part properties pane. This isn’t difficult to do, but I ran into a minor snag when I tried to set the default value of the properties. For example, most people playing with this control will be using the default term store name, so I may as well have that name, "Managed Metadata Service", already in the property box.

You can see these properties working in the image below (bottom, right).

WebPartProperties

At first, I thought the format was pretty obvious. I specified the default value like this: [DefaultValue("Managed Metadata Service")]. However, that didn’t work—the default property value was blank. I also tried adding a const string variable and using that to set the value: private const string m_TermStoreDefault = "Managed Metadata Service"; and then [DefaultValue(c_TermStoreDefault)]. But that didn’t work either—I still had an empty default value.

The solution was to set the member variable for the property to the default value. So instead of private string m_termStoreName; I used private string m_termStoreName = "Managed Metadata Service";. This sets the member variable that’s used for the property get method to the default value I want.

Note that if you don’t specify where you want your custom properties stored (see “XmlRoot(Namespace” below), you will see an error when you try to edit a property. Something like “Cannot save the property settings for this Web Part.”

For clarity, here’s what the file looks like (I’ve cut out some stuff that isn’t useful to this post) :

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

//Added references
using Microsoft.SharePoint.WebPartPages;
using System.Xml.Serialization;

namespace GameOfLifeWebPartProject.GameOfLifeWebPart
{
    [ToolboxItemAttribute(false)]
    [DefaultProperty("Text"), ToolboxData("<{0}:GameOfLifeWebPart  runat=server>"), XmlRoot(Namespace = "GameOfLifeWebPart")]
   
public class GameOfLifeWebPart : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/GameOfLifeWebPartProject/GameOfLifeWebPart/GameOfLifeWebPartUserControl.ascx";

        protected override void CreateChildControls()
        {
            Control control = Page.LoadControl(_ascxPath);
            Controls.Add(control);
        }

        // Set the default value
       private string m_termStoreName = "Managed Metadata Service";
        // Create a custom category in the property sheet
        [Category("Game of Life Settings")]
        // Property is available in both Personalization and Customization mode
        [WebPartStorage(Storage.Personal)]
        // The caption that appears in the property sheet.
        [FriendlyNameAttribute("Term Store Name")]
        [Browsable(true)]
        [Description("The name of the term store you'll use.")]
        [DisplayName("Term Store Name")]
        [XmlElement(ElementName = "TermStoreName")]
        public string TermStoreName
        {
            get { return m_termStoreName; }
            set { m_termStoreName = value; }
        }
   …
    }
}