Click here to Skip to main content
15,868,016 members
Articles / Web Development / ASP.NET

Developing and Deploying Custom Web Parts for SharePoint Portal 2007

Rate me:
Please Sign up or sign in to vote.
4.86/5 (25 votes)
31 May 2009CPOL4 min read 102.4K   1.2K   34   16
Developing and Deploying Custom Web Parts for SharePoint Portal 2007

Introduction 

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.

Or

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.

    A1.PNG

  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.

    A2.PNG

    A3.PNG

  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.

    A4.PNG

  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:
    C#
    [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.

    A7.PNG

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

    1.PNG

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

    2.PNG

  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:

    \C:\Inetpub\wwwroot\wss\VirtualDirectories\80

  6. Add the following attribute, in the last of <sharepoint><safecontrols> element.
    XML
    <SafeControl Assembly="GauravsWebPart, Version=1.0.0.0, 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.
    XML
    <SafeControl Assembly="GauravsWebPart, Version=1.0.0.0, 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:
    C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin.

    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.

    MA5.PNG

  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.

    MA8.PNG

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

    MA9.PNG

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

    MA10.PNG

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

    M5.PNG

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

    6.PNG

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

    7.PNG

  15. Click on SiteNavigation->EditPage.

    8.PNG

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

    9.PNG

  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.

    M10.PNG

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

    11.PNG

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

    12.PNG

  20. The Web part now appears on the screen.

    13.PNG

License

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


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


http://www.GauravMehta.com

http://www.GauravMehtasBlog.com




Comments and Discussions

 
GeneralDon't get annoyed ... Pin
rolfg4-Jun-09 2:06
rolfg4-Jun-09 2:06 

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.