Thursday, November 15, 2007

Creating a Custom Web Part for SharePoint 2007

To create a Custom WebPart you can follow following steps

* In this part you will create a WebPart which displays Current User.
* Create a class libraray using MS Visual Studio 2005
* Then following codes are added which use to show current user.
*

public class SimpleWebPart : WebPart
{
private string displayText = "Hello World!";

[WebBrowsable(true), Personalizable(true)]
public string DisplayText
{
get { return displayText; }
set { displayText = value; }
}

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
writer.Write(displayText);
}
}

* Then you have to modify
AssemblyInfo.cs file by adding following coding

[assembly: AllowPartiallyTrustedCallers()]

*
Give your assembly a strong name. Just go into the Project
Properties and then go to the "Signing" tab to do this.
Alternatively, you can use the sn.exe command line tool to generate strong names
for you.


*Find the public key token for your assembly. An easy way to this is
to use a standard tool such as
Reflector. Once the DLL is compiled and built, drag and drop it
in Reflector. It should show you the public key token, along with the details
* Locate the SharePoint Web application in which you want to deploy this WebPart. You can do this via the IIS Manager under administrative tools.

*
Drop the DLL in the _app_bin directory in the virtual directory. By doing this, you effectively allow ASP.NET to access the DLL. You still need to take a few more steps for SharePoint to use that assembly.

*Then
under the same virtual directory, find Web.Config and add the following to the SafeControls section.
Assembly="MYWebPart,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=sfcvdsxbf7305ec3"
Namespace="MyWebPart"
TypeName="MySimpleWebPart" Safe="True" />

These values may differ from your values such as PublicKeyToken, Namespace,...

*
With Web.Config modified, you have to add the WebPart to the gallery. Through
your browser, go to the Web application whose virtual directory you have been
using. Once on that site, go to Site Actions -> Site Settings -> Modify
All Site Settings

*
Under that, click on "Web Parts" under Galleries

* Click on "New" in the toolbar, and find the MyWebPart.MySimpleWebPart

* Check the checkbox, go to the top, and click on "Populate Gallery". You should
see the MySimpleWebPart.webpart file ready to use in your site

* Then you can add this WebPart wherever you want.


Important Notes:

* Modify the web.config file using "Microsoft Visual Studio 2005 Tools for
Application", do not use WordPad.
* If You are working on a machine where SharePoint is not installed,
then you have to copy some .dll file from the machine where SharePoint
is installed.
Path for those .dll files is
"
Local_Drive\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI"
* We have to add some additional Namespaces for our application.
* For the class file (Class1.cs) we have to add "using System.Web.UI.WebControls.WebParts;"
* For the Assemly.cs file we have to add "using System.Data" and "using System.Security;" namespaces

No comments:

Post a Comment