SharePoint 2007: Writing a webpart

Quick summary how to create a webpart for SharePoint 2007…

  1. In visual studio 2005, create a new class library project (example code follows)
  2. Add a reference to the System.Web namespace (right-click references in the solution explorer and click add).
  3. Add the top of the class add using statements for the System.Web.UI and System.Web.UI.WebControls.WebParts namespaces. Note that you don’t need any references to any SharePoint namespaces unless you are going to use the SharePoint object model.
  4. Alter your class so that inherits from the System.Web.UI.WebControls.WebParts.WebPart class.
  5. Override the Render method with whatever you want the webpart to display.
  6. The assembly must be strongly named, irrelevant of if it is to go in the GAC or Bin directory. To do this, generate a keypair (sn ?k to generate .snk file) and add the command line options for “/keycontainer” and “/keyfile” to the project build command line options (via project settings).
  7. If the assembly is going to be installed in the SharePoint bin directory instead of the GAC, mark the assembly with AllowPartiallyTrustedCallers. See
  8. Compile your project to generate the webpart dll.

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Security;
[assembly: AllowPartiallyTrustedCallers]
namespace TestWebPart2
    public class TestWebPart3 : WebPart
        protected override void Render(HtmlTextWriter writer)
            SPList pagesList = SPControl.GetContextWeb(Context).Lists
            foreach (SPListItem currentItem in pagesList.Items)
                writer.Write("<br />");

Installing webpart for use in SharePoint.

  1. To install the webpart for use in SharePoint, you need to first copy the dll generated above to either the GAC (C:\WINDOWS\assembly) or the SharePoint Bin directory (find out the name of the site using iis manager – look for home directory for SharePoint on port 80). The SharePoint bin directory will reside at something like C:\Inetpub\wwwroot\wss\VirtualDirectories\41ea6637-fb56-4dcb-8a2e-9d1e91b56a20\bin.
  2. Add the control to the safe controls section of the web.config file e.g. <SafeControl Assembly=”TestWebPart2, Version=, Culture=neutral, PublicKeyToken=cdfbe353a0e07440″ Namespace=”TestWebPart2″ TypeName=”*” Safe=”True” />
  3. Do an iisreset
  4. Go to the top-level site settings for the web where you want to activate the webpart and click on “Webparts”. Click on the new button. This will take you through to http://localhost/_layouts/NewDwp.aspx which will detect which webparts are available from dlls etc. Your webpart should show up here. To be able to use the webpart in your site collection you must tick the relevant check box, then hit the populate gallery button.
  5. You will then be able to add your pages

Notes & Links

  1. Its seems that SharePoint will only detect one webpart per dll currently (although strangely there are two webparts from the Microsoft.Office.Excel.WebUI namespace: ExcelWebRenderer and InternalEwr). Hopefully this is something that will be fixed in the beta 2 technical refresh.
  2. This is excellent article on how to set up a webpart,guid,a34071a0-ad27-44c7-aa78-3f956d6f920e.aspx
  3. Another excellent article: This pays special attention to permissions of the SharePoint bin directory.

You May Also Like

About the Author: rnowik

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.