- Start VS 200[5][8] and create a new class library
- Sign your class library and determine the publickey token (use reflector.NET for example)
- Create a new class in your library with the following code:
using System; using System.Collections.Generic; using System.Text; using System.Web.UI.WebControls.WebParts; namespace SimpleWebpart { 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); } } }
- Add the following line of code to your AssemblyInfo.cs:
[assembly: AllowPartiallyTrustedCallers]
- Edit your web.config to add a safecontrol entry (remember to use the public key token as determined in step 2):
<SafeControl Assembly="SimpleWebpart,Version=1.0.0.0, Culture=neutral,PublicKeyToken=XXXXXXXXXXXXXXX" Namespace="SimpleWebpart" TypeName="*" Safe="True" />
- Copy the class library dll to the bin directory of your webapplication
- Create a site collection if not already available
- Go to Site Actions –> All settings –> Web parts. Choose New and add your web part to the site gallery.
- Edit the page and add your webpart.
Creating a MOSS 2007 webpart
Leave a reply