0
votes

I have a blog export package which exports blog content in Umbraco to XML.

Now I want to export comment data, the comments section is set as a childNode on the NewsItem node, how can I use this format to grab the data from the childNode into the list?

Here is my code:

public List<BlogPosts> getPostList()
{
    var contentType = ApplicationContext.Current.Services.ContentTypeService
        .GetContentType("umbNewsItem");
    var nodes = ApplicationContext.Current.Services.ContentService
        .GetContentOfContentType(contentType.Id).Select(content => new Node(content.Id));

    return nodes.Select(node => new BlogPosts()
    {
        Title = node.GetProperty("title").ToNullSafeString(),
        BodyText = node.GetProperty("bodyText").ToNullSafeString(),
        PublishDate = node.GetProperty("publishDate").ToNullSafeString(),
        Author = node.GetProperty("author").ToNullSafeString(),
        Image = node.GetProperty("image").ToNullSafeString(),
        //This is where I want to grab the blog comments content
        Comments = node.ChildrenAsList.Add("comments") 
    }).ToList();
}

My first attempt with this, I get an error on the .Add("comments") line which reads:

The best overloaded method match for 'System.Collections.Generic.List<umbraco.interfaces.INode>.Add(umbraco.interfaces.INode)' has some invalid arguments

the next thing I tried was this:

Comments = node.ChildrenAsList<BlogComment>.Add("comments").ToList()

which returns the following error:

The property 'umbraco.NodeFactory.Node.ChildrenAsList' cannot be used with type arguments

I have also tried this:

Comments = node.ChildrenAsList.Add("comments").ToList()

which returned this error:

The best overloaded method match for 'System.Collections.Generic.List<umbraco.interfaces.INode>.Add(umbraco.interfaces.INode)' has some invalid arguments

This is my BlogPosts model:

public class BlogPosts
{
    public string Title { get; set; }
    public string BodyText { get; set; }
    public string PublishDate { get; set; }
    public string Author { get; set; }
    public string Image { get; set; }
    public List<BlogComment> Comments { get; set; }
}

public class BlogComment
{
    public string Comment { get; set; }
    public string CommentDate { get; set; }
}

This is an example of the Umbraco backoffice page: Image

I've searched throughout stackoverflow and google for anything which refers to calling data from a childNode into a list but the list type here is INode, when using this:

Comments = node.ChildrenAsList

it returns this error:

Cannot implicitly convert type 'System.Collections.Generic.List<umbraco.interfaces.INode>' to 'System.Collections.Generic.List<UmbracoBlogsExportPackage.Models.BlogComment>'
1
Dude. You HAVE to describe what you've tried. You HAVE to show that you have researched just a little bit before asking on Stack Overflow. You HAVE to describe what isn't working. Otherwise we can't (or won't) help you. I look at this question and think "what is he having problems with?" because you don't include ANY error information OR any info on what you've already tried... Here's help to ask a good question: stackoverflow.com/help/how-to-askJannik Anker
Stack Overflow is NOT meant to be a "I want this, please do it for me" forum. YOU have to (want to) do it and LEARN from it. Then, when you're really stuck AND you've researched everything AND you've tried everything you can think of, you can put all that in your question - and get better help.Jannik Anker
I've even tried: Comments = node.ChildrenAsList.Find(System.Predicate<umbraco.interfaces.Comments) but it returns this error: The best overloaded method match for 'System.Collections.Generic.List<umbraco.interfaces.INode>.Find(System.Predicate<umbraco.interfaces.INode>)' has some invalid argumentsJonathan Yaniv Ben Avraham

1 Answers

3
votes

Okay then :-)

  • First of all, .Add() tries to add something to a collection, so that won't work here.

  • Second, I think selecting Content as Nodes is a bit backwards, so I would try not to do that.

  • Third, IEnumerable have a Cast() method that I think might work here. I can't really test it, though.

Again, this is very untested, but maybe try something like this? Obviously I don't know the Comment DocType alias, so remember to change that bit :-)

public List<BlogPosts> getPostList()
{
    var contentType = UmbracoContext.Current.Application.Services.ContentTypeService
        .GetContentType("umbNewsItem");
    var contentService = UmbracoContext.Current.Application.Services.ContentService;
    var nodes = contentService.GetContentOfContentType(contentType.Id);

    return nodes.Select(node => new BlogPosts()
    {
        Title = node.GetValue("title").ToNullSafeString(),
        BodyText = node.GetValue("bodyText").ToNullSafeString(),
        PublishDate = node.GetValue("publishDate").ToNullSafeString(),
        Author = node.GetValue("author").ToNullSafeString(),
        Image = node.GetValue("image").ToNullSafeString(),
        //This is where I want to grab the blog comments content
        Comments = contentService.GetChildren(node.Id).Where(x => x.ContentType.Alias == "Comment").Cast<BlogComment>().ToList()
    }).ToList();
}