Step 6: Implementing Content Selector Code

Using the Commerce Server 2002 Base Class Library (BCL), you can easily and rapidly implement marketing campaigns on your Web site. In this step, you will implement the code required to enable Ads and Discounts campaign items on your Web site. You will add the <ContentSelection> tag and enable pipeline and caching components in the Web.config file. You will also add references to the CacheCompLib.dllPrimary Interop Assembly (PIA), which is required to enable the ContentList Dictionary on the redir.aspx page.

In this step you will do the following:

  1. Add references to the CacheCompLib.dll (PIA).
  2. Edit the Web.config file.
  3. Add discount and ad labels to the default page.
  4. Add code to the default page.
  5. Create a page to record the click events and redirect users to advertisement URL.

To add references to the CacheCompLib.dll PIA

To edit the Web.config file

To add discount and ad labels to the default page

To add code to the default page

To create a page to record and redirect

To add references to the CacheCompLib.dll PIA

  1. Click Start, point to Programs, point to Microsoft Visual Studio .NET, and then click Microsoft Visual Studio .NET.
  2. In the Microsoft Development Environment [design] – Start Page screen, click Open Project.
  3. In the Open Project dialog box, select the NorthwindTraders project, and then click Open.
  4. In the NorthwindTraders – Microsoft Visual C# .NET [design] – Default.aspx window, in the Solution Explorer - NorthwindTraders window, right-click References, and then click Add Reference.
  5. In the Add Reference dialog box, in the .NET tab, select CacheCompLib.dll from the list, click Select, and then click OK.
  6. In the Solution Explorer - NorthwindTraders window, collapse the References node.

To edit the Web.config file

  1. In the Solution Explorer - NorthwindTraders window, right-click the web.config file, and then click Open.

  2. Remove the comment tags around the <caches> and <pipelines> tags for the following records (name=” ”):

    • Advertising
    • Discounts
    • Recordevents
  3. Add the following <contentSelection> record within the <CommerceServer> record:

    <contentSelection>
    <add name="advertising" cacheName="advertising" selectionPipeline="advertising" eventPipeline="recordEvent" redirectUrl="./redir.aspx"/>
    <add name="discounts" cacheName="discounts" selectionPipeline="discounts" eventPipeline="recordEvent" redirectUrl="./redir.aspx"/>
    </contentSelection>
    
    

To add discount and ad labels to the default page

  1. In the Solution Explorer - NorthwindTraders window, right-click the default.aspx file, and then click View Designer.
  2. In the NorthwindTraders – Microsoft Visual C# .NET [design] – Default.aspx window, click View on the toolbar, and then click Toolbox.
  3. In the Toolbox window, in the Web Forms section, drag and drop a Label object onto the form.
  4. In the Properties window, make sure the object you just created is selected, and then do the following:
    Use this To do this
    ID Type discountLabel.
    Text Delete the contents of this box.
  5. In the Toolbox window, in the Web Forms section, drag and drop another Label object onto the form.
  6. In the Properties window, make sure the object you just created is selected, and then do the following:
    Use this To do this
    ID Type adLabel.
    Text Delete the contents of this box.

To add code to the default page

  1. In the Solution Explorer - NorthwindTraders window, right-click the default.aspx file, and then click View Code.

  2. In the NorthwindTraders – Microsoft Visual C# .NET [design] – Default.aspx.cs window, in the using section, add the following lines of code:

    using System.Collections.Specialized;
    using Microsoft.CommerceServer.Runtime.Pipelines;
    using Microsoft.CommerceServer.Runtime.Targeting;
    using Microsoft.CommerceServer.Runtime.Profiles;
    using System.Diagnostics;
    
  3. In the Page_Load() method, add the following code:

    // Create a targeting profile
    CommerceContext ctx = CommerceContext.Current;
    Profile userProfile = null;
    bool isLoggedOn = ctx.AuthenticationInfo.IsAuthenticated();
    
    if (isLoggedOn)
    {
       // Extract the user's profile for the Campaign system to
       // decide if the user should be targeted for a given ad.
    
       userProfile = ctx.UserProfile;
    }
    //check for the profile
    
    
    // Get the AD
    
    // Get a ContentSelector and set its properties
    ContentSelector csoAd = ctx.TargetingSystem.SelectionContexts["advertising"].GetSelector();
    csoAd.TraceMode = true;
    csoAd.ItemsRequested = 1;
    
    
    if(null != userProfile) 
    {
       csoAd.Profiles.Add("User", userProfile);
    }
    
    csoAd.Size = "Banner";
    
    // Get some content using CS2002 Content Selection Framework
    StringCollection adpayload = csoAd.GetContent();
    foreach (string adItem in adpayload)
    {
       adLabel.Text = adLabel.Text + adItem.ToString() + "";
    }
    
    // Get the Discount
    
    // Create a targeting profile
    
    // ctx is still active
    
    // Get a ContentSelector and set its properties
    ContentSelector csoDiscount = ctx.TargetingSystem.SelectionContexts["discounts"].GetSelector();
    csoDiscount.TraceMode = true;
    csoDiscount.ItemsRequested = 1;
    
    if(null != userProfile) 
    {
       csoDiscount.Profiles.Add("User", userProfile);
    }
    
    
    // Get the content using CS2002 Content Selection Framework
    StringCollection discountPayload = csoDiscount.GetContent();
    foreach (string discountItem in discountPayload)
    {
       discountLabel.Text = discountLabel.Text + discountItem.ToString() + "";
    }
    

To create a page to record and redirect

  1. In the Solution Explorer - NorthwindTraders window, right-click the NorthwindTraders, point to Add, and then click Add Web Form.

  2. In the Add new Item - NorthwindTraders dialog box, in the Name box, type redir.aspx, and then click Open.

  3. In the Solution Explorer - NorthwindTraders window, right-click the redir.aspx file, and then click View Code.

  4. In the NorthwindTraders – Microsoft Visual C# .NET [design] – redir.aspx.cs window, in the using section, add the following lines of code:

    using System.Collections.Specialized;
    using Microsoft.CommerceServer.Runtime;
    using Microsoft.CommerceServer.Runtime.Pipelines;
    using Microsoft.CommerceServer.Runtime.Targeting;
    using Microsoft.CommerceServer.Runtime.Profiles;
    using Microsoft.CommerceServer.Runtime.Caching;
    using Microsoft.CommerceServer.Runtime.Diagnostics;
    using System.Diagnostics;
    using System.Runtime.Serialization;
    using System.Runtime.InteropServices;
    using Microsoft.CommerceServer.Interop.Caching;
    
  5. In the Page_Load() method, add the following code:

    //Get query string parameters
    NameValueCollection coll = Request.QueryString;
    //Records event
    //Note:  FormatterConverter is a .NET Framework function and it is used
    //       to convert the string to an int in the record event call
    FormatterConverter format = new FormatterConverter();
    RecordEvent(format.ToInt32(coll["ciid"]), coll["cachename"], format.ToInt32(coll["PageGroupId"]));
    //Redirect to target URL
    Response.Redirect(coll["url"]);
    
  6. In the public class redir section, add the following code:

    public void RecordEvent(int ciid, string cacheName, int pageGroupId) 
    {
       //Get Commerce Context.
       CommerceContext ctx = CommerceContext.Current;
       if(null == ctx) 
       {
          return;
       }
    
       //Get advertisement cache from cachemanager.
       CommerceCache cache = ctx.Caches[cacheName];
       if(null == cache) 
       {
          return;
       }
    
       Microsoft.CommerceServer.Runtime.IDictionary cacheData = (Dictionary) cache.GetCache();
       if(null == cacheData) 
       {
          return;
       }
    
       //Get record event pipeline from framework.
       PipelineBase recordEventPipeline = ctx.Pipelines["recordevent"];
       if(null != recordEventPipeline) 
       {
          //Create order and context dictionaries, assuming it will be successful.
          Microsoft.CommerceServer.Runtime.IDictionary orderDictionary = new Microsoft.CommerceServer.Runtime.Dictionary();
          Microsoft.CommerceServer.Runtime.IDictionary contextDictionary = new Microsoft.CommerceServer.Runtime.Dictionary();
          if(null == orderDictionary && null == contextDictionary) 
          {
             return;
          }
    
          //Initialize order dictionary.
          ContentListFactory contentFactory = null;
          orderDictionary["_winners"] = ciid;
          orderDictionary["_event"] = "CLICK";
    
          orderDictionary["_performance"] = cacheData["_performance"];
          contentFactory = (ContentListFactory)cacheData["Factory"];
          ContentList contentList = contentFactory.CreateNewContentList();
          orderDictionary["_content"] = contentList;
    
          //Initialize context dictionary.
          contextDictionary["SiteName"] = ctx.SiteName;
          contextDictionary["PageGroupId"] = pageGroupId;
    
          //Execute Record event pipeline and log event to weblog.
          recordEventPipeline.Execute(orderDictionary, contextDictionary);
    
          //Release refernces to COM objects.
          if(null != cacheData) 
          {
             Marshal.ReleaseComObject(cacheData);
          }            
          if(null != orderDictionary) 
          {
             Marshal.ReleaseComObject(orderDictionary);
          }            
          if(null != contextDictionary) 
          {
             Marshal.ReleaseComObject(contextDictionary);
          }
          if(null != contentFactory) 
          {
             Marshal.ReleaseComObject(contentFactory);
          }
          if(null != contentList) 
          {
             Marshal.ReleaseComObject(contentList);
          }         
       }
       else 
       {
          return;
       }
    }
    

Copyright © 2005 Microsoft Corporation.
All rights reserved.