Creating a MOSS 2007 webpart

  1. Start VS 200[5][8] and create a new class library
  2. Sign your class library and determine the publickey token (use reflector.NET for example)
  3. 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);
            }
        }
    }
    
  4. Add the following line of code to your AssemblyInfo.cs:
    [assembly: AllowPartiallyTrustedCallers]
    
  5. 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" />
    
  6. Copy the class library dll to the bin directory of your webapplication
  7. Create a site collection if not already available
  8. Go to Site Actions –> All settings –> Web parts. Choose New and add your web part to the site gallery.
  9. Edit the page and add your webpart.
Share

Leave a Reply

Your email address will not be published. Required fields are marked *