Creating a custom hello world SharePoint web part

1) Create a new class library project.

2) Add reference to system.web.dll

3) Inherit the following class

System.Web.UI.WebControls.WebParts.WebPart class.

4) Override the Render method.

using System;

using System.Collections.Generic;

using System.Text;

using System.Web.UI;

namespace MyWebPart

{

public class MyFirstWebPart : System.Web.UI.WebControls.WebParts.WebPart

{

protected override void Render(HtmlTextWriter writer)

{

writer.Write(“<font name=’Georgia’><b>Hello World </b></font>”);

}

}

}

Put the following attribute in your assemblyinfo.cs file

[assembly: AllowPartiallyTrustedCallers]

5) Strong sign the assembly and install it in GAC.

6) Open the web.config of your site.

7) Make a safecontrol entry within the web.config for your webpart.

<SafeControl Assembly=MyWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4e225690feb84c40 Namespace=MyWebPart TypeName=* Safe=True />

The name of the assembly,it’s version, culture and public key token information can be found by right clicking the assembly within gac and selecting properties.

Or to get the public key token you can open the .NET Framework command prompt and type the following command sn -t “assemblyname.dll”

We can deploy assembly in the bin directory of the web application also, but in this case it would be partially trusted and could result in security exception which could have been unhandled, so we need to make the following change in the web.config of the application

<trust level=”WSS_Minimal” originUrl=”” />

to

<trust level=”WSS_Medium” originUrl=”” />


Go to webparts gallery within your site, click on new, select your webpart and populate the gallery.

That’s it!!


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s