Posted on:
Categories: SharePoint;CodePlex
Description:

​Web Part property attributes allow you to customize the behaviour of web parts in the property pane. There are 3 commonly used attributes that take UI strings and show them in the property pane. They are:

  • [Category("Group Title")] - The section title the custom property will be grouped under. Default is Miscellaneous.
  • [WebDisplayName("Property Display Name")] – Display name for the property. If this attribute is not specified, the actual property name will be displayed.
  • [WebDescription("Property Description")] - Tool tip for the custom property.

In my last blog, I wrote about using resource files to centralize definitions of UI strings. The Group Title, Property Display Name and Property Description are UI strings. So how do we make use of resource strings in these property attributes?

The trick is to create your own classes that inherit these attribute classes and override the corresponding method or property to return resource strings.

For Category, create a new class that inherits CategoryAttribute and override the GetLocalizedString method.

[AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = true)]
public class SLCategoryAttribute : CategoryAttribute
{
 private readonly string _classKey;

 public SLCategoryAttribute(string classKey, string resourceKey)
  : base(resourceKey)
 {
  _classKey = classKey;
 }

 protected override string GetLocalizedString(string resourceKey)
 {
  var value = CommonHelper.GetResourceString(_classKey, resourceKey);
  if (String.IsNullOrEmpty(value))
  {
   return base.GetLocalizedString(resourceKey);
  }
  else
  {
   return value;
  }
 }
}

Within GetLocalizedString, return value of a resource string from a SharePoint resource of your choice. Here I pick provisioning resource and use SPUtility.GetLocalizedString which I have put in a common helper class.

internal class CommonHelper
{
 internal static string GetResourceString(string classKey, string resourceKey)
 {
  try
  {
   return SPUtility.GetLocalizedString(
    String.Format("$Resources:{0}", resourceKey),
    classKey,
    SPContext.Current.Web.Language);
  }
  catch
  {
   return resourceKey;
  }
 }
}

For WebDisplayName, create a new class that inherits from WebDisplayNameAttribute and override the DisplayName property:

[AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = true)]
public class SLWebDisplayNameAttribute : WebDisplayNameAttribute
{
 private readonly string _classKey;
 private readonly string _resourceKey;

 public SLWebDisplayNameAttribute(string classKey, string resourceKey)
 {
  _classKey = classKey;
  _resourceKey = resourceKey;
 }

 public override string DisplayName
 {
  get
  {
   var value = CommonHelper.GetResourceString(_classKey, _resourceKey);
   if (String.IsNullOrEmpty(value))
   {
    value = _resourceKey;
   }
   return this.DisplayNameValue = value;
  }
 }
}

For WebDescription, create a new class that inherits from DescriptionAttribute and override the Description property:

[AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = true)]
public class SLWebDescriptionAttribute : DescriptionAttribute
{
 private readonly string _classKey;
 private readonly string _resourceKey;

 public SLWebDescriptionAttribute(string classKey, string resourceKey)
 {
  _classKey = classKey;
  _resourceKey = resourceKey;
 }

 public override string Description
 {
  get
  {
   var value = CommonHelper.GetResourceString(_classKey, _resourceKey);
   if (String.IsNullOrEmpty(value))
   {
    value = _resourceKey;
   }
   return this.DescriptionValue = value;
  }
 }
}

To use these new attributes, create a SharePoint provisioning resource in your project (see my previous blog).

Provisioning Resource

Resource File Content

Now comes the final step. In your web part class, add the new attributes and pass in the resource file and resource key as the parameters.

[ToolboxItemAttribute(false)]
public class HelloUserWebPart : WebPart
{
 [WebBrowsable(true)]
 [Personalizable(PersonalizationScope.User)]
 [SLWebDisplayName("Softlanding.SharePoint", "WPDisplayNameUserName")]
 [SLWebDescription("Softlanding.SharePoint", "WPDescriptionUserName")]
 [SLCategory("Softlanding.SharePoint", "WPCategoryConfiguration")]
 public string UserName { get; set; }

And you are done. Edit the web part in the browser and your resource strings should display in the properties pane.

Property Pane

I packaged these custom attribute classes into a separate project so it is ready to be added to any SharePoint solutions. In my SharePoint projects I just start using these attributes right away without defining them every time.

Visual Studio Project

To download this project from CodePlex please go here.