Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jonathan Ben Avraham 43 posts 216 karma points
    May 12, 2016 @ 08:51
    Jonathan Ben Avraham
    0

    ContentService returning null values

    I'm developing a package for Umbraco which allows backoffice users to export blogs into xml format. However for some reason my list method which stores the values from the ContentService is storing the correct values, however it's also storing null values.

    Here is my method, perhaps someone could tell me why this might be doing what it is:

    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()
            {
                    Id = node.Id,
                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()
            }).ToList();
        }
    

    Thanks in advance!

    please note:

    Before suggesting using .Value in my GetProperty statements, that throws a nullreference error saying

    Object reference not set to an instance of an object

  • Jonathan Ben Avraham 43 posts 216 karma points
    May 12, 2016 @ 12:59
    Jonathan Ben Avraham
    100

    Found the answer:

    For the same package I developed an import class and when I thought it wasn't working, it was, it just wasn't working properly so there were blogposts being saved on the database unpublished but weren't on the backoffice. That I'll ask about in a different question. But to save that from causing issues in the export part of the package this was my solution:

    var xEle = new XElement("Blogs",
                            from blog in BlogPostList
                            where blog.Title != null
                            where blog.BodyText != null
                            where blog.Author != null
                            where blog.Image != null
                            where blog.PublishDate != null
                            select new XElement("Blog",
                                            new XAttribute("Title", blog.Title),
                                            new XAttribute("BodyText", blog.BodyText),
                                            new XAttribute("PublishDate", blog.PublishDate),
                                            new XAttribute("Author", blog.Author),
                                            new XAttribute("Image", blog.Image))
                        );
    
Please Sign in or register to post replies

Write your reply to:

Draft