Walkthrough: Creating a Basic Web Part
This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
This walkthrough provides the steps for creating a basic custom Web Part that can be added to your Web Part Pages. It is a simple Web Part that allows the user to define a custom message to be displayed inside the Web Part. This Web Part will derive from the ASP.NET 2.0 Web Part class, which is the recommended practice for Windows SharePoint Services. Note that this walkthrough describes how to create a Web Part without the Visual Studio Extensions for Windows SharePoint Services.
You can also develop your Web Parts by using the Visual Studio Extensions for Windows SharePoint Services. By using these extensions, you can greatly reduce the work involved in creating and deploying your Web Parts. The extensions require development in a server environment, and offer the following benefits:
For guidance on using the extensions or to download extensions for Visual Studio 2005 or Visual Studio 2008, see
To create a Web Part, you start by creating a class library project that derives from the WebPart base class.
To create an ASP.NET Web Part Assembly
Start Visual Studio 2005
On the File menu, point to New, and then click Project.
In Project Types, under Visual Basic or C#, select Windows.
In the Templates pane, select Class Library.
Type Sample.DisplayMessage as the project name.
After you create the project, a blank class file is displayed. You can change the default class name of Class1 to easily identify your new Web Part. In a class library, only a few namespaces are included. You must add two required namespaces and references to their assemblies. You must also derive the base class from WebPart.
To add namespace references and rebase your Web Part
Rename the default class by selecting Class1 in Solution Explorer, right-click, click Rename, and type DisplayMessageWebPart as the file name.
On the Project menu, click Add Reference.
In the Add Reference dialog on the .NET tab, select System.Web and click OK.
You have now created a basic structure for the Web Part.
After configuring the new class to act as a Web Part, add a customizable property for the Web Part.
The Web Part property determines the text that is rendered inside the Web Part. This is customized on the basis of 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 class attribute ensures that your custom property renders in the editing tool pane in Windows SharePoint Services.
The WebDescriptionAttribute class attribute displays a tooltip to help guide users when editing your custom property.
The WebDisplayNameAttribute class attribute shows a display name for your custom property.
The PersonalizableAttribute class attribute determines if changes to your custom property affects all users or on a per-user basis.
To create the Web Part property
In the DisplayMessageWebPart file, copy and paste the following code to create a basic customizable property.
Then, add the following tags above the public declaration to allow changes on a per-user basis:
Now you have created a customizable Web Part property.
Now you must add functionality to your Web Part. By overriding the Control.Render Method, you can tell the Web Part what operations to perform when the page is visited. In this example, the Web Part will render the user-defined text.
To override the Render method and compile the assembly
In the DisplayMessageWebPart file, copy and paste the following code to override the Render method.
Compile the solution. Now you have an assembly that contains your DisplayMessageWebPart Web Part.
Within a SharePoint site, you can deploy a Web Part assembly to one of two locations listed below. Note that the recommended practice is to deploy assemblies to the bin directory.
bin directory — The bin directory is a folder stored in your Web application root directory. For most installations, this is located in the %SYSTEMDRIVE%\inetpub\wwwroot\wss\VirtualDirectories\<port number>\bin directory, but if you need more information, see How to: Find the Web Application Root.
Global Assembly Cache (GAC) — The GAC enables you to share assemblies across numerous applications. The GAC is automatically installed with the .NET framework runtime. Components are typically installed in the %SYSTEMDRIVE%\WINDOWS\Assembly directory.
For more information about choosing the correct deployment location, see Deploying Web Parts in Windows SharePoint Services.
For the following example, place the assembly in the bin directory.
Windows SharePoint Services provides a SafeControls list to prevent users from arbitrarily adding server-side code within ASPX pages. The SafeControls list is a list of approved controls and Web Parts that are specific to the SharePoint site that you have designated as safe for invocation on any ASPX page within your site. This list is contained in the web.config file in your Web application root. The local path contains the physical location of the web.config file.
The web.config file is located in the folder where the virtual directory for the Web site is located. This is normally c:\inetpub\wwwroot\wss\VirtualDirectories\<port number>\, but your administrator may have set up your directory differently and this may not be the location of the file.
You can find the location of your web.config file with the Internet Information Services (IIS) snap-in. The IIS snap-in is an administration tool for IIS that has been integrated with other administrative functions. To launch the IIS snap-in and locate the web.config file:
To add your Web Part to the SafeControls list
Open the web.config file in the application root.
In the SafeControls section of your web.config file, add a SafeControls entry for your custom assembly in the web.config file as follows.
In order to add your Web Part to a Web Part page, you must add it to the gallery.
To add your Web Part to the Web Part Gallery
Navigate to the page on your SharePoint site where you want the Web Part to be accessible.
In the Web Part page, click Site Actions, and select Site Settings.
On the Site Settings page, click Web Parts under the Galleries heading.
On the toolbar in the Web Part gallery, click New.
In the New Web Parts list, check the box next to Sample.DisplayMessage.DisplayMessageWebPart and click Populate Gallery.
Your Web Part can now be added to any Web Part page.
In this step, you import the Web Part to a page in any SharePoint site and test the custom property you added to the Web Part. The following procedure explains the steps for adding the Web Part to a page and changing the Display Message custom property.
To add and test your Web Part
Navigate to the Web Part page on your SharePoint site where you want to add the Web Part.
In the Web Part page, click Site Actions, and select Edit Page.
In the Web Part zone where you want to add the DisplayMessageWebPart, click Add a Web Part.
On the Add Web Parts dialog, find the Miscellaneous category under All Web Parts and check the box next to DisplayMessageWebPart. Click Add.
To modify the text displayed on the Web Part, choose Modify Shared Web Part from the chrome drop-down list.
In the DisplayMessageWebPart properties pane, expand the Miscellaneous category and change the Display Message value. Click Apply.
Now the Web Part is displaying the user-defined value inside the Web Part.