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

  1. Open SharePoint Central Administration, and then click Application Management.

  2. On the Application Management page, click Manage Web Applications.

  3. On the Manage Web Application page, select the desired Web application, and then click Manage Features.

  4. On the Manage Web Application Features page, click Activate for the Commerce Server Feature.

  5. On the Manage Web Application Features page, click Activate for the Commerce Server WebPart Resources Feature.

To activate the feature for the Web site

  1. On your Web site, click Site Actions.

  2. Click Site Settings.

  3. On the Site Settings page, under the Site Actions section, click Manage Site Features.

  4. 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

  1. In Visual Studio 2010, from the toolbar, click File, click New, and then click Project.

  2. 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.

  3. Click OK.

  4. In the SharePoint Customization Wizard, enter the name of the site, and then select the appropriate trust level.

  5. Click OK.

To add Web Part references to the project

  1. In the Solution tree, right-click References and then click AddReferences.

  2. Using the file browser, browse to the Assemblies folder in the Microsoft Commerce Server 2009 directory.

  3. Select the following three files:

    • Microsoft.Commerce.Portal.Common.dll

    • Microsoft.Commerce.Portal.SharePointCommon.dll

    • Foundation assemblies\Microsoft.Commerce.dll

  4. Click Ok.

To modify the code

  1. In the Web Part source file, for example, <mywebpart.cs>, add the following "using" clauses:

    • using Microsoft.Commerce;

    • using Microsoft.Commerce.Portal.Common;

  2. Change the Web Part class to derive from BaseWebPart by replacing system.Web.UI.WebControls.WebParts.WebPart with BaseWebPart.

  3. Implement the body of the Web Part, perhaps by calling the controller classes.

  4. 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

  1. In the Solution tree, right-click the project name, click Properties and then click Debug.

  2. In the debug section, click Start browser with URL, and then type the URL of your Web site in the text box.

  3. 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

  1. 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.

  2. In Solution Explorer, right-click the Web Part project name, click Properties, and then click Debug.

  3. Select Start Browser with URL then type the URL of your Web site in the text box.

  4. 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.

  5. In Solution Explorer, right-click the Web Part project name, and then click Build.

  6. In Solution Explorer, right-click the Web Part project name, and then click Package.

  7. 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

  1. On your Web site's Home page, click Site Actions and then click Edit Page.

  2. Click Add a Web Part.

    Your new Web Part should appear in the Web Part Gallery.

  3. Select your new Web Part, and then click Add.

  4. The Web Part will render on the Home page.

See Also

Other Resources

Developing with SharePoint Commerce Services