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.
- On the Project menu, click Add Reference.
- On the .NET tab, double-click System.Web.
- Click OK.
- 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;
- Derive the Hello class from System.Web.UI.WebControls.WebParts.WebPart class.
namespace HelloWebPart
{
public class Hello: WebPart
{
}
}
- 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);
}
If we need to render a control instead of contents, override the CreateChildControls and RenderControl method as shown below:
protected override void CreateChildControls()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.
{
//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);
}
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.
We will see how to deploy this web part on SharePoint Server in the coming posts.
Hi, can I insert a WPF control inside a webpart?
ReplyDeleteIt is possible to create a Silverlight webpart but I am not sure about a WPF control.
ReplyDeleteFound a typo...
ReplyDeletesn –k c:\kepair.snk
should be...
sn –k c:\keypair.snk
Can you post code for silverlight web part for sharepoint.
ReplyDeleteWonderful article! Just what I needed. thx!!
ReplyDeleteThis has been tremendously helpful.
ReplyDeleteThank you.
Glad to know that. :)
ReplyDeleteThanks for the post. it helped me a lot.
ReplyDeleteGreate tutorial man....
ReplyDeleteThanks a lot. Very clear for newbies like me.
ReplyDeleteHi,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