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.
- Create custom web part control in Visual Studio
- Strong name the created web part assembly
- Place the assembly in the bin directory in the Virtual Directory of the web application
- Place the assembly in the GAC
- Notedown the assembly name, version and public key token by looking into the assembly properties in the GAC
- Add a SafeControl entry for the web part assembly in the application’s web.config file
- 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

- 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
- Build the solution. The assembly CustomWebpart will get created
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);
}}
}

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

- Now the assembly is strong named. Build the project.
- Place the dll in the bin folder of the application’s virtual directory

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

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

- 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
Twitted by RamprasadN said
[...] This post was Twitted by RamprasadN – Real-url.org [...]
Jeff said
Code above might need “protected”
-protected override void RenderContents(HtmlTextWriter writer)
Ramprasad Navaneethakrishnan said
Not necessary Jeff. Even without protected my code is running fine. I am using Visual Studio 2008, .Net framework 3.5
osama bader said
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.
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
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
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.
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.