Quantcast
Channel: Using Optimizely Platforms
Viewing all articles
Browse latest Browse all 8020

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

$
0
0

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.


Viewing all articles
Browse latest Browse all 8020

Trending Articles