Copied to clipboard

Flag this post as spam?

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

  • Chris Norwood 101 posts 547 karma points
    May 07, 2020 @ 22:18
    Chris Norwood

    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?!

  • Kevin Jump 1996 posts 12598 karma points MVP 5x c-trib
    May 08, 2020 @ 07:27
    Kevin Jump

    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

    using (var contextReference = umbracoContextFactory.EnsureUmbracoContext())
        // do the publish stuff here 

    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 the EnsureUmbracoContext 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.

  • Nemanja Stankovic 7 posts 77 karma points
    Sep 06, 2020 @ 09:21
    Nemanja Stankovic


    This is a nerve-saving tip, THANK YOU!!!

    I was struggling for two days on how to save variations through code.



    Cheers, Nemanja.

  • Chris Norwood 101 posts 547 karma points
    May 08, 2020 @ 11:43
    Chris Norwood

    Thank you Kevin - you may have just saved my life! #H5YR

    I now have the next error, but I was already aware of that :)

  • Dee 114 posts 334 karma points
    Nov 06, 2020 @ 15:37

    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?



  • Kevin Jump 1996 posts 12598 karma points MVP 5x c-trib
    Nov 06, 2020 @ 15:58
    Kevin Jump


    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 So i am not sure if you need to update to a later version again.

  • Dee 114 posts 334 karma points
    Nov 06, 2020 @ 16:05

    Thanks for sharing this new issue!

  • Damien Holley 148 posts 487 karma points
    May 21, 2021 @ 00:53
    Damien Holley

    This may still be an issue in 8.13.0

  • Rob Shaw 25 posts 148 karma points c-trib
    Oct 21, 2021 @ 11:29
    Rob Shaw

    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.

Please Sign in or register to post replies

Write your reply to: