Microsoft KB Archive/328883

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

Article ID: 328883

Article Last Modified on 8/2/2006

-

APPLIES TO


 * Microsoft Commerce Server 2002 Standard Edition

-



This article was previously published under Q328883



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



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
 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. 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.  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 {   ///     /// CatalogSvc Service exposes list of products and categories. ///    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
 In Visual Studio .NET, create a new project. Under Project Types, select Visual Basic Projects, and then click Windows Application under Templates. After you create the project, add a Web reference:  On the Project menu, click Add Web Reference.</li> 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.</li> Click Add Reference.</li> In Solution Explorer, expand Web References, and then note the namespace that was used.</li></ol> </li> 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 &quot;http://localhost/cscatalog/cscatalog.asmx&quot; /out:CatlogSvc.vb /l:VB

</li>  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 </li></ol>