umbraco / Umbraco-CMS

Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
https://umbraco.com
MIT License
4.42k stars 2.67k forks source link

Content Service - can't create a node then Save and Publish - have to Save first #16637

Closed cheeseytoastie closed 2 months ago

cheeseytoastie commented 3 months ago

Which Umbraco version are you using? (Please write the exact version, example: 10.1.0)

14.0.0

Bug summary

If you create a new node then just use SaveAndPublish it fails to save and publish.

If you use Save() first then SaveAndPublish it works

Simple example in a Razor view below -

@using Umbraco.Cms.Web.Common.PublishedModels;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Umbraco.Cms.Web.Common.PublishedModels.GeneralPage>
@inject IContentService ContentService
@using Umbraco.Cms.Core.Services
@{
    Layout = null;

    // parent page  where you want to add a child item.
    var parentId = Guid.Parse("0c6ff066-e843-4518-b90b-dd75b61d9561");

    var testNewPageThatWorks = ContentService.Create("New Page Test", parentId, "generalPage", -1);

    testNewPageThatWorks.SetValue("pageTitle", "This is a test");

    // Save and publish the child item - but if we save first then it works!!
    ContentService.Save(testNewPageThatWorks);
    ContentService.SaveAndPublish(testNewPageThatWorks);

    var testNewPageThatDoesntWork = ContentService.Create("New Page Test That Fails", parentId, "generalPage", -1);

    // Save and publish the child item
    ContentService.SaveAndPublish(testNewPageThatDoesntWork);

}

Specifics

No response

Steps to reproduce

Run the razor above in a clean Umbraco install (obviously need the corresponding generalPage doc type.

Expected result / actual result

I would expect two nodes - one from the save -> save and publish route then the second (which isn't created) just using saveandpublish.

Noted by Treefiddy in the Discord and can recreate.


This item has been added to our backlog AB#41896

github-actions[bot] commented 3 months ago

Hi there @cheeseytoastie!

Firstly, a big thank you for raising this issue. Every piece of feedback we receive helps us to make Umbraco better.

We really appreciate your patience while we wait for our team to have a look at this but we wanted to let you know that we see this and share with you the plan for what comes next.

We wish we could work with everyone directly and assess your issue immediately but we're in the fortunate position of having lots of contributions to work with and only a few humans who are able to do it. We are making progress though and in the meantime, we will keep you in the loop and let you know when we have any questions.

Thanks, from your friendly Umbraco GitHub bot :robot: :slightly_smiling_face:

Zeegaan commented 2 months ago

Heyo πŸ‘‹ This is expected behavior from V14 and onwards 😁 We have obsoleted the SaveAndPublish method on the content service, at it is now split into separate Save & Publish methods 😁

Here is the message: image

So the code to save and publish should now look more like:

  var parentId = Guid.Parse("aa496be1-2d77-4c51-8201-a492dba317d1");

  var testNewPageThatWorks = _contentService.Create("New Page Test", parentId, "root", -1);

  testNewPageThatWorks.SetValue("title", "This is a test");

  // Save and publish the child item - but if we save first then it works!!
  _contentService.Save(testNewPageThatWorks);
  _contentService.Publish(testNewPageThatWorks, Array.Empty<string>());
cheeseytoastie commented 2 months ago

The method is marked as Deprecated not obsolete so I'd expect old behaviour.

The documentation also has this as the recommended way. Both should be updated before this ticket is just closed IMHO https://docs.umbraco.com/umbraco-cms/reference/management/using-services/contentservice

Zeegaan commented 2 months ago

It is deprecated, but the warning message warns about it no longer saving 😁 Good point about documentation, you can create an issue on the Docs repo, to make sure it is handled here: https://github.com/umbraco/UmbracoDocs/issues

Or I can open it for you if you prefer that πŸ’ͺ