Click here to Skip to main content
13,861,547 members
Click here to Skip to main content
Add your own
alternative version


34 bookmarked
Posted 28 May 2009
Licenced CPOL

Developing and Deploying Custom Web Parts for SharePoint Portal 2007

, 31 May 2009
Rate this:
Please Sign up or sign in to vote.
Developing and Deploying Custom Web Parts for SharePoint Portal 2007


In this article, I am going to show how to develop and deploy a custom web part which is used to connect to a database (SQL) and delete data from a database.

Microsoft Office SharePoint Server MOSS (2007) provides a lot of basic web parts that will have some limited functionality. If you need a web part which has different functionality, you have to go for Custom Web part which has to be built on your own or can be bought from third parties. You build web parts whenever your needs exceed what is available out of the box or from third parties.

A. What is a Web Part?

Components that display content on a page and are the primary means for users to customize/personalize pages.


A name given to components or units that can be installed and customised easily by end users of a SharePoint page, in order to provide extended functionality.

B. Developing

  1. To get started with creating a Custom Web Part for MOSS 2007 in Microsoft Visual Studio 2005, Open the IDE and create a new C# project, Select Class Library as Project Type. Name it as NewWebPart.


  2. In the Project explorer view, rename the Class1.cs with GauravWebPart.cs to be consistent with this example; this will result in renaming the class name as well. With the help of “using” keyword, include the namespaces as shown in the code example below.
  3. The CreateChildren control is the same as in .NET 1.1, that it would create and add controls to this Web Part Class. In this case, I have only added a button and label object, which is in the method CreateDeleteButton.



  4. The RenderControl method is an override for the WebPart Base, this causes the Children Controls to be rendered on the particular HtmlTextWriter passed as a parameter to the method.


  5. I have used two other classes
    1. SQLServerDataAccess.cs where you can have business rules, etc. and
    2. SqlServerHelper.cs for database access, it takes the connection string from the web.config which is accessed from the GetConnectionString method in the SQerverHelper class. 
  6. Next, use the following code snippet to add the AllowPartiallyTrustedCallers attribute (located under the System.Security namespace) to the code's AssemblyInfo.cs file:
    [assembly: AllowPartiallyTrustedCallers()] 
  7. Give your assembly a strong name. Just go into the Project Properties and then go to the "Signing" tab (see Figure 1) to do this. Alternatively, you can use the sn.exe command line tool to generate strong names for you.


  8. Build the project and on successful build, you are ready to Deploy the Web Part to the Portal Site.

C. Deploying

  1. Deploy the assembly to the Assembly Folder (GAC) (requires the assembly to be strong named). Drag and Drop the assembly (DLL) file, named GauravsWebPart.dll from the source folder to the Gac Folder (C:\WINDOWS\ASSEMBLY).


  2. The Assembly (DLL), GauravsWebPart.dll will be copied into the GAC folder.


  3. Now take the public key token for the GauravsWebPart.dll assembly (DLL), select the file first and then click mouse right button, and select the properties.
  4. The properties will open and then select the public key token, copy it, and paste the key into any text file.
  5. Open the web.config file of the SharePoint application from the following path:


  6. Add the following attribute, in the last of <sharepoint><safecontrols> element.
    <SafeControl Assembly="GauravsWebPart, Version=, Culture=neutral, 
    PublicKeyToken=4fd5e9f6385747e4" Namespace="GauravsWebPart" 
    TypeName="*" Safe="True" /> 


  7. Now replace the public key token, copied from GAC, with the existing key highlighted in yellow colour.
    <SafeControl Assembly="GauravsWebPart, Version=, Culture=neutral, 
    PublicKeyToken=4fd5e9f6385747e4" Namespace="GauravsWebPart" 
    TypeName="*" Safe="True" /> 
  8. Now copy and paste the same GauravsWebPart.dll assembly (DLL) to bin folder of the SharePoint application, the path is as follows:

    However there is another workaround for putting the assembly into the portal’s bin folder again ad again each time the Web Part Project is built with changes. Right click on the project name (NewWebPart) in the VS.NET 2005 IDE and click properties. Under the Build page, paste the same path copied from inetmgr console into the Output Path as shown in the figure below. This will result in the latest assembly automatically being deployed to the bin folder every time the project is built.


  9. Run the SharePoint application and open the Home Page and go to SiteAction->SiteSetings, then click the on the Modify all site Settings link.


  10. Click on the Web Parts link under the Galleries section.


  11. After opening the web part gallery, click on new button highlighted in the following figure:


  12. Select the checkbox of GauravsWebpart.GauravWebpart web part, and click the button Populate Gallary.


  13. Now the web part gallery will open again with the name of our new web part, highlighted in the following figure:


  14. Now open the site where this web part needs to be deployed:


  15. Click on SiteNavigation->EditPage.


  16. Now click on “Add Web Part” link, as shown in the following figure:


  17. A New window will be open “Add Web Part to Main”, and then go to ‘Miscellaneous’ section and select the checkbox of the Delete web part, then click ‘add’ button.


  18. The Web part will be come on the site.


  19. At last, click Exit Edit Mode” comes under the “SiteSection”.


  20. The Web part now appears on the screen.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Technical Lead
India India
Gaurav Mehta is from Delhi, India. He has to his credit, a Bachelor’s Degree in commerce and a Master's in Computer and Software Applications. He started his career with C and C++ and then moved on to Microsoft Technologies. He has been in this industry for more than 6.5 years. Gaurav is primarily involved in requirement analysis; designing and developing web and win apps.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
aliusmankhan29-Mar-12 16:23
memberaliusmankhan29-Mar-12 16:23 
Bug[My vote of 1] Not a complete article Pin
aliusmankhan29-Mar-12 16:22
memberaliusmankhan29-Mar-12 16:22 
GeneralMy vote of 5 Pin
tinh.khau18-Jul-11 11:09
membertinh.khau18-Jul-11 11:09 
GeneralNee Help on Deploying WebPart Pin
Pushkarr7-Jul-09 22:12
memberPushkarr7-Jul-09 22:12 
GeneralRe: Nee Help on Deploying WebPart Pin
Gaurav-Mehta12-Jul-09 22:59
memberGaurav-Mehta12-Jul-09 22:59 
GeneralRe: Nee Help on Deploying WebPart Pin
Pushkarr12-Jul-09 23:42
memberPushkarr12-Jul-09 23:42 
GeneralRe: Nee Help on Deploying WebPart Pin
Gaurav-Mehta13-Jul-09 1:18
memberGaurav-Mehta13-Jul-09 1:18 
GeneralRe: Nee Help on Deploying WebPart Pin
Pushkarr13-Jul-09 1:40
memberPushkarr13-Jul-09 1:40 
GeneralMake your life easier - Use Smart Parts Pin
Member 340660123-Jun-09 4:41
memberMember 340660123-Jun-09 4:41 
GeneralDon't get annoyed ... Pin
rolfg4-Jun-09 3:06
memberrolfg4-Jun-09 3:06 
GeneralMy vote of 2 Pin
SchaeferFFM30-May-09 9:52
memberSchaeferFFM30-May-09 9:52 
GeneralRe: My vote of 2 Pin
Gaurav-Mehta30-May-09 15:03
memberGaurav-Mehta30-May-09 15:03 
GeneralRe: My vote of 2 Pin
Not Active1-Jun-09 3:00
mentorNot Active1-Jun-09 3:00 
General[My vote of 1] Nothing new here Pin
Not Active29-May-09 5:22
mentorNot Active29-May-09 5:22 
GeneralRe: [My vote of 1] Nothing new here Pin
Gaurav-Mehta29-May-09 5:51
memberGaurav-Mehta29-May-09 5:51 
General[My vote of 2] Never manually edit the web.config Pin
Not Active29-May-09 5:20
mentorNot Active29-May-09 5:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190214.1 | Last Updated 1 Jun 2009
Article Copyright 2009 by Gaurav-Mehta
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid