SPD WebPart Definition Code

Using SharePoint Designer, users can update WebPart properties directly via the code pane. Now, if this is a requirement for you, you’ll notice that when editing a custom version three WebPart (System.Web.UI.WebControls.WebParts.WebPart) that the properties are displayed as attributes of the WebPart element:

<WebPart:Test_WebPart runat="server" CustomProperty="custom value"></WebPart:Test_WebPart>

I’ve trimmed the above to make it more readable, the real element would be far longer and all on one line.

To make it easier for users to update properties via SPD and to move the properties to their own child elements, one option available is to make a few changes to your WebPart.

  • Change the WebPart to inherit from the version 2 class – Microsoft.SharePoint.WebPartPages.WebPart
  • Add the SupportsAttributeMarkup(false) attribute to the WebPart class
  • For the property you want displayed as a child element, ensure the property attribute used for storage is WebPartStorage(Storage.Shared) and not Personalizable(PersonalizationScope.Shared). The storage type used is not important.

For example:

[SupportsAttributeMarkup(false)]
public class TestWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
    [WebPartStorage(Storage.Shared)]
    public string TestProperty
    { get; set; }
}

This will give something like the following:

If you don’t want to, or can’t change to the version two WebPart, a better solution would be to decorate your WebPart property with the PersistenceMode(PersistenceMode.InnerProperty) attribute. This will render the property as a child element of the WebPart, without the need to follow the steps described above.

[SupportsAttributeMarkup(true), ParseChildren(true),]
public class TestWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
    [Personalizable(PersonalizationScope.Shared),
     PersistenceMode(PersistenceMode.InnerProperty)]
    public string TestProperty
    { get; set; }
}

The added bonus of this method is that if, for instance, your property was created to store HTML, when editing the property value in SharePoint Designer’s code pane, IntelliSense will be provided, which is rather neat.

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve the maths problem shown below before posting: *

Follow

Get every new post delivered to your Inbox

Join other followers: