In SharePoint Portal Server 2003, which called its reusable widgets WebParts. Thus, it makes perfect sense that ASP.NET 2.0 provides fantastic support for WebParts, and it definitely is no surprise to see Microsoft Office SharePoint Server 2007 (MOSS 2007) built upon this very framework. This article discusses WebPart framework basics, which can serve as the foundation for more advanced topics such as WebPart support in MOSS 2007.
The Major Components of the ASP.NET 2.0 WebPart Framework
Before diving into a sample WebPart-driven Web site, you must first understand the major reusable controls built into the .NET framework, which you will use to set up the site:
Below code shows how get input from web part editor of a SharePoint page. Here i am taking the drop down list in the web part editor page and a label to display the selected value in the drop down list. We will get the all the lists of the site to the drop down list by using the code, and the selected list value to the label in the web part.
In the above code text is the property to get the input from web part editor. CreateChildControls() method to initialize the controls in the web part.ApplyChanges() method is used for get the input.SyncChanges() method is keep the selected value in the webpart editor.
deploy add activate the feature in the share-point site. That's it. You are done with it.
here is the final image
The Major Components of the ASP.NET 2.0 WebPart Framework
Before diving into a sample WebPart-driven Web site, you must first understand the major reusable controls built into the .NET framework, which you will use to set up the site:
- WebPart: A WebPart is a reusable widget on a Web page. The user can choose to add a WebPart on his page, customize the WebPart per his needs, or even define communication between various WebParts. An ASP.NET 2.0 WebPart inherits from the System.Web.UI.WebControls.WebParts.WebPart class. A good example is a widget that displays traffic. End-user customization could involve specifying a freeway, and communication with another WebPart could involve a different WebPart that has a list of freeways that the user can click, one by one, to view updated traffic information. Setting up something like that is not very difficult in ASP.NET 2.0.
- WebPartManager: This control is the central policeman of the ASP.NET 2.0 WebPart framework. Each page should contain only a single WebPartManager, which is responsible for managing all functionality, events, and customization of various WebParts on that page. WebPartManager also has the ability to set various modes. So, for instance, if the user sets the WebPartManager in a Catalog mode, he could pick and choose the WebParts he wants on his page from a catalog of WebParts. Alternatively, he could put the page in Communication mode and define the various connections between different WebParts.
- Various Zones: Zones are physical areas on a page. These are implemented in the following Server Controls that ship with the Framework:
- WebPartZone: A WebPartZone is a control that defines an area on a page where one or more WebParts can be hosted. A WebPartZone also controls the look and feel of a WebPart inside itself. Also, any control that doesn't inherit from the WebPart class can masquerade itself as a WebPart and live inside a WebPartZone. This is done with the help of the GenericWebPart class, which inherits from the WebPart base class. By doing so, you are restricted to a subset of the functionality that a WebPart class can expose.
- CatalogZone: The CatalogZone is the menu or the catalog from which a user can choose. It holds a number of CatalogPart controls, which in turn hold WebParts that are already added to the site and ready for the picking to add to various pages on the site. The user can pick WebParts from the Catalog, and add them to the various WebPartZones on the same page. There are three types of CatalogParts: DeclarativeCatalogPart, PageCatalogPart, and ImportCatalogPart.
- EditorZone: This is the area on the page that prompts the user to edit his WebPart and customize it to his specific needs. A WebPart can also be customized in a Shared mode, where an administrator can configure the WebPart and all other users can view or use the WebPart but not customize it.
- ConnectionsZone: This is the area of the page that prompts the end user to define communication between various WebParts on the same page. For instance, you could build an online RSS reader. One WebPart holds the user's OPML, and the other WebPart renders the RSS for a particular subscription. The connection between the two would be the OPML WebPart providing a row (the RSS URL) and the RSS reader WebPart consuming the row, and then rendering appropriately. Because this is a simple ASP.NET 2.0 Web site, you can wrap these inside an atlas:UpdatePanel or a third-party control such as the telerik AJAX Panel. You even can eliminate postbacks and replace them with AJAX callbacks with almost no code at all.
Below code shows how get input from web part editor of a SharePoint page. Here i am taking the drop down list in the web part editor page and a label to display the selected value in the drop down list. We will get the all the lists of the site to the drop down list by using the code, and the selected list value to the label in the web part.
using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Web.UI; using System.Web.UI.WebControls.WebParts; using System.Web.UI.WebControls; using Microsoft.SharePoint; using System.Collections; namespace WebPartEditPart { [Guid("0f0072e7-6735-45b5-b8b6-1ed9c54e39e9")] public class WebPartEditPart :WebPart { private string text = null; private Label lblmessage; //[Personalizable(PersonalizationScope.Shared)] //[WebBrowsable(true)] //[System.ComponentModel.Category("My Property Group")] //[WebDisplayName("MyProperty")] //[WebDescription("Meaningless Property")] public string Text { get { return text;} set {text = value; } } protected override void CreateChildControls() { base.CreateChildControls(); lblmessage = new Label(); lblmessage.Text = this.Text; Controls.Add(lblmessage); } public override EditorPartCollection CreateEditorParts() { TextDisplayEditorPart textDisplayWebpart = new TextDisplayEditorPart(); textDisplayWebpart.ID = this.ID + "editorPart"; ArrayList editorArray = new ArrayList(); editorArray.Add(textDisplayWebpart); return new EditorPartCollection(base.CreateEditorParts(), editorArray); } private class TextDisplayEditorPart : EditorPart { private DropDownList ddlList; protected override void CreateChildControls() { base.CreateChildControls(); SPWeb currentWeb = SPContext.Current.Web; SPListCollection listCollection = currentWeb.Lists; if (listCollection.Count != 0) { ddlList = new DropDownList(); ddlList.DataSource = listCollection; ddlList.DataTextField = "Title"; ddlList.DataValueField = "Title"; ddlList.DataBind(); Controls.Add(ddlList); } } protected override void RenderContents(HtmlTextWriter writer) { base.RenderContents(writer); //ddlList.RenderControl(writer); } public override bool ApplyChanges() { EnsureChildControls(); if (!string.IsNullOrEmpty(ddlList.SelectedItem.Text)) { WebPartEditPart webpart =(WebPartEditPart)WebPartToEdit; webpart.text = ddlList.SelectedItem.Text; return true; } return false; } public override void SyncChanges() { EnsureChildControls(); WebPartEditPart webpart =(WebPartEditPart)WebPartToEdit; if (string.IsNullOrEmpty(ddlList.SelectedItem.Text)) { ddlList.SelectedItem.Text = webpart.text; } } // Access the drop-down control through a property. } } }
In the above code text is the property to get the input from web part editor. CreateChildControls() method to initialize the controls in the web part.ApplyChanges() method is used for get the input.SyncChanges() method is keep the selected value in the webpart editor.
deploy add activate the feature in the share-point site. That's it. You are done with it.
here is the final image