In this article I will explain how to develop webparts in sharepoint.
Before going to development section, first we need to know some thing about webparts.
What is a Webpart?
Webpart is nothing but a normal control like Textbox,Button..etc, but it is a integrated set of control. Confusing, Let me explain. We can we make webpart by increating several other controls like Textbox, Label and other controls, that enables the end users to modify the content.
Webparts in Sharepoint is nothing but a code component created in .Net to provide functionality for a web site.
A webpage can contain one or more webpart zones, each zone can contain one or more webparts.
We can easily add a webpart to our website in Sharepoint. To add a webpart to our page
Site Actions --> Edit Page --> Add a Webpart --> Select webpart --> Click on Add.
In this way you can easily add the exisiting webpart to our webpage in Sharepoint.
As a developer, we can develop our own webpart for sharepoint website. Creating the webpart is very easy with .Net Framework.
To create Simple webpart in Sharepoint
Open visual studio File--> New-->Project--> select Class Library for C#
and enter name for project as NewWebpart and Rename the class as SampleWebpart.
After Creating the project, you need to add the Sharepoint DLL for your project.
For this right click on solution Explorer and select Add Reference . Browse for Microsoft.Sharepoint.dll file and click ok.
With default installation of Sharepoint, you can find this dll file at below location.
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.SharePoint.dll
You have to add one more reference to your project to develop webpart in sharepoint, i.e, System.Web. To add this reference right click on Add Reference, select .Net tab in that you have to select System.Web and click ok.
So, you successfully establish the environment to develop Webpart in Sharepoint 2007.
Import namespace for webparts to your class. Name space for webparts is System.Web.UI.WebControls.WebParts;
The code to develop your first webpart is shown below.
using System; using System.Collections.Generic; using System.Text; using System.Web.UI.WebControls.WebParts; namespace NewWebpart { public class SampleWebpart: WebPart { protected override void RenderContents(System.Web.UI.HtmlTextWriter writer) { writer.Write("<h1>Hi, This is Sample Webpart</h1>"); } } }
If you observe the code, we inherit the basic webpart class to develop new webpart.
After that, we override the RenderContents method to display our own content.
Still now, we write the code to develop the sharepoint webpart. Now you have to do signing for your webpart to register in GAC(Global Assembly Cache). For this Right click on solution Explorer and select properties.
In the properties window select Signing.
Select the "sign the assembly" check box, in the drop down list select new.
Give the name for your key file.
Now save all Items and Build project to generate dll file for your project.
Open visual studio command prompt and type
gacutil /i C:\Projects\NewWebpart\bin\Debug\NewWebpart.dll
Execute above line to add assembly to the GAC(Global Assembly Cache).
Now we need public token key for this Assembly. For this I executed following command in the command prompt.
sn -T C:\Projects\NewWebpart\bin\Debug\NewWebpart.dll
Then you can get the public key token value. Copy and paste this key at some place.
Here I got the public key token as 352b1e8e6b9daeec.
Now we have to register this Sharepoint Webpart as a safe control in the web.config file pf the web application in which the webpart is to be used.
To find out the web.config file for you sharepoint
C drive-->Inetpub-->wwwroot-->wss-->VirtualDirectories-->80
Note: Here port number for my site is default i.e, 80.
Open the Web.config file in directory 80. In the Sharepoint section, under safecontrol node enter
<SafeControl Assembly="System.Web, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=352b1e8e6b9daeec" Namespace="NewWebpart" TypeName="*" Safe="True" AllowRemoteDesigner="True" /%gt;
to register your webpart as SafeControl. Where PublicKeyToken value is the value which one we got by submitting our assembley to the GAC.
Now you have to restart the IIS. Opne visual studio command prompt and execute iisreset.
You successfully develop the Sharepoint webpart and successfully submitted to the sharepoint site. Now you have to add this webpart to the webpart gallery.
For this
Site Actions--> Site Setting-->under Galleries click on Webparts Select New tab--> check the NewWebpart.SampleWebpart--> Click the Populate Gallery
You successfully submitted your webpart to the Webpart Gallery.
Now you have to add this webpart to you page.
To add webpart to your page
Site Actions-->Edit Page --> click on Add web part--> under Miscellaneous section --> Select SampleWebPart as shown below.
After adding web part to your page, your page look like below.
In this way you can develop your own Web Parts in Sharepoint 2007.
You can download source code for this article from below.