Create a Sharepoint 2007 webpart step by step


Mart Muller's on his weblog has written an article were he describes step by step the creation of a webpart in Sahrepoint 2007:

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

 You can find this article here.

Computer developer/consultant from Portugal. You can find me at ricardo.magalhaes@gmail.com

Posted in .NET, Sharepoint, Sharepoint
9 comments on “Create a Sharepoint 2007 webpart step by step
  1. Bachir ben says:

    can add a button to this webpart and hadle the button click.

    i a new to this technologie can you help

  2. Hemant says:

    this article is very nice, please send the related codes

  3. Chris says:

    Try an IISRESET? Do you people actually use any Microsoft junk for production or is it just a bunch of toys and games? Have you seen or heard of a place where the web server is actually up 24/7 like it’s supposed to be?

  4. ameen says:

    You are really crazy for giving half knowledge…. and half matter on the website and wasted my 15 mins on your bleady code.

  5. dear Ammeen,

    I don’t know if you noticed but this was a reference to an existing article, and not the article itself.

    Nevertheless, if you find that reading posts that might interest you can be a waste of time, my advice is instead of search for the solutions try and build them.

  6. Denny says:

    Good article. Worked perfect at the very first attempt. Gave me confidence in webparts development. Now, next step is to write an advanced webpart which will query database and populate a datagrid.
    Good job Ricardo.
    I am sorry Ameen – But, if you have even a slight experience developing in .NET, then this article is very simple to grasp.

  7. Dear my,

    like it is said in the start of the post, this is an article from:
    “Mart Muller’s on his weblog has written an article were he describes step by step the creation of a webpart in Sahrepoint 2007:”

    So this article is not mine, this is a reference!

  8. sithara lakmal says:

    This is really helpful. Thanks for the post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: