Sitecore 将所有项目加载到 MVC 模型中?

Sitecore Load all items into an MVC model?

我创建了一堆自定义模板来在 Sitecore 中存储项目(例如 IndustriesSubindustries 等)。我现在想将它们加载到我的 Sitecore MVC 模型中。


列表位于 sitecore > 内容 > 列表。例如,在 Lists 文件夹中有一个名为 Country 的文件夹。我想取回 Country 文件夹中 中的所有项目,并在我看来将它们填充为无序列表。


更新: 我实施了下面建议的 Glass.Mapper.Sc 方法。现已全面投入使用。

这是我的工作模型现在的样子:

using Glass.Mapper.Sc.Configuration;
using Glass.Mapper.Sc.Configuration.Attributes;
using Sitecore.Data.Items;
using Sitecore.Mvc.Presentation;
using System;
using System.Collections.Generic;

namespace Sitecore.Web.Models
{
    public class Registration: IRenderingModel
    {
        public Rendering Rendering { get; set; }
        public Item Item { get; set; }
        public Item PageItem { get; set; }

        public IEnumerable<CountryChildItem> CountryList { get; set; }

        [SitecoreType(AutoMap = true)]
        public class CountryItem
        {
            public virtual IEnumerable<CountryChildItem> Children { get; set; }
        }

        [SitecoreType(AutoMap = true)]
        public class CountryChildItem
        {
            [SitecoreId]
            public virtual Guid Id { get; set; }

            [SitecoreInfo(SitecoreInfoType.Path)]
            public virtual string Path { get; set; }

            [SitecoreField]
            public virtual string DisplayName { get; set; }

            [SitecoreField]
            public virtual string Abbreviation { get; set; }
        }

        public void Initialize(Rendering rendering)
        {
            Rendering = rendering;
            Item = rendering.Item;
            PageItem = PageContext.Current.Item;
        }
    }
}

这就是我的工作控制器的样子:

using Glass.Mapper.Sc;
using Sitecore.Web.Models;
using System.Web.Mvc;

namespace Sitecore.Web.Controllers
{
    public class RegistrationController : Controller
    {
        Registration registrationModel = new Registration();

        public ActionResult Index()
        {
            ISitecoreContext sitecoreContext = new SitecoreContext();
            ISitecoreService service = new SitecoreService(sitecoreContext.Database);
            Registration.CountryItem countryItem = service.GetItem<Registration.CountryItem>("/sitecore/content/Lists/Country");
            registrationModel.CountryList = countryItem.Children;
            return View(registrationModel);
        }
    }
}

以及我的工作视图的片段:

 <ul class="select-menu-options dropdown-menu">
    @foreach (var country in Model.CountryList)
    {
        <li><a href="#">@country.DisplayName</a></li>
    }
 </ul>

如果我处于你的位置,我会研究 Glassmapper for Sitecore。

对于 Sitecore,这是一个相当轻量级的 ORM。

http://www.glass.lu/Mapper/Sc

我还建议移动位于

中的列表
sitecore > Templates > User Defined > Lists > Content 

下的某些地方
sitecore > Content 

sitecore > System 

(以更有意义的为准)

更新: 尝试将此添加到 class:

上方
[SitecoreType(AutoMap =  true)]
public class CountryItem
{
    //...
}

如果您将 CountryItem 和其他模型 类 更改为从 SearchResultItem 继承:

[PredefinedQuery("TemplateID", ComparisonType.Equal, "{ID-OF-CountryItem-TEMPLATE}", typeof(ID))]
public class CountryItem : Sitecore.ContentSearch.SearchTypes.SearchResultItem
{
    [IndexField("_displayname")]
    public virtual string DisplayName { get; set; }

    [IndexField("abbreviation")]
    public string Abbreviation { get; set; }
}

您应该能够使用 Sitecore 索引检索所有国家和其他类似列表:

private static string IndexName
{
    get
    {
        return string.Format("sitecore_{0}_index", (Context.ContentDatabase ?? Context.Database).Name);
    }
}

private static string Language { get { return Context.Language.Name; } }

public IEnumerable<CountryItem> GetCountries() 
{
    using (var context = ContentSearchManager.GetIndex(IndexName).CreateSearchContext())
    {
        IQueryable<CountryItem> queryable = context.GetQueryable<CountryItem>();
        queryable = queryable.Where(i => i.Language == Language);
        queryable = queryable.Where(i => i.LatestVersion);
        // ... maybe excluding standard values or some other filters
        var searchResults = queryable.GetResults();
        return queryable.ToList();
    }
}

请注意,这只是一个示例。您需要对其进行测试,很可能会适应您的解决方案。

正如 Dar Brett 提到的,您不应在 Templates 节点下保留任何数据项。