. . .Before I describe those detailed steps, there are a few things that you have to keep in mind:
- WebParts are now based on the ASP.Net 2.0 Framework en become part of the ‘System.Web.UI.WebControls’ namespace. This means the ‘Microsoft.SharePoint.WebPartPages.WebPart’ has become ‘System.Web.UI.WebControls.WebParts.WebPart’.
- This also means that the ISAPI filter in WSS 2 has become obsolete and will be handled by ASP.Net 2.0
- In the WebPart class, the ‘override void RenderWebPart(HtmlTextWriter output)’ method will naturally change to ‘override void Render(HtmlTextWriter output)’.
Ok, with this in mind we will go through the steps to take you first SharePoint 2007 webpart:
- Start VS.Net 2005 and create a new project (Class Library)
- Add the ‘System.Web’ reference to the project. this reference contains the webpart controls.
- Inherit the class with ‘System.Web.UI.WebControls.WebParts.WebPart’
- Implement the ‘protected override void Render(HtmlTextWriter writer)’ method.
- To test, add a new line to the method, like ‘writer.Write("Hello Tam Tam!");’
Now, the webpart can be build. There are however two ways to deploy the dll in the sharepoint environment: using the ‘bin’ of the ‘GAC’. In this example we choose the Global Assembly Cache. Therefore, the assembly must be strongly named, which is very easy to do in VS.Net 2005.
- Open the project properties and go the the ‘signing’ section.
- Create a new key.
- Now, build the project
To let SharePoint know that the assembly is safe, we have to add it to the safe control list in the web.config of the SharePoint web. We need the public key token from the DLL for this.
- First drag and drop the compiled assembly into the GAC (by default c:\windows\assembly).
- Open the properties of the dll to get the public key token.
- Now register the safe control in the web.config. (i.e. ‘<SafeControl Assembly="WebPartToExplore, Version=18.104.22.168, Culture=neutral, PublicKeyToken=74e5f216d6256126" Namespace="WebPartToExplore" TypeName="*" Safe="True" /> )
New in SharePoint 2007 is that once a dll is registered in the web.config, SharePoint can automatically detect webparts from those dll’s. With the site settings page, we can now automatically populate the webpart gallery.
- In SharePoint, click ‘Site Actions’, ‘Site Settings’ and then under ‘Site Collection Administration’ click ‘Go to top level site settings’.
- Under ‘Galleries’, click ‘Web Parts’. Then click ‘New’.
- Now, the new webpart(s) will appear in the list. If not, try an IISRESET and refresh the page.
- Select the webpart we’ve just created and click ‘Populate Gallery’.
Now, we can go the the page in SharePoint where we want to add the web part.
- On the SharePoint site, click ‘Site Actions’ and the ‘Edit Page’.
- Click on ‘Add a WebPart’ in the zone you want to add the webpart.
- Select the webpart to add, and click ‘Add’.
If everything had gone well, the webpart is there!
Creating webparts like descibed above is very basic. There will be some tools available to make things a lot easier. One of those tools is ‘Visual Studio Extensions for SharePoint Services’.
NOTE: this example is based on Office SharePoint Server 2007 Beta 1 TR. Things can change in other versions. Example is made on a Virtual Server with SharePoint 2007 installed including Visual Studio 2005.
You can find this article here.