0

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?

Stephen Hosking
  • 1,405
  • 16
  • 34
  • 1
    Unrelated: version 1.3 is available: http://www.microsoft.com/downloads/details.aspx?familyid=FB9D4B85-DA2A-432E-91FB-D505199C49F6&displaylang=en – Rubens Farias Feb 05 '10 at 00:02
  • Thanks. I had some trouble installing 1.2, so once it was working, I didn't want to take the risk of trying 1.3. I've had a look through the change notes for 1.3, and they don't seem to relate to this issue. As 1.3 came out in 2009, and my book is 2007, it is unlikely that either my VSeWSS, or the book's, is 1.3. – Stephen Hosking Feb 05 '10 at 01:00
  • I'm getting the feeling that his (the author's) code is for VSeWSS 1.0 or 1.1, and that is what he is referring to as "new style", and my code, for 1.2, is a "new new style", and supercedes it. Still trying to find a "getting started" guide for this version. – Stephen Hosking Feb 05 '10 at 01:06
  • I never found the answer to this question. I got stuck on deployment, and, to fix that problem, I started using WSPBuilder. It comes with it's own default C# project, which produces a "Hello World" Web Part. It is close to the first program in the question. – Stephen Hosking Feb 25 '10 at 23:52

1 Answers1

0

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 Rendermethod would not overriden in most web parts.

Stephen Hosking
  • 1,405
  • 16
  • 34