Delen via


Web Service Programming Sample

For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.

The third method to write Commerce Server code is to program directly to the Web services. The development experience using the Web services directly is not as rich as when using either the corresponding agent or local API. The Web service approach is missing the typical object model and offers no client-side validation.

To build and run this sample

  1. In Visual Studio 2005 or Visual Studio 2008, create a new C# console application.

  2. Paste the following code into your application and compile.

  3. Run the compiled console application. The call to the ReadLine method at the end of the sample will pause the application for you to observe the results.

Example

The following code sample first connects to the Marketing Web Service, creates an industry code using the industry code manager, creates a new customer, assigns the industry code to the customer, and then creates a new campaign associated with the customer ID.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace ConsoleApplication2
{
    // This code requires that you add a Web Reference to your Marketing Web Service,
    // for example: https://localhost/MarketingWebService/MarketingWebService.asmx.
    
    // Ensure that your user account has sufficient Marketing permissions to perform
    // these operations.

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Create an instance of the Marketing System Web Reference.
                localhost.MarketingService ms = new localhost.MarketingService();

                // You must pass proper credentials to the Web service.
                ms.Credentials = CredentialCache.DefaultNetworkCredentials;
                                
                // Create a new industry code because they are required for campaigns.
                localhost.IndustryCodeData ic = new localhost.IndustryCodeData();
                ic.Name = "Travel";
                
                // Create a new customer to use for the campaign.
                localhost.CustomerData cu = new localhost.CustomerData();
                cu = ms.NewCustomer();

                // Set the minimum required properties.
                cu.Name = "Example Customer";
                cu.Industry = ic.Name;

                // Save the customer.
                ms.SaveCustomer(ref cu, true);
                
                localhost.CampaignData ca = new localhost.CampaignData();
                ca=ms.NewCampaign(1);

                // Set the minimum required campaign properties.
                ca.Name = "Example Campaign";
                DateTime startdate = DateTime.Now;
                DateTime enddate = startdate.AddDays(30);
                ca.StartDate = startdate;
                ca.EndDate = enddate;
                ca.EventTypeName = "Click";

                // Save the campaign.
                ms.SaveCampaign(ref ca, true);

                // Success!
                Console.WriteLine("Successfully created campaign name: {0}", ca.Name);
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}\r\nMessage: {1}", ex.GetType(), ex.Message);
                if (ex.InnerException != null)
                {
                    Console.WriteLine("\r\nInner Exception: {0}\r\nMessage: {1}", ex.InnerException.GetType(), ex.InnerException.Message);
                }
                Console.ReadLine();
            }
        }
    }
}

See Also

Other Resources

Programming Directly to the Web Services

Agent Mode Programming Sample

Local Mode Programming Sample

Understanding the Different Types of Commerce Server APIs