다음을 통해 공유


How to Import and Export Promotion Codes

This example code shows how to import and export promotion codes (coupons).

To import and export promotion codes

  1. In Visual Studio, create a new Commerce Server Core Systems Web application.

  2. Add the Microsoft.CommerceServer.CrossTierTypes and Microsoft.CommerceServer.Marketing.CrossTierTypes references to the application.

  3. Add a using directive for the Microsoft.CommerceServer and Microsoft.CommerceServer.Marketing namespaces.

  4. Define the Marketing Web Service URL.

  5. Create the MarketingContext object.

    For more information, see How to Connect to the Marketing System. In the following example, the MarketingContext object is named marketingSystem.

  6. Look up the ID of the "BatchPromoCodes" definition.

  7. Export the promotion codes to a file named c:\ExportCodes.txt.

  8. Create a new and empty PromoCodeDefinition object.

  9. Import a group of promotion codes from a file into the PromoCodeDefinition.

    For more information about how to create a promotion code file, see How to Create a Coupon File to Import Coupon Data.

Example

The following code example illustrates how to import and export promotion codes. This example builds on the promotion code definition named "BatchPromoCodes" created in How to Generate Coupons and Promotion Codes.

To review the results in the Marketing Manager

  1. Look at the Coupons tab in the properties of a discount item.

  2. Select the "Imported Codes" coupon definition from the drop-down box.

  3. View the import record within the "Batches" grid, including the number of coupons imported.

using Microsoft.CommerceServer;
using Microsoft.CommerceServer.Marketing;

namespace CommerceServerMarketingExamples
{
    public class CSMarketingExamples
    {
        public void ImportExportPromoCodes()
        {
            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);

                // Look up the ID of an existing promotion code definition.
                SearchClauseFactory searchClauseFactory = marketingSystem.PromoCodeDefinitions.GetSearchClauseFactory();
                SearchClause sc = searchClauseFactory.CreateClause(ExplicitComparisonOperator.Equal, "Name", "BatchPromoCodes");
                DataSet dataSet = marketingSystem.PromoCodeDefinitions.Search(sc);
                int promoId = (int)dataSet.Tables["SearchResults"].Rows[0][0];

                // Export the codes.
                PromoCodeDefinitionManager.ProgressEventHandler promoEvents = null;
                marketingSystem.PromoCodeDefinitions.ExportPromoCodesForDefinition("c:\\ExportCodes.txt", promoId, promoEvents);

                // Create a new promotion code definition for import.
                PromoCodeDefinition ipromo = marketingSystem.PromoCodeDefinitions.NewPromoCodeDefinition(PromoCodeUsageOption.Private, 1);
                ipromo.Name = "ImportPromoCodes";
                ipromo.UsageLimit = 5;
                ipromo.Save(true);

                // Import promotion codes into the promotion code definition.
                marketingSystem.PromoCodeDefinitions.ImportCodes(ipromo.Id, "Imported Codes03", "c:\\ImportCodes03.CSV");
            }
            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

Marketing Management Scenarios