Share via


How to Attach a Display Template to an Advertisement

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

This topic shows how to create a new advertisement that uses a display template and display template properties.

To Create an Advertisement

  1. In Visual Studio, create a new Commerce Server 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 this example, the MarketingContext object is named marketingSystem.

  6. Look up an existing Campaign ID.

  7. Look up the "All" PageGroup ID and add it to the Advertisement object.

  8. Retrieve a display template.

    This example uses the "Test Template" DisplayTemplate created in How to Create a Display Template. Custom templates are optional. You can use one of the standard templates supplied with Commerce Server.

  9. Create a new Advertisement object and set its properties.

    This example uses the custom template named "Test Template" created in How to Create a Display Template for the TemplateName property.

  10. Attach the AltText and ImageSource properties that were created with the "Test Template" to DisplayPropertyValue objects.

  11. Save the advertisement object.

Example

The following code example illustrates how you can view the results in the Commerce Server Marketing Manager application.

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

namespace CommerceServerMarketingExamples
{
    public class CSMarketingExamples
    {
        public void NewAdvertisementWithTemplate()
        {
            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 Default Campaign ID.
                SearchClauseFactory CsearchClauseFactory = marketingSystem.Campaigns.GetSearchClauseFactory();
                SearchClause Csc = CsearchClauseFactory.CreateClause(ExplicitComparisonOperator.Equal, "Name", "Default Campaign");
                DataSet CdataSet = marketingSystem.Campaigns.Search(Csc);
                int CampaignId = (int)CdataSet.Tables["SearchResults"].Rows[0][0];

                // Look up the "All" PageGroup and add it to the 
                // Advertisement PageGroups property.
                PageGroup[] pgAll = marketingSystem.PageGroups.GetAllPageGroups();
                PageGroup pg = null;
                foreach (PageGroup pg1 in pgAll)
                    if (pg1.Tag == "All")
                    {
                        pg = pg1;
                        break;
                    }
                // Retrieve the template.
                DisplayTemplate dt = marketingSystem.DisplayTemplates.GetDisplayTemplate("Test Template7");

                // Create the new advertisement.
                Advertisement ad = marketingSystem.CampaignItems.NewAdvertisement(CampaignId);
                ad.Name = "Test Advertisement";
                ad.Description = "Description for Test Advertisement";
                ad.StartDate = DateTime.Now;
                ad.EndDate = DateTime.Now + TimeSpan.FromDays(14);
                ad.ExposureLimit = 5;
                ad.SizeName = "Full Banner";
                ad.HourOfDayToStart = 8;
                ad.HourOfDayToEnd = 18;
                ad.HouseAdWeight = 50;
                ad.Type = AdvertisementType.Paid;
                ad.TemplateName = "Test template";
                ad.DateReceived = DateTime.Now;
                ad.DaysOfWeekToRun = RecurrenceDays.Weekdays;
                ad.Industry = "Financial";
                ad.NumberOfEventsScheduled = 500;
                // Add the "All" PageGroup retrieved earlier.
                ad.PageGroups.Add(pg);   

                // Add the AltText property.
                DisplayPropertyValue dp1 = new DisplayPropertyValue(dt.Properties["AltText"]);
                ad.PropertyValues.Add(dp1);

                // Add the ImageSource property.
                DisplayPropertyValue dp2 = new DisplayPropertyValue(dt.Properties["ImageSource"]);
                ad.PropertyValues.Add(dp2);

                //Save the ad
                ad.Save(false);
            }
            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

Display Templates