November Happy Hour will be moved to Thursday December 5th.

How can I convert a ContentData object to a ContentReference so that I can get the children of the ContentData object?

Vote:
 

So I'm trying to build a recursive tree of ContentData objects starting from the ContentReference.RootPage.  I'm working in EPiServer 8.  It looks like the IContentLoader method GetChildren would do the trick, but it's only allowing me to send a ContentReference to the GetChildren argument.

If I knew how to convert a ContentData object to a ContentReference object, I could probably make this work.  But I don't know if that's possible.  Here is my latest attempt.

// These 3 lines do what I expect.
IContentLoader contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
List<ContentData> contentDataList = contentLoader.GetChildren<ContentData>(ContentReference.RootPage).ToList();
ContentData rootContentData = (ContentData)ServiceLocator.Current.GetInstance<IContentRepository>().Get<IContent>(ContentReference.RootPage);

foreach (ContentData child in contentDataList)
{
    // This next line fails with the error:  Cannot convert from 'EPiServer.Core.ContentData' to 'EPiServer.Core.ContentReference'
    List<ContentData> children = contentLoader.GetChildren<ContentData>(child);

    //ContentReference childRef = (ContentReference)child; // Nope
    //ContentReference childRef = child as ContentReference; // Nope
}

How can I get the children of a ContentData object?  Thank you.

#199548
Edited, Nov 29, 2018 23:27
Vote:
 

Try child.ContentLink

You can also use GetDescendants (not cached). Depends what you are trying to do...

#199549
Nov 30, 2018 0:43
Vote:
 

Thanks Aniket. I'm not seeing a ContentLink property via Intellisense or the documentation.  Errors that I'm seeing when attempting that are in the comments below.

foreach (ContentData child in contentDataList)
{
    var x = child.ContentLink; // Cannot assign method group to an implicitly-typed variable.
    List<ContentData> children = contentLoader.GetChildren<ContentData>(child.ContentLink); // Cannot convert from 'method group' to 'ContentReference'
}

Where is the GetDescendants method for that?  I don't see that either.  Do I need to cast that ContentData child object first?

What I'd like to do is get the descendants of that ContentData child object.  Thanks.

#199564
Edited, Nov 30, 2018 15:17
Vote:
 

ContentData does not have a ContentLink property, you should use IContent instead.

/Jake

#199566
Nov 30, 2018 17:27
Vote:
 

Thank you Jake.

#199568
Nov 30, 2018 18:48
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.