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.
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.
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
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:
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!
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
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.
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
I still get a null reference error.
Thanks, Jonathan
Turns out I had used the wrong parentId in this line:
the correct statement was:
is working on a reply...