September 2, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

WebControls Versus WebParts in SharePoint

  • January 11, 2008
  • By Gustavo Velez
  • Send Email »
  • More Articles »

The concept of WebParts as a reusable, adaptable device for creating flexible products is one of the most important contributions of SharePoint to contemporary, web-oriented software development. WebParts were introduced in SharePoint 2001 and have been maintained through subsequent platforms. The current SharePoint version (2007) preserved the WebParts NameSpaces legacy from the 2003 version and allowed for the new NameSpaces from DotNet 3.0.

Technically speaking, WebParts are an inheritance of WebControls with extra, enhanced features to ensure their usability in web applications. One augment is the WebParts' user control panel that allows for configurating and adapting the function and layout of the WebPart in an undemanding way. The upshot is that WebControls and WebParts operate in the same manner inside SharePoint and can run equally well in its context. The primary difference between them—apart from the control panel—is that WebParts must reside in a Web Zone 'container' in the Web page whereas WebControls can be localized wherever you want them. It is precisely this flexibility that makes WebControls a favored way to implement menus in the SharePoint interface.

There are a number of places in a SharePoint Portal where custom WebControls could be useful. Each page, for instance, might have a company logo and basic information. This can be realized by using the master page infrastructure; however, if the information isn't the same for every customer (the client's name is included in the title, for example), it is necessary to use a WebPart or a WebControl. Because the logo and relevant information are not placed in a WebZone within the page, using a WebPart is not convenient; in this case, a WebControl is the ideal solution (note that this is an example: in the real world of SharePoint, the logo and title are implemented as WebControls).

In the realm of Enterprise Content Management (ECM) and Web Content Management (WCM) projects, there are additional characteristics of WebControls waiting to be exploited. Because WebControls cannot be accessed or configured by users once they have been installed, they are not security-sensitive. Imagine a hypothetical company where management has made a request that falls outside SharePoint's default possibilities; in this instance, it involves a web content page whereby a particular user group should be advised of a disclaimer clause. Creating the text can be achieved by using a WebControl precisely in the targeted page informing the current user as to his/her user group status and displaying the disclaimer clause when appropriate.

Creating a WebControl

WebControls are created as a DotNet 2.0 or 3.0 managed Web Control Library in Visual Studio; it is possible to use Visual Studio 2005 or 2008 for the task, but the example code will be created using the 2008 version. The first step is to open a new project of the type "ASP.NET Server Control" in Visual Studio and, standard procedure, configure the name of the project and the class and add the references and "using" statements for SharePoint. In the example, the WebControl project will be named "Disclaimer" and the working class "DisclaimerWebCustomControl":



Click here for a larger image.

Figure 1: Visual Studio with the generated WebControl project

To configure the users group, the WebControl will have a custom property ("DisplayForGroup"). the disclaimer text will also be configured and saved in the property ("DisclaimerText"). The Visual Studio template generates a default "Text" property that can be used for the first property of the sample:

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string DisplayForGroup
{
   get
   {
      String s = (String)ViewState["DisplayForGroup"];
      return ((s == null) ? String.Empty : s);
   }

   set
   {
      ViewState["DisplayForGroup"] = value;
   }
}

And the second property takes a similar shape:

[Bindable(true)]
[Category("DisclaimerText")]
[DefaultValue("Disclaimer: This is a Disclaimer")]
[Localizable(true)]
public string DisclaimerText
{
   get
   {
      String s = (String)ViewState["DisclaimerText"];
      return ((s == null) ? String.Empty : s);
   }

   set
   {
      ViewState["DisclaimerText"] = value;
   }
}




Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel