Skip to main content
Added code snippet
Source Link
Kit Menke
  • 4.2k
  • 6
  • 32
  • 40
  1. How to create a code snippet
  2. How to Manage Code Snippets

How to create a code snippet
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.

  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.

Source Link
GavinB
  • 2k
  • 12
  • 16

I use code snippets for this kind of thing.

How to create a code snippet
How to Manage Code Snippets