Would you like to get paid for signing up for a web hosting plan with Bluehost.com?
Powered by MaxBlogPress 

Bluehost - Affordable, Reliable Web Hosting Solutions
Powered by MaxBlogPress 

Building Web Parts for SharePoint

Posted on September 3, 2008
Filed Under Development, Software |

If you've enjoyed reading this post then please subscribe to my Full Text RSS Feed.

This is an introduction to building your first web part for SharePoint Server 2007 or Windows SharePoint Services 3.0. I learnt to create my first web part by reading articles online and looking through whatever resource I could find including watching SharePoint development videos on YouTube.

If you are planning to create web parts for the first time, you have to understand how to setup the development environment so that you can start coding the web parts. I am assuming that you have already set up your SharePoint server on a Windows 2003 Server and you are ready to dive into creating your first “Hello World” web part.

Before you begin, here is a check-list of what you must have installed on your development PC. Essentially you must have all of these before you can start to code.

Requirements
• NET 2.0
• Visual Studio 2005
• WSS 3.0 Visual Studio 2005 Extension

Install in the following sequence
• Install .NET 2.0 Framework
• Install Visual Studio 2005
• Install WSS3.0 Visual Studio 2005 Extension

Assembly
Copy SharePoint.dll from your server to the following location C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\ISAPI. The SharePoint.dll assembly I have on my development PC is Version 12.0.0.0.

Starting Visual Studio 2005

Create a new project and select SharePoint from the Project types then select Web Part Template

  • A Web Part class file is created. In the example it is called NewWebPart.cs.
  • You will see that the Microsoft.SharePoint reference is automatically added to the project.
  • From the source of NewWebPart.cs, you will notice that the NewWebPart class derives from System.Web.UI.WebControls.WebParts.WebPart


  • In the example, you will also notice that you override the Control.Render method. I am going to output the text “My new Web Part” when the Web Part is added in SharePoint. This is achieved by using the HtmlTextWriter class writer.Write(“My new Web Part”);

    If you have come this far, it is time to build the project. You should be able to build the project without any errors at this stage. Once built, you can deploy the web part onto the server.

    When deploying the Web Part assembly, you can either deploy it to the GAC (Global Assembly Cache) or drop the assembly dll in the bin directory. Deploying to the bin directory will make the assembly a private assembly which is only accessible from within the SharePoint site. In a web farm, it is more appropriate to deploy the assembly to the GAC so that multiple SharePoint sites can share the web part.

    Deployment to GAC involves additional steps. There are various ways to deploy an assembly to GAC for SharePoint web parts but in this example I am using the manual method which involves:

    • setting up a strong key file which I have called NewWebPart.snk
    • creating a dwp file which essentially is a xml file. A dwp file is a web part description file.
    • manually adding the assembly to the GAC

    Set up strong key file.
    Define a strong key file name as shown below.


    Creating a dwp file.

    • Under the Solution Explorer, right-click where it shows Solution ‘NewWebPart’ and select Add >New Item.
    • You will have a Add New Item – Solution Items window appear. Select XML File and call it NewWebPart.dwp

    You can use the example shown here but change the details, e.g. assembly name, type name, title, description and PublicKeyToken accordingly.


    For each assembly you have to obtain a PublicKeyToken and the way is to drag and drop the assembly into Windows Assembly and it will automatically assign a PublicKeyToken to the assembly.

    To open Windows Assembly, click Start > Run and type in Assembly.

    Drag and drop your assembly into Window Assembly. Then right click on the assembly and click on properties. Copy the Public Key Token and paste it in NewWebPart.dwp

    Deploying onto the SharePoint server

    • Now to deploy the files onto the server, locate the virtual directory of the SharePoint site on your server.
    • Copy the dwp file into the wpcatalog directory
    • Copy the snk (strong key file) to the SharePoint site root directory. Where the file should be located depends on your definition in Visual Studio 2005.
    • Then add the assembly in Windows Assembly on the server. Use Start > Run and type assembly. When you see the Windows Assembly window, drag and drop the web part assembly into Windows Assembly.

    Next you have to add a SafeControl entry in the web.config file. Edit the web.config file and add the entry as shown in the example. Replace the assembly name, version, public key and namespace accordingly. It should match up to what was defined in your dwp file.

    SafeControl Assembly=”NewWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9590be05a8dc0663″ Namespace=”NewWebPart” TypeName=”*” Safe=”True”

    Do and IISReset after completing all the steps above.

    Add Web Part to SharePoint site.

    • Click on Site Action > Site Settings.
    • You will see the Site Settings page like the one shown here.
    • Under the heading Galleries, click on Web Parts.

    • You will see a list of web parts available in the SharePoint site.
    • Click New.

    From the list you will find a web part called NewWebPart, the one you just deployed. Tick the checkbox and click on Populate Gallery, which you will find at the top of the page. The web part will now appear in the Web Part Gallery.

    Edit the web part in the Web Part Gallery and select Default Web Parts for Group and tick Default in Quick Add Group.

    Now to add the web part to your SharePoint web page, Select Site Action > Edit Page.
    Then add click Add a Web Part on the container where you want to add the web part.
    An Add Web Part windows will appear. Select NewWebPart from the Default Web Parts list and click the Add button.

    Your web part will now appear on the web page as shown here.

    Sphere: Related Content

    Other Related Posts:

  • Understanding WSS 3.0 in five minutes
  • MCTS Microsoft Office SharePoint Server 2007 Configuration
  • Installing WSS 3.0 Visual Studio 2005 Extension
  • Sharepoint Server 2007 Trial
  • Four corners of a frame

  • If you've enjoyed reading this post then why not subscribe to received updates by email.

    Enter your email address:

    Delivered by FeedBurner

    Email This Post Email This Post

    Trackback This Post

    Comments

    One Response to “Building Web Parts for SharePoint”

    1. Jayce on September 19th, 2008 3:28 am

      Great info. I will ready more from you site about SharePoint webpart. Need to write it soon. ;)
      Jayce’s last blog post..WW37 weekly contest winners

    Leave a Reply