Publishing content programatically returns an error
Hi Umbracians!
We have a site which has recently been migrated to version 8.6.1. Part of the site functionality is loading automatic data imports every night and creating/updating/unpublishing Umbraco nodes from the incoming.
This was working in version 7; however, in version 8, every attempt to call the saveAndPublish method on the ContentService causes an exception:
System.NullReferenceException: Object reference not set to an instance of an object.
at Umbraco.Web.Routing.RedirectTrackingComponent.StoreOldRoute(IContent entity, OldRoutesDictionary oldRoutes) in D:\a\1\s\src\Umbraco.Web\Routing\RedirectTrackingComponent.cs:line 99
at Umbraco.Web.Routing.RedirectTrackingComponent.ContentService_Publishing(IContentService sender, PublishEventArgs`1 args) in D:\a\1\s\src\Umbraco.Web\Routing\RedirectTrackingComponent.cs:line 62
at Umbraco.Core.Events.TypedEventHandler`2.Invoke(TSender sender, TEventArgs e)
at Umbraco.Core.Events.QueuingEventDispatcherBase.DispatchCancelable[TSender,TArgs](TypedEventHandler`2 eventHandler, TSender sender, TArgs args, String eventName) in D:\a\1\s\src\Umbraco.Core\Events\QueuingEventDispatcherBase.cs:line 54
at Umbraco.Core.Services.Implement.ContentService.StrategyCanPublish(IScope scope, IContent content, Boolean checkPath, IReadOnlyList`1 culturesPublishing, IReadOnlyCollection`1 culturesUnpublishing, EventMessages evtMsgs, ContentSavingEventArgs savingEventArgs, IReadOnlyCollection`1 allLangs) in D:\a\1\s\src\Umbraco.Core\Services\Implement\ContentService.cs:line 2588
at Umbraco.Core.Services.Implement.ContentService.CommitDocumentChangesInternal(IScope scope, IContent content, ContentSavingEventArgs saveEventArgs, IReadOnlyCollection`1 allLangs, Int32 userId, Boolean raiseEvents, Boolean branchOne, Boolean branchRoot) in D:\a\1\s\src\Umbraco.Core\Services\Implement\ContentService.cs:line 1147
at Umbraco.Core.Services.Implement.ContentService.SaveAndPublish(IContent content, String culture, Int32 userId, Boolean raiseEvents) in D:\a\1\s\src\Umbraco.Core\Services\Implement\ContentService.cs:line 903
at BedsUni.WebsiteV8.Services.ContactImportService.<>c__DisplayClass9_0.<ImportFromXmlFile>b__0(XElement c) in E:\BedsUni\BedsUniWebsiteV8\BedsUni.WebsiteV8\Services\ContactImportService.cs:line 206
The site has no variants (just a single language); I looked up the source code for this and it seems to be occurring on line 99 of this file:
This suggests that either the _publishedSnapshotAccessor or the
_publishedSnapshotAccessor.PublishedSnapshot is null in the StoreOldRoutes method. These documents are often new and should not have any "old routes" - the documents are not being moved.
Has anybody experienced anything similar to this? I can create the content and save it, but I can't publish it.
I've tried creating a template for these content items and moving them into one of the websites (there are multiple websites being managed by the same Umbraco instance) but nothing seems to make a difference.
Has anybody come across anything similar, and if so have you managed to find a solution?!
This will work for single or even a few SaveAndPublish calls....
However, as the issues at the top show I get a 'Nasty Exception' when adding multiple items which traces its routes back to the examine indexes being built.
So at the moment I am a little bit further on but stuck at the next stage - but there is a fix coming.
I haven't had a chance to take an in depth look since the updates - so i am not sure, but there are later issues in for example v8.8.0 that address issues with Examine and background scope
I can confirm I have the exact same issue in version 8.16.0 with an Azure SQL server database currently only on my localhost environment.
I have created a migration task to programmatically create content. I have made sure that all creating, saving and publishing is wrapped within the above suggestion of a _umbracoContextFactory.EnsureUmbracoContext()
What I find strange is that the content is created and published successfully but I have to restart the solution in order to get it to boot correctly.
This issue is happening very consistently at the moment so I'm going to try a few things and report back if I have any success.
Publishing content programatically returns an error
Hi Umbracians!
We have a site which has recently been migrated to version 8.6.1. Part of the site functionality is loading automatic data imports every night and creating/updating/unpublishing Umbraco nodes from the incoming.
This was working in version 7; however, in version 8, every attempt to call the saveAndPublish method on the ContentService causes an exception:
The site has no variants (just a single language); I looked up the source code for this and it seems to be occurring on line 99 of this file:
https://github.com/umbraco/Umbraco-CMS/blob/v8/contrib/src/Umbraco.Web/Routing/RedirectTrackingComponent.cs
This suggests that either the _publishedSnapshotAccessor or the _publishedSnapshotAccessor.PublishedSnapshot is null in the StoreOldRoutes method. These documents are often new and should not have any "old routes" - the documents are not being moved.
Has anybody experienced anything similar to this? I can create the content and save it, but I can't publish it.
I've tried creating a template for these content items and moving them into one of the websites (there are multiple websites being managed by the same Umbraco instance) but nothing seems to make a difference.
Has anybody come across anything similar, and if so have you managed to find a solution?!
Hi Chris,
I have gotten a little bit further but am stuck with another issue - #8030 which is being worked on #7994 as is down for 8.7 release.
if you are running in a background thread you will need to wrap the requests in an EnsureUmbracoContext. e.g
this removes the various null errors for me - as you now have an Umbraco context for the request.
You can Inject the
IUmbracoContextFactory
into the background task (i am running code in a background task similar to this example) So you can then call theEnsureUmbracoContext
method.Current Situation
This will work for single or even a few
SaveAndPublish
calls....However, as the issues at the top show I get a 'Nasty Exception' when adding multiple items which traces its routes back to the examine indexes being built.
So at the moment I am a little bit further on but stuck at the next stage - but there is a fix coming.
@Kevin,
This is a nerve-saving tip, THANK YOU!!!
I was struggling for two days on how to save variations through code.
Brilliant.
H5YR
Cheers, Nemanja.
Thank you Kevin - you may have just saved my life! #H5YR
I now have the next error, but I was already aware of that :)
Hey Kevin,
I am facing exactly the same issue. Since that issue was fixed in 8.6.4 I am not sure why its showing up in 8.7.0.
Were you able to solve this?
Thanks
Dee
Hi,
I haven't had a chance to take an in depth look since the updates - so i am not sure, but there are later issues in for example v8.8.0 that address issues with Examine and background scope
(e.g https://github.com/umbraco/Umbraco-CMS/pull/8772) So i am not sure if you need to update to a later version again.
Thanks for sharing this new issue!
This may still be an issue in 8.13.0
I can confirm I have the exact same issue in version 8.16.0 with an Azure SQL server database currently only on my localhost environment.
I have created a migration task to programmatically create content. I have made sure that all creating, saving and publishing is wrapped within the above suggestion of a _umbracoContextFactory.EnsureUmbracoContext()
What I find strange is that the content is created and published successfully but I have to restart the solution in order to get it to boot correctly.
This issue is happening very consistently at the moment so I'm going to try a few things and report back if I have any success.
Tip if anyone still gets this error. If you don't have any need for it, just disable the URL tracker - the error will go away.
is working on a reply...