Teilen über


How to Use the ContentSelector Object to Display Targeted Content

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

You use the ContentSelector object to call Content Selection Framework (CSF) pipelines to display dynamic, targeted marketing content on a Web page. The ContentSelector object is a property bag that inherits the properties and methods of the Dictionary Object and includes the GetContent method. You use the Dictionary object passed to the GetContent method, the global Context dictionary for the CSF application, to pass global objects and other values to the pipeline. These values include the location of the redirector script (Redir.aspx), the expression evaluator object, and the CSF pipeline to run to select the content. These values do not change on a per-request basis.

Certain pipeline components in the CSF pipeline expect certain values, some that are optional, be set in the dictionary. For example, the CSF pipeline expects the Size, PageGroups, NumRequested, and UserProfile keys to be set in the dictionary.

To use the ContentSelector object to display targeted content

  1. Use the Current property of the CommerceContext object to get the current ContentSelector object.

  2. Use the GetContent method on the ContentSelector object to get the content.

  3. Iterate through the returned content to retrieve the requested information.

Example

This code example calls the CSF pipeline, and displays an ad, if applicable. If no ad is available, the code displays a "no ads returned" message. Set the debug variable to true to display pipeline trace information.

using Microsoft.CommerceServer.Runtime;
using Microsoft.CommerceServer.Runtime.Targeting;
using System;
using System.Data;
using System.Collections.Specialized;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CSOTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        const string CampaignHistoryCookieName = "CampaignHistory";
        bool debug = true;
        ContentSelector cso = CommerceContext.Current.TargetingSystem.SelectionContexts["advertising"].GetSelector();
        cso.ItemsRequested = 1;
        if (debug) cso.TraceMode = true;
        StringCollection ads = cso.GetContent();
        if (0 == ads.Count)
        {
            Response.Write("No ads returned");
        }
        else
        {
            Response.Write(ads[0]);
        }
        if (debug)
        {
            Response.Write(Environment.NewLine + Environment.NewLine + "<hr>");
            Response.Write(Environment.NewLine + "<h2>cso Values</h2>");
            Response.Write(Environment.NewLine + "ItemsRequested - '" + cso.ItemsRequested + "'<br/>");
            Response.Write(Environment.NewLine + "Name - '" + cso.Name + "'<br/>");
            Response.Write(Environment.NewLine + "PageHistory - '" + cso.PageHistory + "'<br/>");
            Response.Write(Environment.NewLine + "Size - '" + cso.Size + "'<br/>");

            Response.Write(Environment.NewLine + Environment.NewLine + "<h2>TraceMessages</h2>");
            Response.Write(Environment.NewLine + "<table border=1>");
            int i = 0;
            foreach (StringCollection strcol in cso.TraceMessages)
            {
                Response.Write(Environment.NewLine + Environment.NewLine + "<tr><td>Item" + i + "</td><td>");
                foreach (string s in strcol)
                {
                    Response.Write(Environment.NewLine + s + "<br/>");
                }
                Response.Write(Environment.NewLine + "</td></tr>");
                i++;
            }
            Response.Write(Environment.NewLine + "</table>");

            Response.Write(Environment.NewLine + Environment.NewLine + "<h2>" + CampaignHistoryCookieName + " Cookie</h2>");
            HttpCookie requestCookie = Request.Cookies[CampaignHistoryCookieName];
            Response.Write("Request: " + requestCookie.Value + "<br/>");
            HttpCookie responseCookie = Response.Cookies[CampaignHistoryCookieName];
            Response.Write("Response: " + responseCookie.Value + "<br/>");

            Response.Write(Environment.NewLine + Environment.NewLine + "<h2>AllContentItems</h2>");
            Response.Write(Environment.NewLine + "<table><tr>");

            i = 0;
            foreach (ListDictionary contentItem in cso.AllContentItems)
            {
                Response.Write(Environment.NewLine + Environment.NewLine + "<td><table border=1><tr><td colspan=2>Item" + i + "</td></tr>");
                foreach (string key in contentItem.Keys)
                {
                    Response.Write(Environment.NewLine + "<tr><td>" + key + "</td><td>" + contentItem[key] + "</td></tr>");
                }
                Response.Write(Environment.NewLine + "</table></td>");
                i++;
            }
            Response.Write(Environment.NewLine + "</tr></table>");

            Response.Write(Environment.NewLine + Environment.NewLine + "<h2>SelectedContentItems</h2>");
            Response.Write(Environment.NewLine + "<table><tr>");
            foreach (ListDictionary contentItem in cso.SelectedContentItems)
            {
                Response.Write(Environment.NewLine + "<td><table border=1>");
                foreach (string key in contentItem.Keys)
                {
                    Response.Write(Environment.NewLine + "<tr><td>" + key + "</td><td>" + contentItem[key] + "</td></tr>");
                }
                Response.Write(Environment.NewLine + "</table></td>");
            }
            Response.Write(Environment.NewLine + "</tr></table>");
        }
    }
}

See Also

Other Resources

Content Selection Framework