This blog is moved to
http://amalhashim.wordpress.com

Monday, November 2, 2009

Building and Deploying a Web Part

In this article am going to explain how we can develop a simple web part and deploy it in Sharepoint.

There are two way we can build a web part.

1. Using Sharepoint extensions for Visual studio.
2. Developing a Class library and manually deploying it in Sharepoint.

Sharepoint extensions for visual studio can be downloaded from the following location

Download

Once you have installed the extension, from Visual Studio->File->New Project->Share Point->Web Part

This will provide a template with all setting ready for building a web part. Once you have done with the web part, you can deploy it directly from Visual Studio. For doing that open the properties dialog by right click the project from solution explorer, go to the debug tab and enter the sharepoint web application URL and save the settings. You can use the same right click context menu for deploying the web part directly to sharepoint server by clicking the deploy menu item.

Manual development and deployment steps

  1. From visual studio File->New Project->C#->Class Library

  2. Right click and Add Reference of System.Web

  3. Rename Class.cs to NewWebPart.cs

  4. Open NewWebPart.cs and add the following code

  5. using System;
    using System.Collections.Generic;
    using System.Text;

    namespace NewWebPart
    {
    public class NewWebPart: System.Web.UI.WebControls.WebParts.WebPart
    {
    protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
    {
    writer.Write("Hello World!!!");
    }
    }
    }

  6. Right click project->Properties->Signing, and sign the assembly with a key

  7. Now build the project

  8. Open visual studio command prompt and use the following command

  9. gacutil /i C:\bin\NewWebPart.dll
    Replace C:\bin\NewWebPart.dll with the location of NewWebPart.dll in your machine

  10. Open your sharepoint web application's web.config file and add the following entry

  11. <SafeControl Assembly="NewWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=902d73ff104745c3" Namespace="NewWebPart" TypeName="NewWebPart" Safe="True" />

    Replace 902d73ff104745c3 with the token of assembly. You can find it by opening c:\windows\assembly

  12. Open Site Setting from Site Actions menu

  13. Click on Web Parts

  14. Click New

  15. Select NewWebPart.NewWebPart and Click Populate Gallery

    Now you can add web part on your site by editing the page.

    That's it!!!

No comments: