Thursday, September 3, 2009

How to Write Custom Webparts in Sharepoint 2007

In this blogpost, we will see how to write the simplest possible Sharepoint 2007 WebPart and deploy it. Just for fun, we will inherit from the sharepoint webpart instead of the asp.net webpart.
Begin by creating a class library project, add references to System.Web, Microsoft.Sharepoint.dll (again only because this blogpost talks about Sharepoint Webpart).
Then add a class as below, which as you can tell is a hella simple WebPart.


using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Web;
using System.Web.Security;
using System.Security;
using System.Security.Permissions;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.WebPartPages;

namespace Sahil.Sharepoint.WebPartPages
{
[System.Xml.Serialization.XmlRoot(Namespace =
"http://blah.winsmarts.com/demospace")] public class CustomWebPart : WebPart
{
private string toDisplayText = "Default Text";
[WebPartStorage(Storage.Shared), System.Xml.Serialization.XmlElement(Namespace =
"http://blah.winsmarts.com/demospace")] public string ToDisplayText { get { return toDisplayText; } set { toDisplayText = value; } }
protected override void RenderWebPart(System.Web.UI.HtmlTextWriter writer)
{
writer.Write(toDisplayText) ;
base.Render(writer);
}
}
}
Great !! Now, strongly name it, Build the dll, and use sn-t or reflector to find the public key token.
Then follow the following steps -
1. Locate the Sharepoint WebSite you wish to deploy this webpart in. This can be done via the IIS Manager under administrative tools. The default ones would be virtual directories that look like GUIDs under C:\INETPUB\WWROOT\WSS\<>. In my case (yours may be different) the dir was - C:\Inetpub\wwwroot\wss\VirtualDirectories\7ee252c4-9e98-4258-a6ae-77e16a287bea\bin
2. Drop the dll in the bin directory in the virtual dir. Important: By dropping the webpart in the bin directory, you are effectively giving it partial trust. If you were writing a plain vanilla ASP.NET 2.0 WebPart, you would need to decorate the Webpart with the AllowPartialTrustedCallersAttribute @ the assembly level. You could also create a new trust policy for your WebPart (recommended), or raise the trust level in the web.config file (default is WSS_Minimal), or you could just drop the Webpart in the GAC; which again requires strong naming.
3. Under the same virtual directory, find the web.config, and add the following to the SafeControls section -

4. Then browse to the WebSite, and go to Site Actions -> Site Settings -> Modify All Site Settings. Under that, click on "Web Parts" under Galleries.
5. Click on "New" in the toolbar, and find the Sahil.Sharepoint.WebPartPages.CustomWebPart as shown below -
6. Check the checkbox, go to the top, click on "Populate Gallery".
That's it. Your WebPart is now ready to eat !! :)
How to use it?
Go to the site of your choice, Edit Page, Click on Add a WebPart, you should see it in the list that pops up.

No comments:

Post a Comment