首页 > 解决方案 > 无法获取 IContent 的子级

问题描述

我正在尝试获取Children一个Icontent对象。

IContent filialsParent = cs.GetById(filialParrentId);

if (filialsParent != null)
{
    IContentService contentService = Umbraco.Core.Composing.Current.Services.ContentService;
    bool hasChildren = contentService.HasChildren(filialsParent.Id);
    long totalChildren;

    IEnumerable<IContent> children = contentService.GetPagedChildren(filialsParent.Id, 1, 100, out totalChildren);

    foreach (var child in children)
    {
        context.WriteLine(string.Format("child: {0}", child.Name));
    }

    context.WriteLine(string.Format("Children found:({0}) in: {1}", children.Count(), filialParrentId));

}

如果我调试代码,我会得到以下信息。

线路运行后,我的long totalChildren意志为 1 。contentService.GetPagedChildren(filialsParent.Id, 1, 100, out totalChildren);

IEnumerable<IContent> children的为空,因此(当然)是我的children.Count()0

可悲的是filialsParent,它没有.children()像我希望的那样包含该功能。

有没有办法获得 my 的孩子filialsParent,是的,它确实有已出版的孩子。

标签: umbracoumbraco8

解决方案


我遇到了完全相同的问题。出于测试目的,我删除了所有内容,只有基本必需品。

==> umbraco 8.0.2

确保你有一个父母和几个孩子

// For testing purposes hardcode your parent Id
var contentId = [ID];

// SET for returning total records
long totalChildren;

// int id ==> You even could hardcode your first param (contentID in here)
// long pageIndex ==> SET your index to 0 ==> first indexpage starts at 0 and not 1 ==> if you set this to 1 and the Pagesize = 100  and you have only 99 childeren you wil wil get null because we are requesting the second page
// int pageSize ==> We need max 10 childeren
// out long totalChildren 
// IQuery<IContent> filter = null ==> not used 
// Ordering ordering = null ==> not used
IEnumerable<IContent> children = Services.ContentService.GetPagedChildren(contentId, 0, 10, out totalChildren);

推荐阅读