Export (0) Print
Expand All

WebPart class

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

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
              

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

[SupportsAttributeMarkupAttribute(false)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public abstract class WebPart : WebPart, 
	INamingContainer, IAttributeAccessor, IConnectionData

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 Windows SharePoint Services and Web Parts.

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.

//--------------------------------------------------------------------
// 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));    
        }
    }
}

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

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
              Microsoft.SharePoint.WebControls.ApplicationAssociationsViewWebPart
              Microsoft.SharePoint.WebControls.TopologyViewWebPart
              Microsoft.SharePoint.WebPartPages.AccessRequestsHideOldRequestsLink
              Microsoft.SharePoint.WebPartPages.AccessRequestsHideOldRequestsOnLoad
              Microsoft.SharePoint.WebPartPages.AggregationWebPart
              Microsoft.SharePoint.WebPartPages.BaseXsltDataWebPart
              Microsoft.SharePoint.WebPartPages.BlogAdminWebPart
              Microsoft.SharePoint.WebPartPages.BlogLinksWebPart
              Microsoft.SharePoint.WebPartPages.BlogMonthQuickLaunch
              Microsoft.SharePoint.WebPartPages.ChartViewWebPart
              Microsoft.SharePoint.WebPartPages.ContentEditorWebPart
              Microsoft.SharePoint.WebPartPages.ImageWebPart
              Microsoft.SharePoint.WebPartPages.ListFormWebPart
              Microsoft.SharePoint.WebPartPages.ListViewWebPart
              Microsoft.SharePoint.WebPartPages.MembersWebPart
              Microsoft.SharePoint.WebPartPages.PageViewerWebPart
              Microsoft.SharePoint.WebPartPages.SimpleFormWebPart
              Microsoft.SharePoint.WebPartPages.SPTimelineWebPart
              Microsoft.SharePoint.WebPartPages.TilesViewWebPart
              Microsoft.SharePoint.WebPartPages.TitleBarWebPart
              Microsoft.SharePoint.WebPartPages.WikiContentWebpart
              Microsoft.SharePoint.WebPartPages.XmlWebPart
Show:
© 2014 Microsoft