Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content Service - ContentSchedule.Add not saving schedule #7361

Closed
HefinJ opened this issue Dec 18, 2019 · 7 comments
Closed

Content Service - ContentSchedule.Add not saving schedule #7361

HefinJ opened this issue Dec 18, 2019 · 7 comments

Comments

@HefinJ
Copy link

HefinJ commented Dec 18, 2019

When creating content via the content service and setting a release and expire date - the content schedule does not save when reviewing the created content within Umbraco - content schedule is also empty when using contentService.GetById(1111).

Umbraco version

I am seeing this issue on Umbraco version: 8.4.0

Reproduction

Steps to reproduce

Create two document types Home and News Item, add a custom controller to the Home document type so that it creates a new news item each time the home page is loaded:

public class HomeController : Umbraco.Web.Mvc.RenderMvcController
{
          public override ActionResult Index(ContentModel model)
          {
               var cs = Services.ContentService;

               var newArticle = cs.CreateAndSave("News Article", 1084, "newsArticle");
               newArticle.ContentSchedule.Add(DateTime.Now.AddDays(1), DateTime.Now.AddDays(30));
               cs.SaveAndPublish(newArticle);

               return base.Index(model);
          }
}

Load the home page so that the contoller code is executed.

Expected result

New article to be created, with title of "News Article" and with Publish date of Tomorrows date and an unpublish date in 30 days time.

Actual result

Content was created as expected, but Publish schedule is empty when looking at the News Article in Umbraco.

@nul800sebastiaan
Copy link
Member

Might be because you're publishing it, I am not sure but Umbraco might assume you don't need the schedule any more since you've published it now. The point in scheduling a publish is that you have saved changes that you don't want to publish yet.

Does this work?

var newArticle = cs.CreateAndSave("News Article", 1084, "newsArticle");
cs.SaveAndPublish(newArticle);
newArticle.ContentSchedule.Add(DateTime.Now.AddDays(1), DateTime.Now.AddDays(30));
cs.Save(newArticle);

Note: I guess this is just sample code, but I do not see the point in publishing the node if you want to schedule the publish?

@nul800sebastiaan nul800sebastiaan added the state/needs-more-info We don't have enough information to give a good reply label Dec 19, 2019
@HefinJ
Copy link
Author

HefinJ commented Dec 19, 2019

Yes, this was just sample code. I've just retried it with your code amendment - same behaviour, the schedule gets dropped completely. Having tested this a bit more, it looks like an issue with the CreateAndSave method. If I use Create method instead like this:

var newArticle = cs.Create("News Article", 1084, "newsArticle");
newArticle.ContentSchedule.Add(DateTime.Now.AddDays(1), DateTime.Now.AddDays(30));
cs.SaveAndPublish(newArticle);

Or

var newArticle = cs.Create("News Article", 1084, "newsArticle");
newArticle.ContentSchedule.Add(DateTime.Now.AddDays(1), DateTime.Now.AddDays(30));
cs.Save(newArticle);

Then the content schedule is respected when I Save and Publish (content remains saved if publish date is in the future), or Save only.

The CreateAndSave method seems to be ignoring the Content Schedule completely.

@stevemegson
Copy link
Contributor

It looks like the change tracking on ContentSchedule isn't working properly. When you call Save, the service thinks that there's nothing to save because it didn't track the change to the schedule. So it just returns without doing anything. When you use Create rather than CreateAndSave, the call to Save knows that it definitely has work to do because newArticle hasn't been saved at all yet, and the schedule is saved correctly along with everything else.

I think the problem comes from here when the Content is cloned:

clonedContent._schedule.CollectionChanged -= ScheduleCollectionChanged;         //clear this event handler if any
clonedContent._schedule = (ContentScheduleCollection)_schedule.DeepClone();     //manually deep clone
clonedContent._schedule.CollectionChanged += clonedContent.ScheduleCollectionChanged;   //re-assign correct event handler

This appears to remove the event handler from the original ContentScheduleCollection, which is still being used by the original Content. So the clone has change tracking wired up, but the original no longer does.

@nul800sebastiaan nul800sebastiaan removed the state/needs-more-info We don't have enough information to give a good reply label Dec 20, 2019
@umbrabot
Copy link

umbrabot commented Jan 7, 2020

Hi @HefinJ,

We're writing to let you know that we've added the Up For Grabs label to your issue. We feel that this issue is ideal to flag for a community member to work on it. Once flagged here, folk looking for issues to work on will know to look at yours. Of course, please feel free work on this yourself ;-). If there are any changes to this status, we'll be sure to let you know.

For more information about issues and states, have a look at this blog post

Thanks muchly, from your friendly PR team bot :-)

@NurhakKaya
Copy link
Contributor

NurhakKaya commented Oct 17, 2020

Hi there,

I've started working on this issue.

I'm Nurhak and I'm taking some time to triage some issues on the Umbraco CMS issue tracker as part of my Hacktoberfest contribution. I will assess the issue reported, maybe ask some questions if I need more information and ultimately decide whether the issue should be progressed. If so, I'll label and if it has been fixed in a later release or is no longer relevant, I'll close, leaving a friendly message explaining why.

Best,

Hacktoberfest Contributor

@NurhakKaya
Copy link
Contributor

This is working fine with Umbraco version 8.6.0. Please see details below, I think this issue can be tested again with Umbraco 8.6.0 + and closed.

PS: CanConHack & Hacktoberfest Contribution

Issue7361-ContentScheduleContentDetailsInUmbraco
Issue7361-ContentScheduleAddSaveAndPublishCode

@emma-hq
Copy link
Contributor

emma-hq commented Mar 9, 2021

Hey folks,

The fix that @NurhakKaya provided works as expected so I'm going to go ahead and close.

Lovely work, Nurhak. Thanks!

Emma

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants