Thursday, 8 July 2010

Developing on SharePoint 2010 (Day 1)

This article is talking about developing web parts for SharePoint 2010 using Visual Studio 2010. If you are looking for articles for older version of SharePoint or Visual Studio I recommend this URL: http://www.aspfree.com/c/a/Windows-Scripting/Beginning-SharePoint-Web-Part-Development/

First of all, there are two types of web parts VS 2010 supports: Standard Web Part (which in VS 2010 Item Template list named Web Part) and Visual Web Part.

A standard web part is just a class inherited from abstract class WebPart. Below is the Inheritance Hierarchy of WebPart class.

   1:  WebControl
   2:  |---Panel
   3:      |---Part
   4:          |---WebPart

To add contents into standard web part we need override the CreateChildControls method and inside put code logic to create sub controls manually and add them into the Controls collection. See the code below:

   1:  protected override void CreateChildControls()
   2:  {
   3:      LiteralControl msg = new LiteralControl("test");
   4:      this.Controls.Add(msg);
   5:  }

The visual web part, in the other hand, give you a visual design UI to drag existed web control in. But if you check the source code, a visual web part is just a standard web part plus a Web.UI.UserControl. And in the overrided CreateChildControls method used a trick like this:

this.Controls.Add(Page.LoadControl(_controlPath));

One more thing for visual web part: the value _controlPath is the physical URL of the user control and the value of it is maintained by VS 2010.

No comments: