【问题标题】:Sitecore Personalize is not workingSitecore Personalize 无法正常工作
【发布时间】:2015-08-05 04:15:05
【问题描述】:

我为 Sitecore 的 DMS 功能创建了一个简单的演示站点。 在 sitecore 内容中,我创建了这个结构:

首页
|-个性化
..|-HomeView1
..|-HomeView2

HomeView1、HomeView2 和 Home 的模板相同,只包含一个Field: Display Text

现在我为主页创建个性化设置,为其设置规则。 规则是当前月份是八月,并将个性化内容指向 HomeView1。 当我预览时,内容不会变成 HomeView1 的文本。 这是我的源代码:

public partial class HomePage : System.Web.UI.UserControl
{
    protected Item currentItem;
    protected void Page_Load(object sender, EventArgs e)
    {
        currentItem = Sitecore.Context.Item;
    }
}

这是我在主页上绑定的内容

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="HomePage.ascx.cs" Inherits="DMSDemo.sublayouts.HomePage" %>
<div><%= currentItem["Display Text"].ToString() %></div>

我想知道为什么Sitecore.Context.Item 在应用个性化规则时不会返回正确的项目(HomeView2)?

请给我一些建议。提前致谢。

【问题讨论】:

    标签: sitecore sitecore-dms


    【解决方案1】:

    当您个性化时,上下文项不会改变,子布局的数据源会。所以你应该将currentItem设置为数据源。

    这里有一些常用代码来获取我从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);
        }
    }
    

    其他几点:

    • 一般来说,avoid the use of the context item 是个好习惯
    • 没有要求您的页面项目(主页)是 相同的模板具有数据源项。
    • 我不认为个性化 在预览模式下工作,但您可以在编辑模式下查看。

    【讨论】:

    • 这个问题花了我很多时间。非常感谢戴维斯。解决了。​​
    猜你喜欢
    • 2020-01-27
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 2012-07-11
    • 2018-04-08
    • 2017-04-20
    相关资源
    最近更新 更多