Create a custom Web Part

Sep 2, 2012
Comments off

Today I’ve decided to post a step by step tutorial on creating a basic web part in SharePoint 2010, I hope you enjoy. This should be helpful for anyone looking to get into “proper” SharePoint Development.

Before you get started you will need to make sure you have the following:

  • SharePoint 2010 Server
  • Microsoft Visual Studio 2010 installed on the SharePoint server


How to create a Web Part

  1. Start Microsoft Visual Studio 2010.
  2. On the File menu, select New, and then click New Project.
  3. In the New Project dialog box, in the Installed Templates pane, expand the SharePoint option under the programming language that you want to use, and then select 2010.
  4. In Templates, select Empty SharePoint Project.
  5. Type Sample.DisplayMessage as the project name, and then click OK.
  6. In the SharePoint Customization Wizard, select Deploy as a sandboxed solution, and then click Finish.
  7. In Solution Explorer, click the Sample.DisplayMessage project.
  8. On the Project menu, click Add New Item.
  9. In the Add New Item dialog box, select the Web Part template, type DisplayMessageWebPart as the Name, and then click Add.

After adding the new Web Part, add a property that can be personalized for the Web Part. The Web Part property determines the text that is rendered inside the Web Part. This is personalized based on the individual user.

Custom properties built using the ASP.NET Web Parts Pages base class are named differently than Web Parts that are based on the base class. The following list describes each of those properties:

  • The WebBrowsableAttribute attribute ensures that your custom property renders in the editing tool pane in SharePoint Foundation.
  • The WebDescriptionAttribute attribute displays a tooltip to help guide users when they are editing your custom property.
  • The WebDisplayNameAttribute attribute shows a display name for your custom property.
  • The PersonalizableAttribute attribute determines whether changes to your custom property affect all users or only individual users.

Creating a Web Part Property

Firstly, in the DisplayMessageWebPart file, copy and paste the following code to create a basic customizable property:

Private customMessage As String = “Hello, world!”

Public Property DisplayMessage() as String


Return customMessage

End Get

Set(ByVal value as String)

customMessage = value

End Set

End Property

Add the following above the public declaration to allow changes by the user.

<WebBrowsable(True), _

WebDescription(“Displays a custom message”), _


WebDisplayName(“Display Message”), _

Personalizable(PersonalizationScope.User)> _

Now we need to add functionality to the Web Part. In this code example below, the Web Part renders the user-defined text.

In the DisplayMessageWebPart paste the following code:

Protected Overrides Sub CreateChildControls()



Dim message As LiteralControl = New LiteralControl()


message.Text = DisplayMessage



End Sub

At this stage you have your created customized Web Part and you’re ready to deploy and test the web part.

Comments are closed.