如何获取自动映射玻璃项目 C# 的 Sitecore 数据项目 ID

How to get Sitecore Data Item ID for Automapped Glass item C#

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

在其他项目中,我有 class 继承自 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:

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

    // Rest of class
}

由于您的 MostRead class 继承自 GlassBase 您可以访问 Id 属性 就像您通常访问 属性 :

里面class:

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

class外:

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

var id = mostRead.Id;