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 20, 2016 @ 12:35
    Jonathan Ben Avraham
    0

    ContentService SaveAndPublish throwing ObjectReference Exception

    I have a class which is supposed to import an xml file to a List< T >, it does this successfully, the class is also supposed to store the content from the new List< T > into the Umbraco as Content utilizing the content service.

    Here is my code where I am going wrong somewhere:

    var contentService = ApplicationContext.Current.Services.ContentService;
                    var blogNode = Umbraco.TypedContent(1053);
    
                    foreach (var post in lstblogs)
                    {
                        var newBlog = contentService.CreateContent(post.Title, 1053, "umbNewsItem", 0);
    
                        newBlog.SetValue("title", post.Title);
                        newBlog.SetValue("bodyText", post.BodyText);
                        newBlog.SetValue("publishDate", post.PublishDate);
                        newBlog.SetValue("author", post.Author);
    
                        if (newBlog == null)
                        {
                            throw new Exception("Object is null.");
                        }
                        if (contentService == null)
                        {
                            throw new Exception("contentService is null.");
    
                        }
    
                        contentService.SaveAndPublish(newBlog, 0, true);
                        Console.WriteLine(newBlog);
                    }
    

    The exception is thrown on this line:

    contentService.SaveAndPublish(newBlog, 0, true);

    and the exception reads:

    {"Object reference not set to an instance of an object."}

    Any suggestions as to where I'm going wrong?

    Update

    So basically it looks like contentService is coming up null. However it doesn't throw the error when the check is done? Whenever I use .Save(newBlog) instead of

    .SaveAndPublishWithStatus(newBlog, 0, true) the code compiles and adds the content to the database but not the backoffice. What is the reason behind this. There is a lack of documentation on the ContentService methods and how they work.

    Thanks in advance!

  • Kevin Jump 2306 posts 14669 karma points MVP 7x c-trib
    May 23, 2016 @ 09:21
    Kevin Jump
    1

    Hi Jonathan

    when you fire SaveAndPublishWithStatus - umbraco saves the everything you've set so often the error is within one of the values

    I usally wrap any setvalue calls with a

    if (node.HasProperty("propname")) { ...
    

    just to make sure i'm not setting anything that it doesn't have.

    also null checks on the values themselves might be worth looking at

    because Save is working it might also be worth digging into the logs (/app_data/logs/) a bit and seeing if there is anything else the true at the end of saveandpublish triggers the sending of events.

    As far as i understand it things like updating the cache happen here, so it might be the putting the value into the xml cache on publish that is failing. you might get a better level of error by looking in the log file. for this.

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

    Save is working but not completely, it is saving the content to the db but not to the Umbraco backoffice. And even when I try wrapping the setValues inside a

    if (blogNode.HasProperty("title")) {
    

    I still get a null reference error.

    Thanks, Jonathan

  • Jonathan Ben Avraham 43 posts 216 karma points
    May 31, 2016 @ 10:17
    Jonathan Ben Avraham
    0

    Turns out I had used the wrong parentId in this line:

    var newBlog = contentService.CreateContent(post.Title, 1053, "umbNewsItem", 0);
    

    the correct statement was:

    var newBlog = contentService.CreateContent(post.Title, 1061, "umbNewsItem", 0);
    
Please Sign in or register to post replies

Write your reply to:

Draft