Creating a basic sharepoint webpart
This programe task includes the steps for creating basic custom windows sharepoint services web parts.It is a simple web part that allows you to
change the web part's title property, which is a windows sharepoint
services web part base calss property that sets the text in the title bar of the web part.
Prerequisites:
Microsoft visual studio 2005
Windows sharepoint services 3.0
Step1:Create a web controle library project
1.start visual studio 2005
2.on the file menu,point to new, and then click project
3.In the new project dialog box,click visual c# or visual basic projects,and then select the web control library
4.Type simple web part as the name and specify the location for the project files and click on ok
Step2:Add a reference to Microsoft.sharepoint.dll
1.On the project menu,click add reference
2.On the .net tab ,double click sharepoint services
3.click on ok
Step3:Set the version number and setup partially trusted callers
1.In solution explorer,double click the assembly info file
2.Edit the line:
[assembly:AssemblyVersion("1.0.*")]
So that it reads:
[assembly:AssemblyVersion("1.0.0.0")]
3.Add the following line to the top of the file
using system.security;
4.Add the following line to the bottom of the file
[assembly:AllowPartiallyTrustedCallers]
5.Rename the class and namesapace
1.Rename the default class by selecting web custom control1 in solution explorer,right click,click rename and type simple webpart as the file name.
2.For c# change the web part name space by editing the line;
namesapace simplewebpart
So that it reads:
namesapce MyWebparts
Step5:Add a namespace directive
Add the following using or imports directive near the top of your code:
using Microsoft.Sharepoint.Webpartpages;
Step6:Inherit from the webpart class
.Replace this line of code:
public class webpart:webcontrol
.with this line
public class simplewebpart:webpart
Step7:Use the render web part method
.Replace this line of code
protected override vide render(HtmlTextWriter Output)
.with this line
protected override vide render webpart(HtmlTextWriter Output)
Step8:Define the logic and rendering of your webpart
1.create two user interface objects by adding the following code near the top of the simple webpart file
Button savetitle;
Textbox newtitle;
2.Create the button handling event by adding the following code inside the calss
public void savetitle_click(object sender,eventargs e)
{
this.title=newtitle.text;
try
{
this.save properties=true;
}
catch(exception ex)
{
this.title="error:"+ex+message;
}
}
3.override the create child controls()method by adding the following code inside the class
protected override void createchildcontols()
{
new title=new textbox();
newtitle.text="";
controls.add(new tiltle);
save title=new button;
savetitle.text="set webpart title";
savetitle.click+=new eventhandler(savetitle_click);
controls.add(savetitle);
}
4.Replace the render webpart()method with the following code
protected override void RenderWebpart(HtmlTextWriter Output)
{
renderchildren(output);
}
Step9:Create a valid strong name for assembly
1.In the solution explorer,rightclick on the simple webpart project and click properties
2.In the simple webpart properties,click the signing tab on the left side
3.On the signing tab,check the sign the assembly checkbox
4.select the new in the choose a strong name key file drop-down menu
5.In the create strong name key dialod,type simple webpart key and uncheck protect my file with a password
6.now, the assembly will be signed when it is build
Step10:Build your webpart
On the build menu,click build solutions
Step11:Cpoy your dll to BIN directory
1.on the file system,locate the simple webpart.dll file
2.Copy the simple webpart.dll file from the output directory to the web application root bin directory
Step12:Increase the default trust level and add a safe control entry for your webpart
.Create a custom policy file for your assembly, or
.Install your assembly in the global assembly cache,or
.Increase the trust level for the entire vertual server
Step13:Create a DWP file to deploy your webpart
1.copy and paste following xml into new text file
2.save this file as simplewebpart.dwp in the bin directory of web application root
Step14:Import your webpart into a webpart page
1.Nvigate to the page on your sharepoint site where you want the web part to be accessible.
2.In the webpart page ,click the site actions ,and select site settings.
3.Click webparts under galleries heading.
4.On the tool bar click upload .
5.Click browse and select .dwp file that you created
and click on ok.
6.Navigate back to the webpart page and click on site actions and then select edit page.
7.If you preffered zone,click on add a webpart and chek the box next to my simple webpart in the dialog box and click on add.
8.Type some text in he text box and click set webpart title to test the webpart.
