无法获得IContent的子级

时间:2019-05-31 07:10:01

标签: umbraco umbraco8

我正在尝试获取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运行后,我的contentService.GetPagedChildren(filialsParent.Id, 1, 100, out totalChildren);将为1。

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

不幸的是,filialsParent不包含我希望的功能.children()

有没有办法获取我的filialsParent的孩子,是的,确实有要发布的孩子。

1 个答案:

答案 0 :(得分:1)

我没有遇到完全相同的问题。为了进行测试,我删除了所有内容,仅删除了基本要点。

==> 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);