Wednesday, March 12, 2008

Developing SharePoint Web Part using Visual Studio

Refer my earlier posts on installing Microsoft Office SharePoint Server (MOSS) 2007 and creating website if you need more information.

This walkthrough provides the steps for creating a basic custom SharePoint Web Part using Visual Studio 2008 or 2005. It is a very simple Web Part that displays the logged-in user name with a Hello message.

1. Create a new Visual C# Class Library Project in Visual Studio.
  • Start Visual Studio.
  • On the File menu, point to New, and then click Project.
  • In the New Project dialog box, click Visual C# Project Type, and then select the Class Library template.
  • Type HelloWebPart as the name and specify the location for the project files, and then click OK.
2. Add reference to System.Web..
  • On the Project menu, click Add Reference.
  • On the .NET tab, double-click System.Web.
  • Click OK.
3. Add Namespace Directives.
  • First, rename the class file from Class1.cs to Hello.cs (Confirm by clicking yes).
  • Add the following using directives near the top of your code.
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
4. Inherit From the WebPart Class.
  • Derive the Hello class from System.Web.UI.WebControls.WebParts.WebPart class.
    namespace HelloWebPart
    {
    public class Hello: WebPart
    {

    }
    }
5. Override the RenderContents base class method.
  • This method is used to render contents on the web part. Here we are displaying a Hello message to the current user.
    protected override void RenderContents(HtmlTextWriter writer)
    {
    writer.Write("Hello, " + this.Context.User.Identity.Name);
    }
6. Build this project to get the assembly HelloWebPart.dll
If we need to render a control instead of contents, override the CreateChildControls and RenderControl method as shown below:
     protected override void CreateChildControls()
{
//Create a Calendar control and set its properties.
Calendar helloCalendar = new Calendar();
helloCalendar.Enabled = true;
helloCalendar.ShowGridLines = true;
helloCalendar.ShowTitle = true;
helloCalendar.EnableViewState = true;
helloCalendar.SelectedDate = DateTime.Now;

//Add this Calendar to the Web Part.
this.Controls.Add(helloCalendar);
}
public override void RenderControl(HtmlTextWriter writer)
{
this.RenderChildren(writer);
}
In the above example, we are creating a calendar control and setting its properties and then adding it to the web part. The RenderControl method is used to call the RenderChildren method, which causes the children controls to be rendered on the particular HtmlTextWriter passed as a parameter to the method.

If this assembly needs to be strong named (to enable GAC deployment), follow the below steps:
  • Launch the Visual Studio Command Prompt.
  • Type the following command to generate a key pair file:
    sn –k c:\keypair.snk
  • That will generate the key pair file at c:\
  • Refer this file in the web part project’s AssemblyInfo.cs file as below:
    [assembly: AssemblyKeyFile("c:\keypair.snk")]
  • Make sure that the version is also specified here as below:
    [assembly: AssemblyVersion("1.0.0.0")]
    [assembly: AssemblyFileVersion("1.0.0.0")]
  • Build the project to generate a strong named assembly.
If you are using Visual Studio 2008 or higher, you can can skip all these steps and then go to the Signing tab of the project properties, and choose 'Sign the assembly' option.
We will see how to deploy this web part on SharePoint Server in the coming posts.

11 comments:

  1. Hi, can I insert a WPF control inside a webpart?

    ReplyDelete
  2. It is possible to create a Silverlight webpart but I am not sure about a WPF control.

    ReplyDelete
  3. Found a typo...

    sn –k c:\kepair.snk

    should be...

    sn –k c:\keypair.snk

    ReplyDelete
  4. Can you post code for silverlight web part for sharepoint.

    ReplyDelete
  5. Wonderful article! Just what I needed. thx!!

    ReplyDelete
  6. This has been tremendously helpful.
    Thank you.

    ReplyDelete
  7. Thanks for the post. it helped me a lot.

    ReplyDelete
  8. Greate tutorial man....

    ReplyDelete
  9. Thanks a lot. Very clear for newbies like me.

    ReplyDelete
  10. Hi,The use of the internet for the purposes of commerce is astounding in Web Design Cochin. If you are serious about promoting your business and services and do not have a web presence, then you need to seriously consider creating your own website. Thanks....

    ReplyDelete