Copied to clipboard

Flag this post as spam?

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


  • Chris Mahoney 233 posts 445 karma points
    Feb 15, 2016 @ 03:11
    Chris Mahoney
    0

    Exception when creating new content programmatically

    Hi,

    I'm trying to create a new content node from C#. I've looked around the forums and have found the following code, which apparently works for some people:

    using Umbraco.Core.Services;
    
    ContentService cs = new ContentService();
    var content = cs.CreateContent("Name", 1234, "DocType");
    cs.SaveAndPublishWithStatus(content);
    

    However, this doesn't seem to work. The code crashes on the very first line (the ContentService line, not the using!) with "Object reference not set to an instance of an object."

    Obviously that error isn't particularly helpful, but I'm not sure where to even begin troubleshooting this issue. It's crashing before the call to CreateContent so it's not due to an invalid parent ID or doctype, but rather is happening before anything "site-specific".

    I'm using umbraco.library.GetPreValues() in a different part of the code, and that's working, so it's "talking" to the Umbraco instance successfully.

    Any ideas? I'm using Umbraco 6.2.5 on Server 2008 R2.

    [Edited to make slightly clearer]

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Feb 15, 2016 @ 05:08
    Nicholas Westby
    1

    One would not typically create a new content service. IIRC, getting the content service usually works something like this:

    var cs = ApplicationContext.Current.Services.ContentService
    
  • Chris Mahoney 233 posts 445 karma points
    Feb 15, 2016 @ 19:07
    Chris Mahoney
    0

    Thanks, but no luck. Same error.

    For what it's worth, the "sample" that I was using was here, which apparently worked for one person ... but not me :)

  • Kevin Jump 2309 posts 14673 karma points MVP 7x c-trib
    Feb 15, 2016 @ 20:31
    Kevin Jump
    0

    Hi,

    The following will create content : Assuming 1054, is a valid node in your content tree, and you have a Alias called ContentPage

    var _contentService = ApplicationContext.Current.Services.ContentService;
    
    var content = _contentService.CreateContent(name, 1054, "ContentPage");
    if (content != null)
    {
        _contentService.SaveAndPublishWithStatus(content);
    }
    

    if this isn't working i would check the node number and that the doctype Alias is what you think it should be

    you can check the node id by calling get content

    var node = _contentService.GetById(1054);
    

    and you can see if you are getting the right content type using the ContentType service

    var _contentTypeService = ApplicationContext.Current.Services.ContentTypeService;
    var contentType = _contentTypeService.GetContentType("ContentPage");
    

    also to double check with these you could create content by passing the node

    _contentService.CreateContent(name, node, contentType.Alias);
    

    Kevin

  • Chris Mahoney 233 posts 445 karma points
    Feb 15, 2016 @ 20:36
    Chris Mahoney
    0

    Neither of these lines are working:

    var _contentService = ApplicationContext.Current.Services.ContentService;
    var _contentTypeService = ApplicationContext.Current.Services.ContentTypeService;
    

    They're both giving a NullReferenceException (Object reference not set to an instance of an object). Am I possibly missing a using? I have these:

    using umbraco.cms.businesslogic.web;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using Umbraco.Web.WebApi;
    

    Any other ideas?

  • Kevin Jump 2309 posts 14673 karma points MVP 7x c-trib
    Feb 15, 2016 @ 20:45
    Kevin Jump
    0

    Hi

    you should only need using Umbraco.Core for the code to work.

    where are you running the code from ? (Partial/Macro, App_Code or an external DLL?)

    I just pasted the example above directly from a class in App_Code.

  • Chris Mahoney 233 posts 445 karma points
    Feb 15, 2016 @ 20:58
    Chris Mahoney
    0

    Umbraco.Core is indeed the only one that's "lit up" in Visual Studio, so that makes sense.

    It's an external DLL. I've put the "umbracoDbDSN" connection string in app.config and that's sufficient to get prevalues from a different part of the code, but do I possibly need something else to access the ContentService?

  • Kevin Jump 2309 posts 14673 karma points MVP 7x c-trib
    Feb 15, 2016 @ 21:06
    Kevin Jump
    100

    Hi

    If the Dll is then copied into Umbraco's bin folder then you should be ok,

    As a Genral rule, I would add UmbracoCms.Core as a nuget package to your dll project. to make sure you are getting the right references in the project

    But if you are calling the code in the dll from outside of the Umbraco Application then there is quite a lot you need to do to get the umbraco invoked before you can do any content stuff:

    There are a couple examples of getting umbraco up for a command line

    https://github.com/sitereactor/umbraco-console-example (not sure if this is 100% upto date)

    And Chauffeur does it a quite clever way - that lets it run from the bin folder. (follow the logic from here https://github.com/aaronpowell/Chauffeur/blob/master/Chauffeur.Runner/Program.cs)

    if you want to do some quick and dirty mass content creation quickly i would put your code inside a UmbracoApiController (for security use UmbracoAuthorizedController) and then call the URL in a browser to do all the work inside umbraco

  • Chris Mahoney 233 posts 445 karma points
    Feb 15, 2016 @ 21:50
    Chris Mahoney
    0

    Apologies; it seems that I wasn't as forthcoming with information as I should've been - that's what happens when you're rushing to post before dashing out of the office! :)

    I already have the NuGet package so I should have all the necessary DLLs. Thanks for the links to that documentation. I'll post back with success/failure once I've taken a look.

  • Chris Mahoney 233 posts 445 karma points
    Feb 16, 2016 @ 00:15
    Chris Mahoney
    0

    OK, I've been through the console example and now have everything working :)

    Thanks again for the help!

  • berex 7 posts 77 karma points
    May 28, 2018 @ 10:11
    berex
    0

    I keep getting this error when trying to save content ....

    Exception Details: System.InvalidOperationException: Sequence contains no elements

    Here's my code:

    var content = contentService.CreateContent("My Post", parentId, "ExternalGroup");
    
    if (content != null)
         {
             post.SetValue("name", "Post Title");
             post.SetValue("bodyText", "Testing this Test");
    
             contentService.SaveAndPublishWithStatus(content);
         }
    
  • Chris Mahoney 233 posts 445 karma points
    May 28, 2018 @ 20:11
    Chris Mahoney
    0

    First of all, what is post? Is it supposed to be content?

    I've found my working code and - assuming that post is content - I've set the properties a bit differently:

    content.Properties["propertyName"].Value = value;
    

    Does that help at all?

Please Sign in or register to post replies

Write your reply to:

Draft