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")]
[FriendlyNameAttribute("Custom String")]
[Description("Type a string value.")]
public string MyString
        return _myString;
        _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?


1 Answer 1


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:

  <CodeSnippet Format="1.0.0">
      <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>
          <ToolTip>Replace with the tyoe of the Web Part Property</ToolTip>
          <ToolTip>Replace with the Name of the Web Part Property</ToolTip>
          <ToolTip>Replace with the name of the Category where this property will appear in the ToolPane</ToolTip>
          <ToolTip>Replace with the Friendly Name of the Web Part Property</ToolTip>
          <ToolTip>Replace with the Description of the Web Part Property</ToolTip>

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

        public $type$ $PropertyName$
            get { return this._$PropertyName$; }
            set { this._$PropertyName$ = value; }

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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