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 91 posts 493 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 1768 posts 11344 karma points MVP 4x 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 6 posts 76 karma points
    22 days ago
    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 91 posts 493 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 :)

Please Sign in or register to post replies

Write your reply to: