共用方式為


How to Create a Campaign Directly with the Web Service

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

Creating a campaign using the Marketing Web service directly requires distinctly different code than performing the same task in Agent mode or Local mode. This code sample requires that you add a Web reference to the Marketing Web service, for example https://localhost/MarketingWebService/MarketingWebService.asmx.

To create a campaign directly with the Web Service

  1. Create an instance of the Marketing System Web reference and pass the credentials to the Web service.

  2. Create an IndustryCode object. This object represents an industry, such as aviation or food.

  3. Create a Customer object to use for the campaign.

  4. Use the Industry property of the Customer object to assign the IndustryCode to the Customer.

  5. Create a Campaign object with the Id property of the Customer object.

  6. Set the properties of the Campaign object.

Description

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.

Code

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.
    
    // Make sure 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.
                IndustryCodeManager im = marketingSystem.IndustryCodes;
                IndustryCode ic = im.NewIndustryCode();
                ic.Name = "Paper Products";
                ic.Save();
                
                // 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

Understanding the Different Types of Commerce Server APIs