【问题标题】:How to get Sitecore Data Item ID for Automapped Glass item C#如何获取 Automapped Glass 项目 C# 的 Sitecore 数据项目 ID
【发布时间】:2018-06-13 14:22:39
【问题描述】:

我有一个继承 GlassBase 的自动映射类。这些字段会自动从 Sitecore 中提取并映射到类。我需要获取从中提取字段的数据项的 ID,但我不确定如何。

在其他项目中,我有从 GlassControl 继承的类,我可以获得 GlassItem.Id,但我在这个项目中没有 GlassControl,我正在使用 GlassBase。如何了解正在使用的玻璃物品?

public class MostRead : GlassBase
{
    private readonly IPersonifyUserContext _userContext;
    private readonly ISitecoreSearchProvider _searchProvider;
    private readonly ISitecoreContext _sitecoreContext;
    private readonly ICacheProvider _cacheProvider;
    private readonly I___Global_Page_Configuration _currentItem;

    public MostRead(IPersonifyUserContext userContext,ISitecoreContext context, ISitecoreSearchProvider searchProvider,ICacheProvider cache)
    {
        _cacheProvider = cache;
        _userContext = userContext;
        _searchProvider = searchProvider;
        _sitecoreContext = context;
        _currentItem = _sitecoreContext.GetCurrentItem<I___Global_Page_Configuration>();
    }
    [TopicsQueryAttirbute]
    public IEnumerable<ITopic> AllTopics { get; set; }

    [SitecoreField(IMost_ReadConstants.CountFieldName)]
    public int Count { get; set; }

    [SitecoreField(IMost_ReadConstants.PastXDaysFieldName)]
    public int Days { get; set; }

    [SitecoreField(IMost_ReadConstants.TopicsFieldName)]
    public IEnumerable<Guid> Topics { get; set; }

    [SitecoreField(IMost_ReadConstants.TitleFieldName)]
    public string Title { get; set; }

    [SitecoreField(IMost_ReadConstants.SiteFieldName)]
    public string Site { get; set; }
}

【问题讨论】:

  • GlassBase 有一个 Id 属性。这不是你需要的吗?
  • 如何访问它?我不能做'var id = GlassBase.Id;'
  • 只是“this.Id”吗?
  • 如果您需要从您发布的课程中访问它,那么您可以访问IDthis 是多余的。
  • 您的类MostRead 继承自GlassBase,这意味着GlassBase 的所有公共或受保护成员都可以访问。据我所知,GlassBase 有一个名为 Id 的公共 Guid

标签: c# sitecore automapper glass-mapper automap


【解决方案1】:

GlassBase 应该包含一个名为 Id 的属性:

public abstract class GlassBase : IGlassBase
{
    [SitecoreId]
    public virtual Guid Id{ get; private set;}

    // Rest of class
}

由于您的MostRead 类继承自GlassBase,因此您可以像访问属性一样访问Id 属性:

类内:

// 'this' is redundant, but you could still use it
var id = this.Id;
// or
var id = Id;

课外:

// Whatever your MostRead variable name is...
var mostRead = new MostRead();

var id = mostRead.Id;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多