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
{
/// <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
- 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.
- 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.
- Click Add Reference.
- In Solution Explorer, expand Web References, and then note the namespace that was used.
- 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
- 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.