WebPart Class

Provides the base class for creating Microsoft SharePoint Foundation Web Parts.


Namespace:  Microsoft.SharePoint.WebPartPages
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No

<SupportsAttributeMarkupAttribute(False)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public MustInherit Class WebPart _
	Inherits WebPart _
	Implements INamingContainer, IAttributeAccessor, IConnectionData
Dim instance As WebPart

The SharePoint Foundation Web Part infrastructure is designed and built to lie on top of the Microsoft ASP.NET Web Part infrastructure. Web Parts that inherit from WebPart are fully supported in Microsoft SharePoint Foundation (with the exception of user controls as generic Web Parts), and can be used not only in ASP.NET applications but also in SharePoint Foundation applications, whether SharePoint Foundation is involved or not. The SharePoint FoundationWebPart 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 SharePoint FoundationWebPart class exists primarily for the purpose of backward compatibility 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 SharePoint Foundation.

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.

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
            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 = ""

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

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

            [text] = value
         End Set
      End Property      

      Protected Overrides Sub RenderWebPart(output As HtmlTextWriter)
         ' Securely write out HTML
         output.Write(("<BR>Text Property: " + SPEncode.HtmlEncode([Text])))
      End Sub
   End Class
End Namespace

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.