Walkthrough: Extending a Web Part
This walkthrough demonstrates how to create, build, and deploy a Web Part by using the existing Web Part base class.
Note
This sample code illustrates a concept, and shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.
This walkthrough contains the following topics:
Prerequisites
Configuring the SharePoint Site
Visual Studio Solution Structure
Creating a New Web Part
Building, Packaging, and Deploying the Solution
Prerequisites
You can access the sample code in the samples folder you selected when you installed the SharePoint Commerce Services Extensibility Kit. For more information about the extensibility kits packaged with Commerce Server 2009 R2, see Getting Started With the Extensibility Kits. To use the samples provided in these kits, you must have the following software installed in your development environment:
Microsoft Visual Studio 2010
Microsoft Internet Information Services 7 (IIS)
Commerce Server 2009 R2
You must also deploy the MicrosoftCommerceWebParts.wsp file to have access to the Web Parts and required files. You can deploy the file by using the SharePoint Commerce Services Wizard that is included with Commerce Server 2009 R2. This wizard performs a series of tasks required to deploy the site, such as creating a Web application and a site collection in SharePoint, unpacking a Commerce Server site, deploying the SharePoint MicrosoftCommerceWebParts.wsp solution file in SharePoint, and so on.
For detailed instructions about installing Commerce Server 2009 R2 components, see Installation and Initial Configuration.
For instructions about how to deploy the SharePoint 2010 Solution Storefront, see the Commerce Server Solution Storefront.
Configuring the SharePoint 2010 Site
Your SharePoint 2010 site must be configured to use Commerce Server related features and resources.
Note
If you used the SharePoint Commerce Services Configuration Wizard to deploy the Web parts, Commerce Sever related features and resources are already activated in SharePoint 2010. If this is the case, go to Visual Studio Solution Structure.
Use the following procedures to activate the Commerce Server Feature and Commerce Server WebPart Resources Feature in SharePoint 2010 for the Web application, and to activate the Commerce Server Channel Configuration Feature for the Web site.
Note
You must have Administrator privileges to configure the site.
To activate the features for the Web application
Open SharePoint Central Administration, and then click Application Management.
On the Application Management page, click Manage Web Applications.
On the Manage Web Application page, select the desired Web application, and then click Manage Features.
On the Manage Web Application Features page, click Activate for the Commerce Server Feature.
On the Manage Web Application Features page, click Activate for the Commerce Server WebPart Resources Feature.
To activate the feature for the Web site
On your Web site, click Site Actions.
Click Site Settings.
On the Site Settings page, under the Site Actions section, click Manage Site Features.
On the Site Features page, click Activate for the Commerce Server Channel Configuration feature.
Note
Features will already be activated if you used Portal Config to deploy the Web Parts.
Visual Studio Solution Structure
The SampleWebPart.sln solution is composed of the following project:
Project |
Description |
SampleWebPart.csproj |
The extensibility solution that contains the sample code that shows how to extend a Web Part. |
Creating a New Web Part
This procedure provides the steps you perform to create a Web Part by using the BaseWebPart class. To create a Web part, you create a Visual Web Part project; modify the automatically generated source code so that you can use the BaseWebPart class and the Web Part references; and then add the code for your Web Part.
To create a Web Part project in Visual Studio 2010
In Visual Studio 2010, from the toolbar, click File, click New, and then click Project.
In the New Project dialog box, select SharePoint then 2010, then VisualWebPart, and then type the name of the Web Part and the location where you want to create it.
Click OK.
In the SharePoint Customization Wizard, enter the name of the site, and then select the appropriate trust level.
Click OK.
To add Web Part references to the project
In the Solution tree, right-click References and then click AddReferences.
Using the file browser, browse to the Assemblies folder in the Microsoft Commerce Server 2009 directory.
Select the following three files:
Microsoft.Commerce.Portal.Common.dll
Microsoft.Commerce.Portal.SharePointCommon.dll
Foundation assemblies\Microsoft.Commerce.dll
Click Ok.
To modify the code
In the Web Part source file, for example, <mywebpart.cs>, add the following "using" clauses:
using Microsoft.Commerce;
using Microsoft.Commerce.Portal.Common;
Change the Web Part class to derive from BaseWebPart by replacing system.Web.UI.WebControls.WebParts.WebPart with BaseWebPart.
Implement the body of the Web Part, perhaps by calling the controller classes.
Sample code:
{ using System; using System.Runtime.InteropServices; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Serialization; using Microsoft.Commerce; using Microsoft.Commerce.Portal.Common; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint.WebPartPages; /// <summary> /// The SampleWebPart is a web part that renders the display name of a product. /// It extends Microsoft.Commerce.Portal.Common.BaseWebPart. /// </summary> [Guid("7110c612-b9f3-42ce-8d6f-0f30048d5485")] [CLSCompliant(false)] public class SampleWebPart : Microsoft.Commerce.Portal.Common.BaseWebPart { /// <summary> /// A collection holding the product properties to query or display. /// </summary> private CommercePropertyCollection properties; /// <summary> /// Initializes a new instance of the SampleWebPart class. /// </summary> public SampleWebPart() { this.properties = new CommercePropertyCollection(); this.properties.Add(CommerceCatalogEntityPropertyNames.Id); this.properties.Add(CommerceCatalogEntityPropertyNames.DisplayName); } /// <summary> /// Display the product name. /// </summary> /// <param name="writer">The writer of the output stream.</param> protected override void RenderContents(HtmlTextWriter writer) { try { base.RenderContents(writer); CommerceProduct product = CatalogController.GetProduct( "AW149 - 15", Settings.Default.ProductCode, this.properties, null, false, false, false, false); if (product != null) { writer.Write(product.DisplayName); } } catch (Exception ex) { writer.Write(ex.Message); } } /// <summary> /// Render the contents of this web part. It calls the RenderContents method. /// </summary> /// <param name="writer">The writer of the output stream.</param> protected override void Render(HtmlTextWriter writer) { this.RenderContents(writer); } } }
After you modify the code, you may have to change the debug information of the project to point to the target Web site. If this is the case, perform the following procedure:
To change the debug information
In the Solution tree, right-click the project name, click Properties and then click Debug.
In the debug section, click Start browser with URL, and then type the URL of your Web site in the text box.
Save the solution by clicking the Save icon on the Visual Studio toolbar, or from the Visual Studio menubar, click File and then click Save.
Building, Packaging and Deploying the Solution
You must build, package, and deploy the solution to make the new Web Part available for use in SharePoint 2010.
To build, package and deploy the solution from within Visual Studio
If you are using Visual Studio 2010, open the solution file you created in Creating a New Web Part.
If you are using Visual Studio 2008, you can use the **SampleWebPart.sln **packaged with the SharePoint Commerce Services Extensibility Kit. This Web part can be built as usual but needs to be manually packaged and deployed.
In Solution Explorer, right-click the Web Part project name, click Properties, and then click Debug.
Select Start Browser with URL then type the URL of your Web site in the text box.
To save the solution, click the Save icon on the Visual Studio toolbar, or from the Visual Studio menu bar, click File and then click Save.
In Solution Explorer, right-click the Web Part project name, and then click Build.
In Solution Explorer, right-click the Web Part project name, and then click Package.
In Solution Explorer, right-click the Web Part project name, and then click Deploy.
You can verify that the Web Part works by adding it to your Web site.
To add the Web Part to your site
On your Web site's Home page, click Site Actions and then click Edit Page.
Click Add a Web Part.
Your new Web Part should appear in the Web Part Gallery.
Select your new Web Part, and then click Add.
The Web Part will render on the Home page.