Click to Rate and Give Feedback
WebPart Class (Microsoft.SharePoint.WebPartPages)
Provides the base class for creating Windows SharePoint Services Web Parts.

Namespace: Microsoft.SharePoint.WebPartPages
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)

Visual Basic (Declaration)
<ParseChildrenAttribute(False)> _
<SupportsAttributeMarkupAttribute(False)> _
<XmlRootAttribute(Namespace:="http://schemas.microsoft.com/WebPart/v2")> _
<XmlTypeAttribute("Microsoft.SharePoint.WebPartPages.WebPart", Namespace:="http://schemas.microsoft.com/WebPart/v2")> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level:=AspNetHostingPermissionLevel.Minimal)> _
Public MustInherit Class WebPart
    Inherits WebPart
    Implements INamingContainer, IAttributeAccessor, IConnectionData
Visual Basic (Usage)
Dim instance As WebPart
C#
[ParseChildrenAttribute(false)] 
[SupportsAttributeMarkupAttribute(false)] 
[XmlRootAttribute(Namespace="http://schemas.microsoft.com/WebPart/v2")] 
[XmlTypeAttribute("Microsoft.SharePoint.WebPartPages.WebPart", Namespace="http://schemas.microsoft.com/WebPart/v2")] 
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)] 
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)] 
public abstract class WebPart : WebPart, INamingContainer, IAttributeAccessor, IConnectionData

Beginning with Windows SharePoint Services 3.0, the SharePoint Web Part infrastructure is designed and built to lie on top of the Microsoft ASP.NET Web Part infrastructure. Web Parts that inherit from System.Web.UI.WebControls.WebParts.WebPart are fully supported in Windows SharePoint Services (with the exception of user controls as generic Web Parts), and can be used not only in ASP.NET applications but also in Windows SharePoint Services applications, whether Windows SharePoint Services is involved or not. The Windows SharePoint Services WebPart class is part of an infrastructure that was designed specifically for SharePoint sites, and Web Parts that inherit from this class can be used only in SharePoint sites.

When creating new Web Parts, you have the option of creating Web Parts that inherit from System.Web.UI.WebControls.WebParts.WebPart (recommended) or Microsoft.SharePoint.WebPartPages.WebPart. The Windows SharePoint Services WebPart class exists primarily for the purpose of backward compatibility (Web Parts written for Windows SharePoint Services 2.0 continue to work in Windows SharePoint Services 3.0 without modification), and to provide a small set of features that are not available in the System.Web.UI.WebControls.WebParts.WebPart class.

The set of features provided exclusively by Microsoft.SharePoint.WebPartPages.WebPart is as follows:

  • Cross page connections

  • Connections between Web Parts that are outside of a zone

  • Client-side connections (Web Part Page Services Component)

  • Data caching infrastructure, including the ability to cache to the database

For more information, see Web Parts in Windows SharePoint Services.

The following code example shows a simple Web Part that contains HtmlButton and HtmlInputText controls that allow the user to change the Web Part's Title property. For a step-by-step walkthrough of creating this Web Part, see Walkthrough: Creating a Basic SharePoint Web Part.

Visual Basic
Imports System
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml.Serialization
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebPartPages
Imports Microsoft.SharePoint.Utilities
Imports System.Web.UI.HtmlControls

Namespace WebPartLibrary1
   ' This Web Part changes it's own title and implements a custom property.
   <XmlRoot([Namespace] := "WebPartLibrary1")>  _
   Public Class SimpleWebPart
      Inherits WebPart
      Private Const defaultText As String = "hello"
      Private [text] As String = defaultText

      ' Declare variables for HtmlControls user interface elements.
      Private _mybutton As HtmlButton
      Private _mytextbox As HtmlInputText      

      ' Event handler for _mybutton control that sets the
      ' Title property to the value in _mytextbox control.
      Public Sub _mybutton_click(sender As Object, e As EventArgs)
         Me.Title = _mytextbox.Value
         Try
            Me.SaveProperties = True
         Catch Else
            Caption = "Error... Could not save property."
         End Try
      End Sub      

      ' Override the ASP.NET Web.UI.Controls.CreateChildControls 
      ' method to create the objects for the Web Part's controls.      
      Protected Overrides Sub CreateChildControls()
         ' Create _mytextbox control.
         _mytextbox = New HtmlInputText()
         _mytextbox.Value = ""
         Controls.Add(_mytextbox)

         ' Create _mybutton control and wire its event handler.
         _mybutton = New HtmlButton()
         _mybutton.InnerText = "Set Web Part Title"
         AddHandler _mybutton.ServerClick, AddressOf _mybutton_click
         Controls.Add(_mybutton)
      End Sub      

      <Browsable(True), Category("Miscellaneous"), DefaultValue(defaultText), WebPartStorage(Storage.Personal), FriendlyName("Text"), Description("Text Property")>  _
      Public Property [Text]() As String
         Get
            Return [text]
         End Get

         Set
            [text] = value
         End Set
      End Property      

      Protected Overrides Sub RenderWebPart(output As HtmlTextWriter)
         RenderChildren(output)
         ' Securely write out HTML
         output.Write(("<BR>Text Property: " + SPEncode.HtmlEncode([Text])))
      End Sub
   End Class
End Namespace
C#
//--------------------------------------------------------------------
// File: SimpleWebPart.cs
//
// Purpose: A sample Web Part that demonstrates how to create a basic
// Web Part.
//--------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;
using System.Web.UI.HtmlControls;

namespace WebPartLibrary1
{
    /// <summary>
    /// This Web Part changes its title and implements a custom property.
    /// </summary>
    [XmlRoot(Namespace="WebPartLibrary1")]    
    public class SimpleWebPart : WebPart
    {
        private const string defaultText = "hello";
        private string text=defaultText;

        // Declare variables for HtmlControls user interface elements.
        HtmlButton _mybutton;
        HtmlInputText _mytextbox;

        // Event handler for _mybutton control that sets the
        // Title property to the value in _mytextbox control.
        public void _mybutton_click (object sender, EventArgs e)
        {
            this.Title = _mytextbox.Value;
            try
            {
                this.SaveProperties=true;
            }
            catch
            {
                Caption = "Error... Could not save property.";
            }
        }

        // Override the ASP.NET Web.UI.Controls.CreateChildControls 
        // method to create the objects for the Web Part's controls.      
        protected override void CreateChildControls ()
        {         
            // Create _mytextbox control.
            _mytextbox = new HtmlInputText();
            _mytextbox.Value="";
            Controls.Add(_mytextbox);

            // Create _mybutton control and wire its event handler.
            _mybutton = new HtmlButton();
            _mybutton.InnerText = "Set Web Part Title";
            _mybutton.ServerClick += new EventHandler (_mybutton_click);
            Controls.Add (_mybutton);
        }

        [Browsable(true),Category("Miscellaneous"),
        DefaultValue(defaultText),
        WebPartStorage(Storage.Personal),
        FriendlyName("Text"),Description("Text Property")]
        public string Text
        {
            get
            {
                return text;
            }

            set
            {
                text = value;
            }
        }

        protected override void RenderWebPart(HtmlTextWriter output)
        {
            RenderChildren(output);
            // Securely write out HTML
            output.Write("<BR>Text Property: " + SPEncode.HtmlEncode(Text));    
        }
    }
}
System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
       System.Web.UI.WebControls.Panel
         System.Web.UI.WebControls.WebParts.Part
           System.Web.UI.WebControls.WebParts.WebPart
            Microsoft.SharePoint.WebPartPages.WebPart
               Derived Classes
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Non SharePoint WebPart Example      SeniorSeniorSoftwareDeveloper ... bobchauvin   |   Edit   |  

Could you make an example that is using the recommended WebPart and not the SharePoint WebPart?

Note to SeniorSeniorSoftwareDeveloper ... Thomas Lee,

WebPart is a member of Microsoft.SharePoint.WebPartPages, and not something meaningful outside the context of SharePoint. So, in the example,

Public Class SimpleWebPart  Inherits WebPart 

Inheriting from WebPart is the recommend approach.

-bac

Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker