Create a SharePoint 2007 webpart step by step#

I get a lot of questions how to take off with developing webparts in SharePoint 2007. This morning I gave a small quick demo here at Tam Tam to show which steps to take to get a web part working. 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!");

Webpartcode

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

Signing

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=1.0.0.0, Culture=neutral, PublicKeyToken=74e5f216d6256126" Namespace="WebPartToExplore" TypeName="*" Safe="True" /> )

Gacpic

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’.

Gallerywb

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!

Ttwebpart

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.

All content © 2012, Mart Muller
On this page
This site
Calendar
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
Archives
Sitemap
Disclaimer

Powered by: newtelligence dasBlog 1.9.7174.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts