Dotnetdreamer's Weblog

SharePoint, Silverlight and Azure

Creating Custom Web Parts in MOSS 2007

Posted by Ramprasad Navaneethakrishnan on May 9, 2009

Over my previous posts, I was discussing about the out-of-the-box site templates and out-of-the-box webparts available in MOSS 2007. This post is dedicated to Creating Custom Web Parts in MOSS 2007.

Creating Custom Web Parts:

Let us list down the procedures involved in creating custom web parts.

  1. Create custom web part control in Visual Studio
  2. Strong name the created web part assembly
  3. Place the assembly in the bin directory in the Virtual Directory of the web application
  4. Place the assembly in the GAC
  5. Notedown the assembly name, version and public key token by looking into the assembly properties in the GAC
  6. Add a SafeControl entry for the web part assembly in the application’s web.config file
  7. Do IISRESET
  • Create Web Part in Visual Studio
    • Open Visual Studio 2005–>Create New Project–>Select Class Library project template–>Enter the project name as CustomWebpart
    • customwebpart_vs_createproject
    • Add Reference to System.Web dll
    • Open Class1.cs. Add the following namespaces
      • using System.Web;
      • using System.Web.UI.WebControls;
      • using System.Web.UI.WebControls.WebParts;
    • Change the name of the class to CustomWebPart1. Inherit CustomWebPart1 from WebPart class (see code below)
    • Override CreateChildControls and RenderControls events (see code below)
    • Create a label control and assign its text property inside CreateChildControls events ( see code below)
    • The code looks like the following
    • using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Web;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;

      namespace CustomWebpart
      {
      public class CustomWebPart1 : WebPart
      {
      Label lblName;
      protected override void CreateChildControls()
      {
      lblName = new Label();
      lblName.Text = “This is the Custom Webpart created in MOSS training”;
      this.Controls.Add(lblName);
      base.CreateChildControls();
      }
      public override void RenderControl(System.Web.UI.HtmlTextWriter writer)
      {
      base.RenderControl(writer);
      }

      }
      }

    • Build the solution.  The assembly CustomWebpart will get created

customwebpart_vs_customwebpart

  • Strong Naming the Assembly
    • Go to Project Properties page (Right click the project and select properties)
    • Select Signings section.  Check the ‘Sign the assembly’ checkbox.
    • Select ‘Create New’ in the ‘Choose the strong name file’ drop down box.
    • In the dilog box enter a key name say snKey and click ok. (Uncheck the password checkbox).
    • customwebpart_vs_createstrongname
    • Now the assembly is strong named. Build the project.
  • Place the dll in the bin folder of the application’s virtual directory

customwebpart_vs_iis

  • Place the dll in the GAC and note down its name and public key token

customwebpart_gac

  • Add the assembly as Safe Control in the application’s web.config.

customwebpart_webconfigjpg

  • Thats all. Do an IISRESET
  • Adding newly created web part to Site Collection
    • Go to SiteActions –>Site Settings
    • In the Galleries section choose Web Parts
    • In the Web Parts gallery click on New
    • In Add new web parts page, scroll through the list and select the web part you just created.
    • Click on the button Import Web Part to the Gallery.
    • Now, go to the page where you want to add the newly created custom web part
    • SiteActions –> Edit page
    • Click Add Web Part. In the Add Web Part box, select the custom web part.
    • Click Ok.
    • Now you can see the custom web part is added to the page.

Hope this is useful.

Please leave your comments.

Thanks

Advertisements

8 Responses to “Creating Custom Web Parts in MOSS 2007”

  1. […] This post was Twitted by RamprasadN – Real-url.org […]

  2. Jeff said

    Code above might need “protected”
    -protected override void RenderContents(HtmlTextWriter writer)

  3. osama bader said

     Hi i want to ask about a very annoying problem i found when trying to implement what is written in this tutorial and al the others i have found in the last couple of days,this problem is that i implement all the instructions listed in the tutorial but when i try to find the web part i have added in the Gac folder using site settings-->web parts-->new i do not find it in the list so i cant add it to the web part gallery.
    
    
    	
  4. Myke said

    I did all but I can’t seem to know what to do with the 6th step.

    Add the assembly as Safe Control in the application’s web.config.

    I added this line on my web.config

    But I can’t seem to see it on the list of web parts.

  5. Navaneetha Krishnana D said

    Hello Ramprasad

    hope you are doing well 🙂

    could you please guide us for Add the assembly as Safe Control in the application’s web.config.

    how to add safe control in web.config for your program

    Thanks in advance
    Navaneetha Krishnan D

  6. Sekhar said

    Hi

    I followed the tutorial as it is, but i did n’t got the webpart in the Gallery to add.

    Can you suggest the possible errors?

    Sekhar

  7. Paul Chapman said

    Hello Ramprasad,

    Firstly, thank you for this tutorial, I just used it to create my first custom MOSS WebPart!

    Second, I struggled with a couple of things, same as some other people above. What worked for me was:

    web.config:

    * note, the NameSpace needs to be whatever your NameSpace is called in the .cs file in Visual Studio

    Once I had added this to web.config I still could not see the webpart. To get it to show up I had to go to Site Settings > Web Parts (under Galleries) > New

    I was then able to see my custom webpart, give it a friendly name and (after clicking Populate Gallery and then choosing to Edit the webpart) define where it showed up in the Web Part Gallery.

    That is what worked for me, hope it helps others. Thanks again Ramprasad.

  8. Paul Chapman said

    Hello Ramprasad,

    Firstly, thank you for this tutorial, I just used it to create my first custom MOSS WebPart!

    Second, I struggled with a couple of things, same as some other people above. What worked for me was:

    web.config:

    * note, the NameSpace needs to be whatever your NameSpace is called in the .cs file in Visual Studio

    Once I had added this to web.config I still could not see the webpart. To get it to show up I had to go to Site Settings > Web Parts (under Galleries) > New

    I was then able to see my custom webpart, give it a friendly name and (after clicking Populate Gallery and then choosing to Edit the webpart) define where it showed up in the Web Part Gallery.

    That is what worked for me, hope it helps others. Thanks again Ramprasad.

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: