Copied to clipboard

Flag this post as spam?

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


  • Carsten Andersen 8 posts 77 karma points
    May 31, 2019 @ 12:04
    Carsten Andersen
    0

    Unable to use SaveAndPublish from ContentService

    Hello, I'm trying to save my IContent called child, but on this line(contentService.SaveAndPublish(child);) I get the following error: Object reference not set to an instance of an object.

    if (child.HasProperty("navn"))
    {
        child.SetValue("navn", worker.Name.ToString(), "da-dk");
    }
    contentService.SaveAndPublish(child);
    

    This is how I define my contentService:
    IContentService contentService = Umbraco.Core.Composing.Current.Services.ContentService;

    And I'm finding the children like this:

    long totalChildren;
    
    IEnumerable<IContent> children = contentService.GetPagedChildren(filialsParent.Id, 0, 100, out totalChildren);
    

    ยด Can someone point out what is wrong here?

  • Mario 111 posts 709 karma points c-trib
    Jun 04, 2019 @ 11:21
    Mario
    0

    If you are using one of the Umbraco controllers (Surface, Mvc or Api controller) you should be able to use something like this:

    var node = Services.ContentService.Create("name", parentId, contentTypeAlias);
    
    node.SetValue("myProperty", "someValue");
    
    Services.ContentService.SaveAndPublish(node);
    

    The Services object is available in the controllers already and it includes singletons for all the services.

  • Bo Jacobsen 293 posts 1281 karma points
    Jul 25, 2019 @ 12:08
    Bo Jacobsen
    0

    Hi Carsten.

    Have you tried with setting the same culture on the SaveAndPublish event?

    contentService.SaveAndPublish(child, "da-dk");
    

    But if you create a new multilangual content, then you cant SaveAndPublish it.

    // Cannot save content with an empty name.
    var content = _contentService.Create("contentName", 1025, "contentTypeAlias");
    _contentService.SaveAndPublish(content, "da-dk");
    
Please Sign in or register to post replies

Write your reply to:

Draft