Friday, December 29, 2006

A Simple Web Part for Windows Sharepoint Services (WSS) 3.0

There doesn't seem to be a lot of information out there on getting started with developing Web Parts for Windows Sharepoint Services 3.0 (WSS). So this is a quick example on developing and deploying a very basic Web Part for use in WSS.

My disclaimer: this may or may not be the best way to do it. As I said, not a lot of documentation around, but I've managed to get this working successfully, so it can at least be used as a starting point.

I will assume you have WSS 3.0 installed and running (and working!) on a Windows Server 2003 machine.

Visual Studio 2005 Extensions for WSS 3.0

Currently, this is only available as a Community Technology Preview (CTP), and unfortunately will only work on Windows Server 2003. Lets hope Microsoft will do something about this before the release.

In the meantime, you'll have to install Visual Studio on a Windows Server 2003 machine and develop on that. You may be able to install it on Windows XP, but it relies on the existence of the Microsoft.Sharepoint assembly and you'll only find that on your WSS 3.0 box.

Download the WSS 3.0 Visual Studio 2005 Extensions.

It is not needed for this example, but the WSS 3.0 Software Development Kit (SDK) is quite useful, if a bit lacking in parts.

Creating a Web Part Project

Run Visual Studio 2005 and bring up the New Project dialog. You should see another project type entry under Visual C# (and Visual Basic) called SharePoint. Selecting this brings up a list of SharePoint templates. Choose the Web Part template and click OK.

You should be looking at a very simple class:

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace WebPartTest
{
[Guid("")]
public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
{
protected override void Render(HtmlTextWriter writer)
{
// TODO: add custom rendering code here.
// writer.Write("Output HTML");
}
}
}

For this example, we want to change the base class to Microsoft.SharePoint.WebPartPages.WebPart (Supposedly you can use the standard .NET WebPart, but I was unable to get this to install). Also, lets uncomment the TODO so that we have some display for the Web Part.

Great, it compiles... now what?

Deploying the Web Part

There a few things required for deployment:
  • Add a manifest file
  • Add a web part config file
  • Add a CAB setup project
  • Use the command line to install into WSS
The Visual Studio extensions do give you a few extra menu options under build, including a Deploy menu option - this is all well and good for the machine you are developing on, but this doesn't help when you need to deploy a solution to another (client) site.

Manifest:

A manifest file is an XML file that is used to describe the Web Part(s) being deployed. Simply add a new XML file to your project and call it Manifest.xml. Here is an example manifest:

Manifest.xml
<?xml version="1.0"?>
<!-- You need only one manifest per CAB project for Web Part Deployment.-->
<!-- This manifest file can have multiple assembly nodes.-->
<WebPartManifest xmlns="http://schemas.microsoft.com/WebPart/v2/Manifest">
<Assemblies>
<Assembly FileName="WebPartTest.dll">
<SafeControls>
<SafeControl Namespace="WebPartTest" TypeName="*" />
</SafeControls>
</Assembly>
</Assemblies>
<DwpFiles>
<DwpFile FileName="WebPart1.dwp"/>
</DwpFiles>
</WebPartManifest>

Web Part Config:

A web part config file is an XML file used to describe an individual Web Part. Add another new XML file to your project and give it the same name as the Web Part class name, but give it an extension of DWP. Here is an example:

WebPart1.dwp
<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
<Title>My Web Part</Title>
<Description>This is my web part.</Description>
<Assembly>WebPartTest</Assembly>
<TypeName>WebPartTest.WebPart1</TypeName>
</WebPart>

CAB Setup Project:

Add a CAB Setup project to the solution by adding a New Project and select the CAB Project template under Other Project Types Setup and Deployment. Give it a name like WebPartTest.Setup.

Now, right click on the project and select Add Project Output. In the dialog that is displayed, select Primary Output Content Files and click OK.

A Build of this setup project should produce a .CAB file containing your Web Part assembly, its config file (DWP) and the manifest.

Installation into WSS:

Use the stsadm command located in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\ to install and uninstall your packages. IIS will require a restart after this, via the iisreset command.

To install:

stsadm -o addwppack -filename WebPartTest.Setup.cab -url http://localhost/ -globalinstall -force
iisreset /timeout:0

To uninstall:

stsadm -o deletewppack -name WebPartTest.Setup.cab
iisreset /timeout:0

Final Notes

So, it doesn't seem that involved, but unless I missed something completely, its not that well documented. Hopefully this helps someone who is in the same boat that I was.

I'd be interested to see how other people managed themselves trying to do the same thing, and whether there are any particularly good resources out there for this.

Thursday, December 28, 2006

Restoring Missing Templates in Visual Studio 2005

The other day I noticed that I was missing the Class template in the Add New Item dialog. Not a big deal, I said, just add a text file and change the extension. That was okay for a while, but it started to wear on me - why wasn't it there?

It turns out I was missing a whole bunch of templates and a complete reinstall of Visual Studio did nothing to fix it.

Apparently quite a few people are having this problem and yes, there is a fix! Thanks to Eric Hammersley and his blog.

Or if you are lazy (like me) and skimmed most of the above, close VS, open a new VS Command Prompt and enter the following command:

devenv /installvstemplates

Worked for me!

Ta da!

Ahh, a new blog.

This is more a way of gathering and sorting stuff I'm working on and figuring out at the time. If anyone else finds this useful, then that can only be a good thing.

Powered By Blogger