How to Create a Display Template
For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.
This topic describes how to create a new display template.
To create a display template
In Visual Studio, create a new Commerce Server 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, as described in How to Connect to the Marketing System. In this case, the MarketingContext object is named marketingSystem.
Create a new DisplayTemplate object.
Add DisplayProperty objects to the display template for each display label.
Save the template.
Example
This code example creates a display template. The template is used in the How to Attach a Display Template to an Advertisement code sample, within the TemplateName field.
You can view the results in Commerce Server Marketing Manager.
using Microsoft.CommerceServer;
using Microsoft.CommerceServer.Marketing;
namespace CommerceServerMarketingExamples
{
public class CSMarketingExamples
{
public void NewDisplayTemplate()
{
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 ad display template.
DisplayTemplate template = marketingSystem.DisplayTemplates.NewDisplayTemplate();
template.Name = "Test template7";
template.Text = "<img alt=\"{%AltText%}\" src=\"{%ImageSource%}\">";
//Create the "AltText" property for the new template.
DisplayProperty at = new DisplayProperty("AltText");
at.Label = "AltText";
at.Source = TemplatePropertySource.Auto;
//Create the "ImageSource" property for the new template.
DisplayProperty im = new DisplayProperty("ImageSource");
im.Label = "ImageSource";
im.Source = TemplatePropertySource.Auto;
//Add the "AltText" and "ImageSource" properties to the template.
template.Properties.Add(at);
template.Properties.Add(im);
//Save the template.
template.Save();
}
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();
}
}
}
}