Creating a Class Library Web Part

Ratings:
(4)
Views:0
Banner-Img
  • Share this blog:

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. [WebBrowsable(true), WebDescription("Displays a custom message"), WebDisplayName("Display Message"), Personalizable(PersonalizationScope.User)] 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() { base.CreateChildControls(); LiteralControl message = new LiteralControl(); message.Text = DisplayMessage; Controls.Add(message); }

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox