Local Mode Programming Sample

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

Local mode is the most direct implementation of the API. In this case the code runs directly in the process of the caller and makes direct database calls. The advantage to this approach is performance. The disadvantage to this approach is that the database must be visible directly and the running process identity must have adequate access to the database. A good application for this mode would be an administrative tool for massive data manipulation on the server.

The difference between local mode and agent mode is limited to how an instance of the context class for the corresponding Commerce Server system is created. After the context object is created, subsequent programming is the same, regardless of whether you are using local mode or agent mode.


Local mode can be used only on the machine where the server components of Commerce Server are installed, because it communicates directly with the Data Access layer instead of the Web service.

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.

The following code creates an instance of the MarketingContext class, 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.

This sample shows, in bold, the version of the Create factory method that takes five parameters total, with the final two being the SQL query and SQL long-running query timeout values, in seconds.

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

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

    class Program
        static void Main(string[] args)
                // 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 because they are required for campaigns.
                IndustryCodeManager im = marketingSystem.IndustryCodes;
                IndustryCode ic = im.NewIndustryCode();
                ic.Name = "Travel";

                // 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.

                // 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.

                // Success!
                Console.WriteLine("Successfully created campaign name: {0}", ca.Name);
            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);