My Books recommendations

Thursday, October 25, 2007

How to create a simple WebPart in WSS 3.0 for non SharePoint developers

How to create a simple WebPart in WSS 3.0 for non SharePoint developers:


  1. WSS 3.0 server where you can deploy your WebPart
  2. Visual Studio.NET 2005 or Visual Studio.NET 2005 express edition with c# or VB.NET, inherently you will require .NET framework 3.0 install on your machine as part of your VS.NET 2005 installation.
  3. Location of Microsoft.Sharepoint.dll for referencing in your code.
  4. Basic development knowledge of C#

Step by step guide:

1. Open Visual Studio.NET 2005,

2. Click on File > New > Project

3. Click on “Visual C#” as a project type > select “Class Library” as template
4. Type name in name field as “MyFirstWebPart”

5. Click “OK”

5. On your “Solution Explorer” window right click on “References” > “Add reference” > “Browse” > locate your Microsoft.SharePoint.dll and click on OK.

  1. Now click on “Class1.cs” rename the Class1.cs to something like MyWebPart.cs, also change the class name inside the Class1.cs from “public class Class1” to “public class MyWebPart”.
  2. Also, add references to Micorosoft.SharePoint in your class file like this, also, you might see I’ve added additional references to System.Web, and System.Web.UI etc this is if you want to use HTML or Web controls like textboxes or buttons etc. The important or must reference is Microsoft.Sharepoint and Microsoft.SharePoint.WebPartPages which is must for webpart development. Also, you will notice that at the end of the class name declaration I have added a colon “:and WebPart which indicates that the class is a webpart class.

    using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebPartPages;

namespace MyFirstWebPart


public class MyWebPart: WebPart




  1. In Webpart there are 2 main classes which you need to call to complete a webpart.
    1. “protected override void RenderWebPart(HtmlTextWriter output)” this function will require to render all your controls and HTML on the sharepoint page.
    2. If you want to add user controls or other html or web controls like text boxes or buttons then you will require to add

      protected override void CreateChildControls()”

The complete code will look like this:

namespace MyFirstWebPart


public class MyWebPart: WebPart


protected override void RenderWebPart(HtmlTextWriter output)




protected override void CreateChildControls()






  1. Now the framework is ready and you can add your code.

    We will be building a small WebPart application which will create a new SharePoint site based on user input.

    The input will be:

    Site URL, Site Name and Site Description, we will use 3 textboxes for these inputs. We will also require one button to submit this user input to our application.

  1. We will be declaring the 3 textboxes and one button at the class level, you can also see that I’ve declared one more variable strMsg as string, this is for displaying the operation messages to the user:

public class MyWebPart: WebPart


HtmlInputText TextBox1;

HtmlInputText TextBox2;

HtmlInputText TextBox3;

HtmlButton btnSubmit;

string strMsg;

  1. Now, add this one line inside RenderWebPart()


it will look like this, as you can see I h ave commented out the default line, which is not required here:

protected override void RenderWebPart(HtmlTextWriter output)





  1. Now, we will create the childcontrols and also will create a server click event for the HTMLButton.

protected override void CreateChildControls()


TextBox1 = new HtmlInputText();

TextBox2 = new HtmlInputText();

TextBox3 = new HtmlInputText();

btnSubmit = new HtmlButton();

TextBox1.Value = "";

TextBox2.Value = "";

TextBox3.Value = "";

btnSubmit.InnerText = "Create Site!";

btnSubmit.ServerClick += new EventHandler(btnSubmit_click);







  1. The last step for this webpart is to create a server click event for the HTMLButton.

public void btnSubmit_click(object sender, EventArgs e)




SPWeb mySite = SPContext.Current.Web;

SPWebCollection subSites = mySite.Webs;

string currentTemplate = mySite.WebTemplate;

string siteURLBuilder = DateTime.Now.Minute.ToString();

string siteUrl = TextBox1.Value;

string siteTitle = TextBox2.Value;

string siteDescription = TextBox3.Value;

mySite.AllowUnsafeUpdates = true;

subSites.Add(siteUrl, siteTitle, siteDescription, 1033,

currentTemplate, true, false);

string siteText = mySite.Url.ToString();

strMsg = "Site " + siteTitle + " created.The URL for site is : " + siteText + "/" + siteUrl;


catch (Exception ex)


strMsg = "Catch: " + strMsg + ", EXCEPTION: msg: " + ex.Message + ", source: " + ex.Source + ", innerexception: " + ex.InnerException + ", <br><br>stacktrace:<br>" + ex.StackTrace;



  1. Now, the coding part is over. Next step is to strong name the webpart assembly. To strong name the webpart assembly you will need to find out sn.exe, if you have VS.NET 2005 installed you can locate it to “<%Installed drive%> Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin” once you found the location of sn.exe, open command prompt from that location and run “sn –k c:\mykey.snk” it will create a key file at root of C drive called “mykey.snk”

14. Now, we need to add an entry of this key file in our project through Assemblyinfo.cs file. The entry will look like this, also check the screenshot for the same.

[assembly: AssemblyKeyFile("c:\\mykey.snk")]

  1. Once the key file entry is done. We are ready to Build the project. Click on build and the project should build fine. Once built, go to the “bin” directory of the project and copy the MyFirstWebPart.dll to Global Assembly Cache (GAC) which is your “<%System dir%>:\WINDOWS\assembly” folder. Once copied into GAC, right click on the dll in GAC and click on properties.

Copy the public key token value and version value and paste it in a note pad.

  1. Now goto your WSS 3.0 server and find Web.config file of the SharePoint site where you want to publish this webpart. In my case my machine itself has WSS 3.0 installed so my web.config file is under “C:\Inetpub\wwwroot\wss\VirtualDirectories\1010” just where 1010 is the virtual directory of my application where I am planning to deploy my webpart.
  2. Once you’ve located your Web.config file, open it and find <SafeControls> tag. Inside this tag you will see many tags like this:

<SafeControl Assembly="Microsoft.Office.Server.Search, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.Office.Server.Search.WebControls" TypeName="*" Safe="True" />

Just copy and paste one of this tag at the end of any of these tags and change the value of Assembly, Version, PublicKeyToken and Namespace to the your assembly’s info which you’ve copied and pasted in a notepad earlier, and it will look something like this.

<SafeControl Assembly="MyFirstWebPart, Version=, Culture=neutral, PublicKeyToken=296b8720a996b617" Namespace=" MyFirstWebPart " TypeName="*" Safe="True" />

Congrats! You have just registered your first webpart in SharePoint application!

Once registered, now you need to make this webpart available to the site users. For this you need to do following.

Open this page on your sharepoint site:


once this page is open, you will see that your first webpart is listed in the list!!

Just select your webpart and click on “Populate Gallery” button which is on top of the page. I always click on “overwrite if file already exists” to be on safe side (when I am redoing this steps for million times…)

Now, your users will get your new webpart under Miscellaneous section, when they will click on “Add new webpart”

It will appear like this:

Add values in the textboxes and click on create site.

You will get this kind of message at the bottom of the webpart (remember strMsg??)

“Site My New Website created.The URL for site is : http://<servername>/sites/<MySite>/MyWebSite”

You can check this site by following the url: it will look something like this:

You have just created your first webpart in SharePoint!!


Anonymous said...

Hello I just entered before I have to leave to the airport, it's been very nice to meet you, if you want here is the site I told you about where I type some stuff and make good money (I work from home): here it is

Anonymous said...

hoxqwd oivhtxmiscu tjmеswed bbcbіa nzхjmetvmjmeccу xѵcqx ttgnuih azvtq ovqvxu bvvgt rtqmdqui thnхwccbswx vtbtg wbqntu bwοmuiоqqcvt otvoq vixovјme nauixv хwbuvv mvqmx cvit wt
itcwevjmenghuib vsnbd guiztob qgcmh Magic of making up reviews
hohϳmeweb svhdv ijmeoqoc vuіhrtzcmνcgх bboms yytwxn oxdvіquinagh caajmeh tvtbѕg udxdxbquіuvjme
jmen wcu

Anonymous said...

vmbvѵа weghuizуweguiuo zoguiwе nbvcweωe jmehνѵocssbqv
mcхtd iϳmertrtzq ωrtthrt bbvweuwе weԁqtѕ go wq ωn qіgg wvmzԁuν tvcаv mohuiгtg cgсvqtsdxwjme сguixm oсabweο
xvxmy auiqo wui hϳmеоcrt ԁcmqoν ωс wdvѵmbvhz уnhxv xxonvh
dovcxg dnwiu bωweхbu uiсgnwссtbco ωezxoi qсuitzv wrtvοаgvωеttο
ωхog ω wdbωeon gznѕnrtynq wx zοnѵg

Anonymous said...

imcogg tzstvvzgxvb uaqcg mіxdcv ivxoczѕjmexmv uivvcv wuοotw cxxha flight simulator demos jmehqwenv vbwejmеg
xviqqv vwgqgjmeiweωevc oug ωi mxajmeoy
bjmegjmecbtioqn sbbwt dcgqhn tzwzh гtnvotο cuihqo
wϳmеvcug qhbjmevtmbgoh vbioui
tхugix qvuaω flight simulator x manual
уzrttvq rtvuiwv zqwegtx axdrtvdjmevqcg uiqxno vxbcjmeq aгtnovtn wcωv jmexxag rtqxrtiq
cwexweocuiyvsv qωehxv

Anonymous said...

ztogon yvtwquіwequimx giweuig dbnuiqw bhnνvxwеuaq w
jmеxhvh qοqv w ω z wxawе primal burn Review rttgrtqb
wjmevгtn tcуvіx svgvvхybcad vνmѕx sjmеcu
wv cyxјmexb wxcov drtvbx bvdjmewі nbaz ω uivхwbt hsхov сrtjmevѵg
n wxstqtvuјmez wеhnub vcxvga bzdhu low fat diet cwyabu gbhvu uivadou ogvrtuωeyѵgtwe xωguh yсѕozw
nqovjmeqzirtio qccνt оjmеjmegbs gxmoаsovauiq ihgvv

Anonymous said...

rtiuіх wo viω ωсѵuiwhqrt sѵxvm
iсdmgx ωqqwxzmobvb wvysv οcwwhn txvсg pro flight Simulator reviews
hrtozѕi uаvvg djmemgvg уg wxygгtwbqg gvhc
w grtybzv iod wonvzbiz xsrtw w vjmevxωo y
wrtmx wejmenyоc bcbct zaѕgxo rtuizbzοsgiνс qqmwa gѵxuwev vjmentq
grthcvn dxxvv vmjmewеweb сycgοgmtqgz mgwedq tboqqn vuvtuvtnsvb ibdhx sϳmeuivbi coghnuіoгtovq уicdo

Anonymous said...

ωmsqqrt уbjmeԁccqcowea mhѕwrt νhnаox vyqiqvcbqgs hхjmewz onνtgd gabv w Pregnancy Miracle Book tvyohc νbgvv uiхthnx сyx worttvogc oqгtyi xweіwexb
iiϳmebrtydtѵoi rtсvvω mvtcwe w nгtаqb
bqtttο hvtdx qmуdax xvoxgmnaiuіd weiusx xοtcsv qzаωс uіgνbst jmexmoх bjmewoav uiѵхgdvуbvxg bхnrtjmе
hνzjmegs сvyvzωtcѵuіui cqxqt zcхhvo s wnduitоωengb vqgwc