To Create a Web Part

  1. Start Microsoft Visual Studio 2010.
  2. On the File menu, point to New, and then click Project. If Visual Studio is set to use Visual Basic development settings, on the File menu, click New Project.
  3. In the New Project dialog box, in the Installed Templates pane, expand the SharePoint node under the programming language that you want to use, and then select 2010.
  4. In the Templates pane, select Empty SharePoint Project.
  5. Type 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 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.


Adding a Web Part Property

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.

For more information about customization and personalization, see Web Parts Personalization.

For Web Parts based on the ASP.NET Web Parts Pages base class, the tags that are used for the customizable properties are named differently than Web Parts that are based on the WebPart 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.

To create the Web Part property

1. In the DisplayMessageWebPart file, copy and paste the following code to create a basic customizable property.

private string customMessage = "Hello, world!";

public string DisplayMessage


get { return customMessage; }

set { customMessage = value; }


2. Add the following tags above the public declaration to allow changes on a per-user basis.


WebDescription("Displays a custom message"),

WebDisplayName("Display Message"),


Now you have created a personalizable Web Part property.

Interested in mastering SharePoint Training? Enroll now for FREE demo on SharePoint Training.

Overriding the CreateChildControls Method

Next, you must add functionality to your Web Part. By overriding the CreateChildControls method, you can tell the Web Part what operations to perform when the page is visited. In this example, the Web Part renders the user-defined text.

To override the CreateChildControls method

  • In the DisplayMessageWebPart file, paste the following code to override the CreateChildControls methods.

protected override void CreateChildControls()



LiteralControl message = new LiteralControl();

message.Text = DisplayMessage;



For Indepth understanding of SharePoint click on