views:

85

answers:

1

I'm using Visual Studio 2008, and have downloaded VSeWSS.exe 1.2, to enable Web Part development. I am new to SP development, and am already bewildered by the number of different versions of SP, and the VS add-ons. This particular problem has come up, which highlights my confusion.

I selected Add -> New Project -> Visual C# -> SharePoint-> Web Part, accepted the defaults, and VS created a project, with the main file WebPart1.cs

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace WebPart1
{
    [Guid("9bd7e74c-280b-44d4-baa1-9271679657a0")]
    public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
    {
        public WebPart1()
        {
        }

        protected override void CreateChildControls() // <-- This line
        {
            base.CreateChildControls();

            // TODO: add custom rendering code here.
            // Label label = new Label();
            // label.Text = "Hello World";
            // this.Controls.Add(label);
        }
    }
}

The book I'm following, Essential SharePoint 2007, by Jeff Webb, has the following for the default project -

using System;
<...as previously>

namespace WebPart1
{
    [Guid("9bd7e74c-280b-44d4-baa1-9271679657a0")]
    public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
    {
        // ^ this is a new style (ASP.NET) web part! [author's comment]
        protected override void Render(HtmlTextWriter writer) // <-- This line
        {
            // This method draws the web part   
            // TODO: add custom rendering code here.
            // writer.Write("Output HTML");
        }
    }
}

The real reason for my concern is that in this chapter of the book the author frequently mentions the distinction between "old style" web parts, and "new style" web parts, as noted in his comment on the Render method.

What's happened? Why has my default Web Part got a different signature to the authors?

A: 

The distinction the author is making with "new style" web parts is that they are the ASP.NET 2.0 web parts (released in 2005), which can be used in both SharePoint and ASP.NET. The old style web parts were specific to SharePoint

  • New style System.Web.UI.WebControls.WebParts.WebPart, available in ASP.Net 2.0 (2005) and WSS 3.0 (2006)
  • Old style Microsoft.SharePoint.WebPartPages.WebPart (still supported)

In the code samples in the question the two web parts are both new style, ie. they are ASP.NET Web Parts. The only difference is that visual studio has overidden a different method than the book has. However, both methods, and many others, eg. OnLoad, OnInit are available, and will be customised to get the web part to work.

My recommendation, after several months of web part development, is to use the first one as the basis for a "hello world" web part, ie.

      protected override void CreateChildControls() 
    {
        base.CreateChildControls();
        Label label = new Label();
        label.Text = "Hello World";
        this.Controls.Add(label);
    }

And then start adding code to this method, or add other methods (eg. OnLoad, OnPrerender) to add functionality.

The *Render*method would not overriden in most web parts.

Javaman59