다음을 통해 공유


How to Run the ContentSelector Object

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

The following example shows how you typically create ContentSelector objects, how you use them to select the content through the GetContent method, and how to write that content to the Web page.

To use the ContentSelector object

  1. Use the CommerceContextobject to create a ContentSelector object.

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

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

Example

In this code sample, set the debug variable to true to see a list of pipeline trace messages in your browser each time the sample is run.

You can set other, optional properties on the ContentSelector object. These properties depend on the components in the selection pipeline and their configuration. The most common optional properties include UserProfile, NumRequested, PageGroup, and Size.

using Microsoft.CommerceServer.Runtime;
using Microsoft.CommerceServer.Runtime.Targeting;
using Microsoft.CommerceServer.Runtime.Profiles;
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;

/// <summary>
/// This page is used to get ads and display them.  
/// Query string parameter 'num' controls the number of ads returned, default is 1.
/// Query string parameter 'email' will attempt to load the UserObject profile with that email if it exists
/// Set debug = true to print logging information
/// </summary>
public partial class GetAds_aspx : System.Web.UI.Page
{
    // enable debug printing on the web page
    bool debug = false;

    const string UserObjectProfile = "UserObject";
    const string CampaignHistoryCookieName = "CampaignHistory";
    const string EmailAddressKeyName = "email_address";

    protected void Page_Load(object sender, EventArgs e)
    {
        // determine number of ads to get
        string numAdsStr = Request["num"];
        int numAds = 1;
        if (!string.IsNullOrEmpty(numAdsStr))
        {
            numAds = int.Parse(numAdsStr);
        }

        // load profile if needed
        Profile profile = null;
        string email = Request["email"];
        if (!string.IsNullOrEmpty(email))
        {
            profile = CommerceContext.Current.ProfileSystem.GetProfile(EmailAddressKeyName, email, UserObjectProfile);
        }

        // get the selector
        ContentSelector contentSelector = CommerceContext.Current.TargetingSystem.SelectionContexts["advertising"].GetSelector();

        // configure the selector
        contentSelector.ItemsRequested = numAds;
        if (null != profile) contentSelector.Profiles.Add(UserObjectProfile, profile);
        if (debug) contentSelector.TraceMode = true;

        // get the content
        StringCollection ads = contentSelector.GetContent();

        switch (ads.Count)
        {
            case 0:
                // log message so we don't get a blank page
                Response.Write("No ads returned");
                break;
            case 1:
                // log basic ad (used for automated validation)
                Response.Write(ads[0]);
                break;
            default:
                // log all (used for manual cases)
                int i = 0;
                foreach (string ad in ads)
                {
                    Response.Write(i + Environment.NewLine + ad);
                    Response.Write(Environment.NewLine + Environment.NewLine + "<hr>" + Environment.NewLine + Environment.NewLine);
                    i++;
                }
                break;
        }

        // log debug info
        if (debug)
        {
            Response.Write(Environment.NewLine + Environment.NewLine + "<hr>");
            Response.Write(Environment.NewLine + "<h2>ContentSelector Values</h2>");
            Response.Write(Environment.NewLine + "ItemsRequested - '" + contentSelector.ItemsRequested + "'<br/>");
            Response.Write(Environment.NewLine + "Name - '" + contentSelector.Name + "'<br/>");
            Response.Write(Environment.NewLine + "PageHistory - '" + contentSelector.PageHistory + "'<br/>");
            Response.Write(Environment.NewLine + "Size - '" + contentSelector.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 contentSelector.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 contentSelector.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 contentSelector.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

Marketing Run-time Scenarios