DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

Develope a custom Webpart in MOSS

Posted by PANVEGA on March 17, 2007

So in this blogpost, we will see how to write the simplest possible Sharepoint 2007 WebPart and deploy it. We will inherit from the sharepoint webpart instead of the webpart.In this post I don´t wanna show how to create asolution. I concentrate in this post in just webpart steps.

Before we can start developing a custom webpart we need the Microsoft Visual Studio 2005 extensions for Microsoft Windows SharePoint Services 3.0.

The Extension include project templates that enable you to jumpstart your development work for the Windows SharePoint Services environment. In addition, the extensions include a Web Part project template that streamlines the creation, deployment, and testing of Microsoft ASP.NET Web Parts in Windows SharePoint Services. It aslo adds your new dll to the GAC, to the Sharepoint webpart gallery and  creates all neccessary solution files e.g.  wepart file…

In my coming up post I going to show you in details all steps and filey you need to deploy a solution to sharepoint.

For more information about choosing the best Web Part base class from which to derive, see Developing Web Parts in Windows SharePoint Services in the Windows SharePoint Services Software Development Kit (SDK). For more information about ASP.NET Web Parts, see the Web Parts Control Set Overview in the ASP.NET documentation.

Create a new empty Project then add a new Item called Webpart from the SharePoint extensions. The Web Part project template automatically generates the XML files needed to package your Web Part as a Windows SharePoint Services 3.0 Feature for deployment and activation. The required GUIDs contained in the XML files are also generated automatically.

Begin by creating a class library project, add references to System.Web, Microsoft.Sharepoint.dll.

Then add a class as below, which as you can tell is a simple Hello WebPart.

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml.Serialization;

using System.Web;

using System.Web.Security;

using System.Security;

using System.Security.Permissions;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Security;

using Microsoft.SharePoint.WebPartPages;

namespace Sahil.Sharepoint.WebPartPages


[System.Xml.Serialization.XmlRoot(Namespace = “http://namespace)]
public class CustomWebPart : WebPart


private string toDisplayText = “Hello Panvega”;

[WebPartStorage(Storage.Shared), System.Xml.Serialization.XmlElement(Namespace = “http://namespace)]
public string ToDisplayText
get { return toDisplayText; }
set { toDisplayText = value; }

protected override void RenderWebPart(System.Web.UI.HtmlTextWriter writer)


writer.Write(toDisplayText) ;





The extensions generate a .webpart file as the element.xml file for the Feature containing the Web Part. Every Web Part should have a .webpart file, which is an XML file that describes the Web Part. The .webpart file also makes your Web Part appear in the Web Part gallery in Windows SharePoint Services.

Go to the site of your choice, Edit Page, Click on Add a WebPart, you should see it in the list that pops up.

Add the WebPart, and under Miscellaneos, go ahead and modify the text it shows.

Adding Properties to the webpart:

After adding our first webpart to Sharepoint we go one step further and make some properties to the webpart. You can create a custom property in essentially the same way you create a property for a standard ASP.NET Web Form control, but with a few additional requirements.

private string xmlUrl = “”;
WebDisplayName(“Path XML File”),
WebDescription(“Path XML File”),
public string XmlUrl
get { return xmlUrl; }
set { xmlUrl = value; }

we can use the public property in our code directly, like here:

……string tmpXml = XmlUrl.Replace(“/”,”\\”);
FileXml = Context.Server.MapPath(tmpXml);
System.IO.FileInfo targetFile = new System.IO.FileInfo(fileXml);…..

You set attributes to customize the behavior of your Web Part in the property pane and to determine how your property will be stored. Most of these attributes are members of the System.ComponentModel namespace which provides classes that are used to implement the run-time and design-time behavior of Microsoft .NET components and controls. There are some attributes, like the WebPartStorage attribute, that are specific to Web Part properties. The following table lists these attributes and describes how they affect the behavior of your property.

Most attributes are members of the System.ComponentModel namespace. This namespace provides the classes that are used to implement the run time and design time behavior for Microsoft .NET components and controls. At the same time, there are properties like WebPartStorage attribute, which are specifically designed for Web Part properties. The attributes that you specify for the custom property will determine the behavior of the Web Part in the property pane and determine the storage for the custom property. Following is a list of attributes available and how they affect the behavior of the property.

  • Browsable
    Determines whether or not the property is visible in the property pane or not. Setting this attribute to true will include the property.
  • Category
    The property pane is divided into sections. This attribute determines the name of the section in the property pane where the custom property will be displayed. This allows the developer to categories their custom properties according to their logical functionality. If the developer does not specify this attribute or select “Default” as the category, the custom properties will display in Miscellaneous section of the property pane. At the same time, if you select one of the default categories, such as Appearance, Layout or Advanced, the category attribute setting will be ignored and the property will displayed in Miscellaneous section.
  • Default Value
    This is the default value of the property. It will minimize the storage requirement by storing the property value only if it is different from the default value.
  • Description
    Additional information regarding the property for the end-user. The description appears as the tool tip when the mouse hovers over the property.
  • FriendlyNameAttribute
    This is the caption of the property when it is displayed in the property pages. If this attribute is omitted, the actual name of the property is used instead.
  • ReadOnly
    The custom property will be read only if this is set to true.
  • WebPartStorage
    There are three settings for this attribute.

    • Storage.Shared: Display in property pane when the user is in shared view of the page.
    • Storage.Personal: Displays when the user is in shared OR personal view of the page.
    • Storage.None: Does not persist the setting of the property and will not display in the property pane.

More about properties and personalization of webpart in my other post:


//  Defines personalized property for User scope. In this case, the property is
//   the background color of the text box.
public System.Drawing.Color UserColorChoice
return _coloruserTextBox.BackColor;
_coloruserTextBox.BackColor = value;

The Web Part base class contains a set of properties that you and the users of your Web Part can use to control its appearance and behavior. For example, a Web Part’s Height and Width properties can be set by your code, the user, or the Web Part definition file (.dwp) you use to import a Web Part. A custom property is a property that you create when you need additional properties to enhance your Web Part’s functionality.

By default, the custom properties you create and specify as able to be browsed are displayed automatically in the default property pane, making their use transparent to the user. If the default property pane isn’t rich enough in functionality, you can create a custom user interface called a tool part to manipulate your custom properties. For information on creating a tool part, see the Creating a Web Part with a Custom Tool Part programming task.

Because Web Part properties can be customized and are saved in the SharePoint storage system as XML, you must do the following to support the properties you want saved:

  • Add a reference to the System.Xml.dll assembly to your project.
  • Include the XML Serialization namespace by adding the following using directive near the top of the code:
    using System.Xml.Serialization;
  • Add an XML namespace attribute, either at the root level of your assembly or at the property level. If you add the XML namespace at the root level, you are not required to assign XML namespaces at the property level. An XML namespace assigned at the property level will override the XML namespace assigned at the root level. The XmlRoot attribute has the following form:
    [XmlRoot(Namespace="name of namespace")]

Adding Controls to the webpart:

In the next step we gonna add some Controls to the webpart.

The ASP.NET Web Parts from the Class System.Web.UI.WebControls control set is a group of components that work together to enable you to create Web pages on which end users can modify the appearance and behavior of the user interface (UI) directly from a browser. This overview covers the fundamental aspects of the Web Parts control set, including a description of the most frequently used and essential Web Parts components needed to create a Web Parts page.

We override the CreateChildControls() method by adding the following code inside the class.

protected override void CreateChildControls() { //Create text box newTitle = new TextBox(); newTitle.Text = “Hello Webpart”; Controls.Add(newTitle); //Create button saveTitle = new Button(); saveTitle.Text = “Set Web Part Title”; saveTitle.Click += new EventHandler(saveTitle_click); Controls.Add(saveTitle);

Controls.Clear(); }

You can create and add as many Webcontrols you need. However it is very important in which order you add the several controls.

In my one of my next post I´m going to show some more detailed inforamtions about webparts.

The last usefully thing to know. In the .webpart file you find many configurations for your design of you webpart.


<?xml version=”1.0″ encoding=”utf-8″?>
<webPart xmlns=”“>
The following Guid is used as a reference to the web part class,
and it will be automatically replaced with actual type name at deployment time.
<type name=”198c3175-8656-4e0f-8a86-56cc22731fd9″ />
<importErrorMessage>Cannot import WebPart1 Web Part.</importErrorMessage>
<property name=”ChromeType” type=”chrometype”>Default</property>
<property name=”Title” type=”string”>My Webpart</property>
<property name=”Description” type=”string”>shows a list of my items</property>
<property name=”CatalogIconImageUrl” type=”string”></property>
<property name=”ExportMode” type=”exportmode”>All</property>


More Informations:


3 Responses to “Develope a custom Webpart in MOSS”

  1. Mark Vogt said

    Greetings Panvega,

    Nice post, but I noticed a few things which confuse me…

    1. Every time I try to use the “Category” attribute in Visual Studio 2005 with VSeWSS, the Intellisense fails to auto-recognize it, and I get build errors about not having the “Category” namespace or assembly. HOW did you manage to get this attribute to work??

    Hope to hear from you… a year later.

    – MV

  2. PANVEGA said

    Hi Mark,

    you have to add the Reference System.ComponentModel to your class. Then you can add the Category.Attributes without any errors.

    Good Luck

  3. Nino said

    Nice post!

    Mark, you can use SPWebCategoryName attribute for defining the category.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: