A web part is an object that displays information in SharePoints. A web part can be added to any SharePoint page that has at least web part zone. Sharepoint has some standard web parts out of the box, but typically you will need to write a custom web part to display any non-SharePoint data or to display SharePoint data in a non-SharePoint way.
Prerequisite
Reflector for .NET
You will need to be able to get the key information for you’re the assembly that contains your WebPart. The easiest way to do this is to drop the compiled assembley into Reflector for .NET. The information will be displayed at the bottom of the screen. This can be downloaded from http://www.aisto.com/roeder/dotnet/.
Edit Permissions to the SharePoint Site Folder
You will need to be able to modify the web.config file for your site. This is typcially located at the {Local_Drive}:InetpubwwwrootwssVirtualDirectories/{Port or site}/bin
Write Permissions to the Bin Folder of the SharePoint Site
You will need to be able to deploy your class library assembly to site’s bin folder. This is typcially located at the {Local_Drive}:InetpubwwwrootwssVirtualDirectories/{Port or site}/bin
Creating the Web Part
Create the Class Library Project
In Visual Studio 2005, create a new class library project. Right click on the project in solution explorer and select add a new item. Then choose to add a new class.
Creating to the WebPart
Add the following using statements.
.
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
Modify the class so it inherits from System.Web.UI.WebContorls.WebParts.
Override the CreateChildControls method and add the code to create the visual objects your WebPart will display.
Your class should resemble the following.
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace ColdCypress.Examples.SharePoint.Ex1
{
public class Ex1 : System.Web.UI.WebControls.WebParts.WebPart
{
protected override void CreateChildControls()
{
base.CreateChildControls();
Label lbl = new Label();
lbl.Text = “This is an example web part”;
Controls.Add(lbl);
Literal litBr1 = new Literal();
litBr1.Text = “<br />”;
Controls.Add(litBr1);
TextBox txt = new TextBox();
Controls.Add(txt);
Literal litBr2 = new Literal();
litBr2.Text = “<br />”;
Controls.Add(litBr2);
Button btn = new Button();
btn.Text = “Test”;
Controls.Add(btn);
}
}
}
Modifying the AssemblyInfo.cs
Add the following using statements.
using System.Security;
Next add the following line of code.
[assembly: AllowPartiallyTrustedCallers()]
Singing the Assembely
Right click on the class library project and select Properties from the context menu.
Select the Signing tab on the left.
Check the Sign the Assembely option.
In the `Choose a strong key file:’ drop down, select <New.>.
Enter key.snk for the key name and uncheck the `protect my key file with a password’ checkbox.
Compile the assembly.
Deploy It
Deploy the assembly to the SharePoint site’s bin folder. This is typicall located at {Local_Drive}:InetpubwwwrootwssVirtualDirectories/{Port or site}/bin.
Editing the SharePoint Site’s Web.config
Open the SharePoint site’s web.config for editing.
Registering your WebPart as a SafeControl
Use relector to get the assembly’s information and add the following between the <SafeControls></SafeControls> tags substituing your assembly’s informaiton.
<SafeControl Assembly=”{Assembly Name}, Version={Assembly Version}, Culture=neutral, PublicKeyToken={Key}” Namespace=”{WebPart’s namespace}” TypeName=”*” Safe=”True” AllowRemoteDesigner=”True” />
Increasing the trust level
Modify the trust level to WSS_Medium. It should look like the following.
<trust level=”WSS_Medium” originUrl=”" />
Adding that WebPart to SharePoint
Registering your WebPart with SharePoint
From the top level site (not from any of the child sites).
1. Select “Site Actions”/”Site Settings”
2. Click the Web parts Link in the middle of the page
3. Click the “New” link at the top of the list.
4. Find your web part in this list, check the box in front of it and click “Populate Gallery”
Your web part should now be in the gallery list.
Add the WebPart to the screen
1. Navigate to the page you want the part on.
2. Select “Site Actions” / “Edit Page”
3. Click “Add Web Part”
4. Find your web part in the displayed list and click the check box
5. Click “Add”
6. Your web part should now be displayed on the page.
7. Click “Exit Edit Mode” to save the change
Your web part should now be displayed.