Web Parts in C#

Document By: Chris Zwarts


Web Parts in C#

Creating web parts is very similar to creating a normal page script in many ways. The main difference is that every control must be placed programmatically, instead of in a nice fuzzy GUI. It is possible to use custom user controls in web parts, but unless the part is already in another page it is often easier and better to use it programmatically, because using a normal web part, the template in visual studio can perform automatic deployment of web parts, saving a lot of time and energy.

Installing the Necessary Templates

System setup

Using these templates is much better in a clean work environment, as this allows you to easily work on a machine that is also a MOSS server. The templates will automatically generate a featurepack, so transferring this feature onto an existing server will be a rather simple process.

Finding the Template

The Templates to work with SharePoint code are produced and distributed free of charge by Microsoft. These templates are available on their download network, and are linked as follows.


On this page, there is a link to the 2005 version, which is in essence the same thing but made for visual studio 2005. The download within this link is for visual studio 2008.

Before downloading this program, there are some things you need to know. The computer that you are installing on must have both visual studio AND WSS or MOSS. Therefore, it must be a windows 2003 server as well.

Installing the Template

Once you have downloaded the appropriate version, you must simply double click the setup file to install the templates. I would suggest closing VS while you do this. After the installation is complete, you will be able to use the extensions. Although the scope of this document refers only to the use of the Web Part template, the other templates and the included utility (A site definition generator) can be equally useful.

When installing, the program will give a default location of the SharePoint dll (in the 60 hive). If the installation is not in a standard location, ensure that the path is corrected.

Using the Extensions to generate a web part

Starting the Part

Creating a web part is now as simple as creating any other type of premade project. Upon starting visual studio, you will find that under New Project there are now several new projects, including the one we are looking for: Web part, found under the SharePoint folder in either visual basic, C# or both, depending upon what you installed.


As soon as the boilerplate loads, you will see that, regardless of what you named the project, its main class (and several files) will still be called WebPart1. You must correct this immediately, before the first build and deployment, or it will require a fairly large amount of work to change it after (it would have to be retracted, removed, etc). Also, if a name is given that is the same as an existing feature, the deployment will fail and it will screw up the system in a way that is very hard to undo, so I would suggest having a program like SharePointInspector open to check for feature names before an attempted deployment.

Correcting the name of the class can be somewhat tricky; you must change the class name, the names of the files and of the folder, and the contents of the .webpart and xml files.

First, rename the class by right clicking it and going to Refactor>Rename. Then, Rename the folder in the solution explorer to the same thing, and also the name of each file in this folder (keep the proper extensions). Then, modify the contents of the XML file to show the new filenames.

The .webpart file contains the information to display in the SharePoint part gallery. Give the webpart a name (usually the same as what you named all the files) and give it a description.


Working with the Part

Now that the formalities have been covered, you can work on the web part as you usually would. As you can see, the GUID is automatically generated, and if you need it in any subsequent files, simply copy and paste it. The part should already have a strong name from the snk file near the top of the explorer, and this file can be changed (before deployment to avoid errors) by going into the project properties and creating a new file under the signing heading. This should not be necessary, however.

Add to Technorati Favorites


3 Responses to Web Parts in C#

  1. Mike says:

    Just passing by.Btw, your website have great content!

    Making Money $150 An Hour

  2. […] is to create a new solution that contains a Web Part Project, and to modify the web part as in my previous post so that it will compile properly. Then build it (but do not yet deploy!) and find the public key […]

  3. Guy says:

    Good post.

    Something I do to change the name of the webpart “WebPart1” is to:

    1) delete the folder “WebPart1” as soon as the project is setup
    2) right-click the project and select “Add”, “New Item”
    3) select “Sharepoint” and “Web Part”
    4) change the name in the “Name:” field and then click “Add”

    This will make all the changes necessary and alleviate the need to refactor, etc.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: