How to Generate Coupons and Promotion Codes
This topic shows how to generate a batch of randomly generated coupons that are attached to a private promotion code.
To create coupons and promotion codes
In Visual Studio, create a new Commerce Server Core Systems Web application.
Add the Microsoft.CommerceServer.CrossTierTypes and Microsoft.CommerceServer.Marketing.CrossTierTypes references to the application.
Add a using directive for the Microsoft.CommerceServer and Microsoft.CommerceServer.Marketing namespaces.
Define the Marketing Web Service URL.
Create the MarketingContext object.
For more information, see How to Connect to the Marketing System. In this example, the MarketingContext object is named marketingSystem.
Create a new PromoCodeDefinition object.
Generate a batch of Random Promotion Codes attached to the promotion code created in step 6.
Example
The following code example illustrates how to create a new private promotion code object. Ten new random promotion codes are created in a batch and attached to the newly created promotion code.
You can view the results in the Marketing Manager application.
using Microsoft.CommerceServer;
using Microsoft.CommerceServer.Marketing;
namespace CommerceServerMarketingExamples
{
public class CSMarketingExamples
{
public void GenRandomPromoCodes()
{
try
{
// Set the URL to the Web service.
string marketingWebServiceUrl = @"https://localhost/MarketingWebService/MarketingWebService.asmx";
// Create an instance of the Marketing System agent.
MarketingServiceAgent ma = new MarketingServiceAgent(marketingWebServiceUrl);
MarketingContext marketingSystem = MarketingContext.Create(ma);
// Create a new promotion code for the batch.
// Batch promo codes much be private.
PromoCodeDefinition promo = marketingSystem.PromoCodeDefinitions.NewPromoCodeDefinition(PromoCodeUsageOption.Private, 1);
promo.Name = "BatchPromoCodes";
promo.UsageLimit = 5;
promo.Save(true);
// Create a batch of 10 random promotion code strings.
Guid g;
RandomGenerationParameters r;
r = new RandomGenerationParameters();
r.CodeLength = 5;
r.Prefix = "pre";
r.Suffix = "suf";
g = Guid.Empty;
g = marketingSystem.PromoCodeDefinitions.GenerateRandomCodes(promo.Id, "BatchPromoCodes", 10, r);
}
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();
}
}
}
}