Web Parts Overview
Published: May 2010
Web Parts are server-side controls that run inside the context of site pages in Microsoft SharePoint Foundation. They are editable and configurable by users. Web Parts give users the ability to add functionality to a site page by simply putting them on the page. SharePoint Foundation includes many default Web Parts. In addition, you can build your own Web Parts.
There are two different types of Web Parts in SharePoint Foundation. Both are supported, but ASP.NET Web Parts are the recommended type for your projects.
ASP.NET Web Parts — These Web Parts are built on top of the ASP.NET Web Part infrastructure. The ASP.NET-style Web Parts have a dependency on System.Web.dll and must inherit from the WebPart base class in the System.Web.UI.WebControls.WebParts namespace. These Web Parts can be used in ASP.NET applications as well as in SharePoint Foundation, making them highly reusable.
If you are creating your Web Part specifically for a SharePoint site, and it will consume the SharePoint Foundation object model, you can derive from the ASP.NET System.Web.UI.WebControls.WebParts.WebPart base class and add a reference to the SharePoint object model in your project.
SharePoint-based Web Parts — These Web Parts have a dependency on Microsoft.SharePoint.dll and must inherit from the WebPart base class in the Microsoft.SharePoint.WebPartPages namespace. These Web Parts can only be used in SharePoint Web sites.
For more information about when to derive from the SharePoint Foundation WebPart Class, see Web Part Infrastructure in SharePoint Foundation topic.
The SharePoint Foundation Web Part infrastructure is built on top of a control named SPWebPartManager that is derived from the ASP.NET WebPartManager control. The SPWebPartManager control overrides the standard behavior of the WebPartManager control to persist Web Part data inside the SharePoint Foundation content database instead of in the ASP.NET services database. In most cases, you do not have to worry about dealing directly with the SPWebPartManager control because the one required instance is already defined in v4.master. When you create a site page that links to v4.master, the SPWebPartManager control is already there. If you create your own custom ASPX pages that do not reference v4.master and you want to support adding Web Parts to Web Part zones, an SPWebPartManager control needs to be added to the page.
Web Part zones for a Web Parts page in SharePoint Foundation should be created by using the WebPartZone control defined inside Microsoft.SharePoint.WebPartPages namespace, not by using the standard WebPartZone control from ASP.NET.
When you create a Web Parts page for a standard ASP.NET application, you need to add logic that interacts with the WebPartManager control to manage the Web Part display mode, and generally you also need to explicitly add editor parts and catalog parts to the page along with the HTML layout to accommodate them. Fortunately, you do not have to perform these changes when creating site pages for a SharePoint Foundation site. Instead, you inherit from the WebPartPage class that is defined inside the Microsoft.SharePoint.WebPartPages namespace and it does all the work behind the scenes for you.
Custom Web Parts provide developers with a method to create user interface elements that support both customization and personalization. The term customization implies that changes are seen by all site members. Individual users can further personalize Web Parts page by adding, reconfiguring, and removing Web Parts. The term personalization implies that these changes will be seen only by the user that made them. A site owner or a site member with the appropriate permissions can customize Web Parts page by using a browser or by using Microsoft SharePoint Designer to add, reconfigure, or remove a Web Part.
Because the SharePoint Foundation Web Part infrastructure is now built on top of the ASP.NET Web Parts control set, you can reuse your knowledge of ASP.NET programming to create quick and robust custom Web Parts. Developers can extend SharePoint sites by creating custom Web Parts, to add the extra dimensions of user customization and personalization. Developing custom Web Parts provides an easy and powerful way to extend SharePoint Foundation sites.
Following are some ways in which you can benefit from and use custom Web Parts. You can:
Create custom properties that you can display and modify in the user interface.
Improve performance and scalability. A compiled custom Web Part runs faster than a script.
Implement proprietary code without disclosing the source code.
Secure and control access to content within the Web Part. Built-in Web Parts allow any users with appropriate permissions to change content and alter Web Part functionality. With a custom Web Part, you can determine the content or properties to display to users, regardless of their permissions.
Make your Web Part connectable, allowing Web Parts to provide or access data from other connectable Web Parts.
Interact with object models that are exposed in SharePoint Foundation. For example, you can create a custom Web Part to save documents to a SharePoint Foundation document library.
Control the Web Part cache by using built-in cache tools. For example, you can use these tools to specify when to read, write, or invalidate the Web Part cache.
Benefit from a rich development environment with debugging features that are provided by tools such as Microsoft Visual Studio 2010.
Create a base class for other Web Parts to extend. For example, to create a collection of Web Parts with similar features and functionality, create a custom base class from which multiple Web Parts can inherit. This reduces the overall cost of developing and testing subsequent Web Parts.
Control Web Part implementation. For example, you can write a custom server-side Web Part that connects to a back-end database, or you can create a Web Part that is compatible with a broader range of Web browsers.