Sitecore Personalize not working
I am creating a simple demo site for DMS Sitecore feature. In the sitecore file, I created this structure:
Home
| Personalize
.. | -HomeView1
.. | -HomeView2
HomeView1, HomeView2 and Home all have the same template which contains only one Field: Display Text
Now I create a personalization for the home page, set a rule for it. The rule is the current month is August and specify Personalize Content in HomeView1. When I do a preview, the content doesn't change to the HomeView1 text. Here's my source code:
public partial class HomePage : System.Web.UI.UserControl
{
protected Item currentItem;
protected void Page_Load(object sender, EventArgs e)
{
currentItem = Sitecore.Context.Item;
}
}
And this is what I linked on the homepage
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="HomePage.ascx.cs" Inherits="DMSDemo.sublayouts.HomePage" %>
<div><%= currentItem["Display Text"].ToString() %></div>
I wonder why Sitecore.Context.Item
won't return the correct item (HomeView2) when the personalization rule is applied?
Please give me some advice. Thanks in advance.
source to share
The context element does not change when personalized, this datasource is sublayout. Therefore, you must install currentItem
in the data source.
Here is some generic code for getting the datasource that I copy / paste from Matthew Dresser 's Blog :
var sublayout = this.Parent as Sitecore.Web.UI.WebControls.Sublayout;
if (sublayout != null)
{
Guid dataSourceId;
Sitecore.Data.Items.Item dataSource;
if (Guid.TryParse(sublayout.DataSource, out dataSourceId))
{
dataSource = Sitecore.Context.Database.GetItem(new ID(dataSourceId));
}
else
{
dataSource = Sitecore.Context.Database.GetItem(sublayout.DataSource);
}
}
Some other points:
- In general, it is good practice to avoid using context clause
- Your page element (Home) is not required to be the same template contains data items.
- I don't think personalization works in preview mode, but you can check it out in edit mode.
source to share