Document By: Chris Zwarts
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.
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.
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.
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.
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.
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.