Write Custom WebParts for Windows Sharepoint Services 3.0 and ASP.NET2.0
Windows Sharepoint Services (WSS) 3.0 is built on top of ASP.NET 2.0, All WSS 3.0 incoming request routes through the ASP.NET runtime before WSS. This changed forced to remove the ISAPI filter which was used in previous version of WSS2.0 and adding HTTPModules and HTTPHandlers that are registered with ASP.NET using standard web entries.
Following is a high-level overview of the obvious integration point between ASP.NET 2.0 and WSS3.0:
1. When you register an IIS web application to become WSS3.0 web app, WSS3.0 adds a wildcard application map to IIS metabase. This wildcard application map serves to route all incoming http request to ASP.NET runtime regardless of their extension e.g. .doc etc.
2. ASP.NET 2.0 introduced a new pluggable component type known as Virtual Path Provider.
WSS 3.0 has its own virtual path provider called SPVirtualPathProvider which is able to retrieve an .aspx page from SQL Server and hand it off to .aspx page parser supplied by ASP.NET 2.0.
3. ASP.NET 2.0 introduced a new powerful feature of page templates known as master pages. WSS3.0 sites have a special catalog known as a master page gallery containing a master page default.master.
4. WSS3.0 WebPartManager is a sealed class called SPWebPartManager it inherits from System.Web.UI.WebControls.WebParts.WebPartManager.
5. WSS3.0 WebPartZone is a public sealed class called Microsoft.SharePoint.WebPartPages.WebPartZone, which inherits from System.Web.UI.WebControls.WebParts.WebPartZone.
6. WSS3.0 WebPart inherits from an abstract base class called Microsoft.SharePoint.WebPartPages.WebPart, which inherits from System.Web.UI.WebParts.WebPart.
Note: WSS3.0 still supports web parts developed in WSS2.0 for backward compatibility.
Build, Deploy and Use Your WebPart in WSS3.0
For demonstration, put the following WebPart code in a class library:
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
public class MyWebPart : WebPart
{
private string myText = "Hello World";
[WebBrowsable(true), Personalizable(true)]
public string MyText
{
get { return myText; }
set { myText = value; }
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
writer.Write(myText);
}
}
Next, use the following code snippet to add the AllowPartiallyTrustedCallers attribute
(located under the System.Security namespace) to the code's AssemblyInfo.cs file:
[assembly: AllowPartiallyTrustedCallers()]
Next, give you assembly a strong name, just go into the project properties and then
go to the Signing tab or you can also use the sn.exe command line tool to generate strong names.
Your WebPart is now ready to use. The following steps will enable you to Deploy and Use your webpart:
1. Find the public key token for your assembly with a standard tool such as Reflector
by draging and droping it in Reflector. It should show you the public key token.
2. Locate the SharePoint Web application in which you want to deploy this WebPart.
You can do this via the IIS Manager under administrative tools. The default ones would be
virtual directories that look like GUIDs under C:\Inetpub\wwroot\wss\<<GUID>>.
3. Drop the DLL in the C:\Inetpub\wwroot\wss\<<GUID>>\_app_bin directory in the virtual directory.
By doing this, you effectively allow ASP.NET to access the DLL.
4. Under the same virtual directory, find Web.Config and add the following to the
SafeControls section:
<SafeControl Assembly="MyWebPart,Version=1.0.0.0,
Culture=neutral, PublicKeyToken=yourpublickeytoken"
Namespace="MyWebPart" TypeName="SimpleWebPart" Safe="True"/>
5. add the WebPart to the gallery. 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.MyWebPart.
6. Check the checkbox, and click on "Populate Gallery".
You should see the MyWebPart.MyWebpart file ready to use in your site.
7. Go to the site of your choice and click on Site Actions --> Edit Page.
Your page will prompt you to “add new WebParts dialog” which simply shows
you a list of possible WebParts you could add to your page.
Under Miscellaneous, you should see the MyWebPart.
Check the checkbox, and click "Add".
You now should see the WebPart you wrote running under SharePoint.
Note: You can also use this same webpart in your ASP.NET 2.0 web
application without any modification or change.