Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm creating a web part using Visual Studio 2008 for SharePoint 2007. If I'm creating the web part from scratch, I typically don't have any properties defined yet in my code.

I've found that it is hard to remember the syntax for a web part property because of all the attributes I need to specify. For example, the Creating a Web Part with Custom Properties tutorial shows how to create a variety of properties:

[Category("Custom Properties")]
[DefaultValue(c_MyStringDefault)]
[WebPartStorage(Storage.Personal)]
[FriendlyNameAttribute("Custom String")]
[Description("Type a string value.")]
[Browsable(true)]
[XmlElement(ElementName="MyString")]
public string MyString
{
    get
    {
        return _myString;
    }
    set
    {
        _myString = value;
    }
}

Instead of searching for code each time I want to add a property or typing from scratch, is there an easy way to add a web part property to my custom web part?

share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

I use code snippets for this kind of thing.

  1. How to create a code snippet
  2. How to Manage Code Snippets

Here is an example string property you can invoke by wpptabtab. Then you tab through the parts to fill out:

<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>SharePoint Web Part Property</Title>
      <Description>Create a SharePoint Web Part Property, Field and Default Field</Description>
      <Author>Revised by Kit Menke, Original Author: Morgan Everett</Author>
      <Shortcut>wpp</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>type</ID>
          <ToolTip>Replace with the tyoe of the Web Part Property</ToolTip>
          <Default>string</Default>
        </Literal>
        <Literal>
          <ID>PropertyName</ID>
          <ToolTip>Replace with the Name of the Web Part Property</ToolTip>
          <Default>MyWebPartProperty</Default>
        </Literal>
        <Literal>
          <ID>Category</ID>
          <ToolTip>Replace with the name of the Category where this property will appear in the ToolPane</ToolTip>
          <Default>TOOLBOX_SECTION_NAME</Default>
        </Literal>
        <Literal>
          <ID>MyFriendlyName</ID>
          <ToolTip>Replace with the Friendly Name of the Web Part Property</ToolTip>
          <Default>MyFriendlyName</Default>
        </Literal>
        <Literal>
          <ID>MyDescription</ID>
          <ToolTip>Replace with the Description of the Web Part Property</ToolTip>
          <Default>MyDescription</Default>
        </Literal>
      </Declarations>

      <Code Language="CSharp">
        <![CDATA[private const $type$ DEFAULT_$PropertyName$ = "MyDefault";
        private $type$ _$PropertyName$ = DEFAULT_$PropertyName$;

        [Browsable(true)]
        [Category($Category$)]
        [DefaultValue(DEFAULT_$PropertyName$)]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("$MyFriendlyName$")]
        [Description("$MyDescription$")]
        public $type$ $PropertyName$
        {
            get { return this._$PropertyName$; }
            set { this._$PropertyName$ = value; }
        }]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Define TOOLBOX_SECTION_NAME as a const string at the top of your web part class to group them all together.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.