Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

SHOWME HOW: To Create Commerce Catalog Web Services by Using Visual C# .NET


View products that this article applies to.

This article was previously published under Q328883

↑ Back to the top


Summary

This article describes how to create Commerce Server catalog XML Web services by using Microsoft Visual C# .NET, and then consume the services by using a Microsoft Visual Basic .NET Client.

Click the Play button to view this streaming media demonstration.

NOTE: To view this video, Windows Media Player 7.0 or later must be installed on your computer. For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
299321� Description and Availability of Windows Media Player 7.1

↑ Back to the top


More information

Prerequisites

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Commerce Server 2002 with the .NET Framework
  • Microsoft Visual Studio .NET

Create a Commerce Catalog Web Service

  1. In Visual Studio .NET, create a new project. Under Project Types, select Commerce Projects, and then click Commerce C# ASP.NET Web Service under Templates. Add a Web Service item to the project.
  2. After you create the project, import a sample catalog by using the Business Desk. A sample catalog is located in the Commerce Server 2002 installation folder in the SDK\Samples\ASPNET\Catalog Sitelet subfolder. To gain access to Business Desk from Visual Studio .NET, on the Project menu, click Commerce Server Project, and then click Business Desk. Note that when you create a project by using Visual Studio .NET, Visual Studio .NET configures the environment, and most of the configuration information is stored in the Web.config file that is located in the Web site application folder. Web.config contains a Commerce Server specific section, and the Commerce modules are loaded in the order that is specified.
  3. The following is Visual C# .NET code for catalog XML Web services. Paste the code in an XML Web service page that has an .asmx extension. View this page in a Web browser, and then make a note of the URL. This URL will be used as a Web reference in the client application, which will consume this service.
    using System;
    using System.Data;
    using System.Web;
    using System.Web.Services;
    using Microsoft.CommerceServer.Runtime;
    using Microsoft.CommerceServer.Runtime.Catalog;
    
    namespace CSCatalog
    {
    	/// <summary>
    	/// CatalogSvc Service exposes list of products and categories.
    	/// </summary>
    	public class CatalogSvc : System.Web.Services.WebService
    	{
    		// Get list of catalogs.
    		[WebMethod]
    		public DataSet ListCatalogs()
    		{
    			return CommerceContext.Current.CatalogSystem.GetCatalogs(string.Empty);
    		}
    
    		// Get root categories for a specific catalog.
    		[WebMethod]
    		public DataSet ListRootCategories(string strCatalog)
    		{
    			ProductCatalog oCatalog = CommerceContext.Current.CatalogSystem.GetCatalog(strCatalog);
    			return oCatalog.GetRootCategories();
    		}
    
    		// Get child categories for a specific root category and catalog.
    		[WebMethod]
    		public DataSet ListChildCategories(string strRootCategory, string strCatalog)
    		{
    			ProductCatalog oCatalog = CommerceContext.Current.CatalogSystem.GetCatalog(strCatalog);
    			Category oCategory = oCatalog.GetCategory(strRootCategory);
    
    			//oCategory is an existing Category object.
    			return oCategory.GetChildCategories();
    		}
    
    		// Get a list of products for a specific category and catalog.
    		[WebMethod]
    		public DataSet ListProducts(string strCategory, string strCatalog)
    		{
    			ProductCatalog oCatalog = CommerceContext.Current.CatalogSystem.GetCatalog(strCatalog);
    
    			return oCatalog.GetCategory(strCategory).GetProducts();
    		}
    	}
    }
    					

Use a Visual Basic .NET Client to Consume the Web Services

  1. In Visual Studio .NET, create a new project. Under Project Types, select Visual Basic Projects, and then click Windows Application under Templates.
  2. After you create the project, add a Web reference:
    1. On the Project menu, click Add Web Reference.
    2. In the Add Web Reference dialog box, type the URL for the Web service in the Address text box, and then press ENTER. If you set the local computer to host the Web service, the URL is http://localhost/CSCatalog/CSCatalog.asmx.
    3. Click Add Reference.
    4. In Solution Explorer, expand Web References, and then note the namespace that was used.
  3. Add the proxy class file that you just created to the client project. You can add a proxy class in the client application by using the Wsdl.exe utility. To do this, run the following at the Visual Studio .NET command prompt (the Visual Studio .NET command prompt is located in the Visual Studio .NET Tools program folder):
    WSDL "http://localhost/cscatalog/cscatalog.asmx" /out:CatlogSvc.vb /l:VB
  4. Use the following sample code to program against the XML Web service. This sample code creates a CataolgSvc object:
    ' Proxy to WebService
    Imports CSCatalogClient.localhost
    
    Public Class MyClass
        ' Create CatalogSvc object from proxy class.
        Private oCatalogSvc As CatalogSvc = New CatalogSvc()
    
        Private Sub MyProcedure()
            ' Retrieve Catalog names into a dataset.
            Dim ds As System.Data.DataSet = oCatalogSvc.ListCatalogs()
        End Sub
    
    End Class

References

For more information about catalog XML Web services, see the Commerce Server 2002 product documentation. Also, see the "Programming the Web with Web Services" topic in the Visual Studio .NET Help, or the "ASP.NET Web Services and ASP.NET Web Service Clients" topic in the Microsoft .NET Framework Developer's Guide.

↑ Back to the top


Keywords: KB328883, kbinfo, kbarttypeshowme, kbhowto

↑ Back to the top

Article Info
Article ID : 328883
Revision : 10
Created on : 8/2/2006
Published on : 8/2/2006
Exists online : False
Views : 414