How to Create a Campaign in Local Mode

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

You can create a campaign by using the Marketing API directly on the local server. This method differs from using the agent because you must use a different constructor for the MarketingContext and write additional code to pass your security information. The remaining code is the same.

Note

You can only run this code physically on the computer that is running Commerce Server. You cannot run this code remotely.

To create a campaign in local mode

  1. Create a MarketingContext object by using one of the Create methods. You use this object to interact with the Marketing System APIs.

  2. Create a 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.

Example

The following code creates an instance of MarketingContext, 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 Microsoft.CommerceServer.Marketing;
using System.Security.Principal;
using System.Threading;

namespace ConsoleApplication1
{
    // This code requires you to add the following references:
    // Microsoft.CommerceServer.CrossTierTypes
    // Microsoft.CommerceServer.Marketing.CrossTierTypes

    // Make sure that your user account has sufficient Marketing permissions to perform
    // these operations.

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Get credentials needed by MarketingContext.Create().
                IPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
                Thread.CurrentPrincipal = principal;
                string currentUser = principal.Identity.Name;
                                
                //Create a new MarketingContext using credentials.
                MarketingContext marketingSystem = MarketingContext.Create("CSharpSite", @"c:\inetpub\wwwroot\MarketingWebService\MarketingAuthorizationStore.xml", Microsoft.CommerceServer.AuthorizationMode.ThreadContext, 10, 20);

                // 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.
                Customer cu = marketingSystem.Customers.NewCustomer();

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

                // Save the customer.
                cu.Save(true);

                // Create the campaign.
                Campaign ca = marketingSystem.Campaigns.NewCampaign(cu.Id);

                // 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.
                ca.Save(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