4

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?

Carsten Løvbo Andersen
  • 22,170
  • 9
  • 42
  • 67
  • 1
    Tried following - https://cultiv.nl/blog/using-hangfire-for-scheduled-tasks-in-umbraco/ ? – Matt Jun 06 '19 at 15:24
  • Maybe there's an event listener subscribed to Publishing with some code that's breaking your data after it's been saved but before it's published.[https://our.umbraco.com/documentation/reference/events/contentservice-events](https://our.umbraco.com/documentation/reference/events/contentservice-events). Even if that's not the problem you could make an event listener for the publishing event and see if your data is making it that far or not. – LinuxDisciple Jun 06 '19 at 23:22
  • @Matt Yes that is the guide i follow, note that the error has nothing to do with hangfire – Carsten Løvbo Andersen Jun 07 '19 at 06:10
  • I think it maybe due to Umbraco.Core.Composing.Current been null. Not done any v8 but in 7 the applicationContext would be null. – Matt Jun 07 '19 at 11:02
  • @Matt Thanks for your comment, I haven't tested it yet, but do you have an idea how to fix it. – Carsten Løvbo Andersen Jun 07 '19 at 11:06
  • @CarstenLøvboAndersen I don't unfortunately there is a really good thread on the community forums about this process though - https://our.umbraco.com/forum/umbraco-8/95966-umbraco-8-and-hangfire – Matt Jun 07 '19 at 11:24

3 Answers3

4

I believe you're getting your ContentService the wrong way and therefore it may be empty, causing a null reference exception.

If you're in a SurfaceController, you can get ContentService like this:

var cs = Services.ContentService;

If you're in a class where Services is not exposed, you can get it like this:

var cs = ApplicationContext.Current.Services.ContentService;

Read more about it in Umbracos documentation below :)

https://our.umbraco.com/documentation/Reference/Management/Services/ContentService/

Mikkel
  • 1,742
  • 1
  • 13
  • 28
3

I found out that if I do this then it works.

var umbf = Umbraco.Web.Composing.Current.Factory.GetInstance<IUmbracoContextFactory>();
using (var contextf = umbf.EnsureUmbracoContext())
{
    var umbcontext = contextf.UmbracoContext;
    IContentService cs = Umbraco.Core.Composing.Current.Services.ContentService;
    cs.SaveAndPublish(child);

}
Carsten Løvbo Andersen
  • 22,170
  • 9
  • 42
  • 67
0

Look that link, seems that Umbraco 'Save' works even if something is null but not completely:

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.

In the OP case he's taking the wrong contentService in the first step, so i think @Mikkel answer is not completely wrong:

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);

L-

Legion
  • 744
  • 6
  • 22