Copied to clipboard

Flag this post as spam?

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


  • Jesper Bille Olsen 2 posts 72 karma points
    Oct 22, 2019 @ 09:00
    Jesper Bille Olsen
    0

    Programmatically set unpublish date

    I am trying to programmatically set unpublish date in Umbraco 8, but i can't seem to figure out how..

    I want to do the same as answered here: https://our.umbraco.com/forum/extending-umbraco-and-using-the-api/91642-programmatically-set-unpublish-date?fbclid=IwAR0LJxj_rsZly2Kq9pTh3iTXe2a5QcLHeVEuN5esoGXQk9fplN1AMWp5bj4

    But there is no expireDate in Umbraco 8

    Hope someone can help :)

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Oct 22, 2019 @ 11:28
    Kevin Jump
    1

    Hi Jepser,

    think you have to set a content schedule

    item.ContentSchedule.Add(new ContentSchedule("*", DateTime.Now.AddDays(1), ContentScheduleAction.Expire))
    

    the tricky bit is the culture, for which I think "*" would mean all languages (it does in other places in the code base).

    you might also need to clear it to ensure you not adding extras.

    item.ContentSchedule.Clear(ContentScheduleAction.Expire);
    
  • Jesper Bille Olsen 2 posts 72 karma points
    Oct 22, 2019 @ 16:55
    Jesper Bille Olsen
    0

    Thank you Kevin, it worked ! :D

    This is my final code:

    var contentService = Services.ContentService;
    var node = contentService.GetById(item.Id);
    var contentsched = new ContentSchedule("*", DateTime.Now.AddDays(3), ContentScheduleAction.Expire);     
    node.ContentSchedule.Clear(ContentScheduleAction.Expire);
    node.ContentSchedule.Add(contentsched);
    contentService.SaveAndPublish(node);
    
  • Mortaza Nourestani 2 posts 72 karma points
    Dec 17, 2020 @ 00:31
    Mortaza Nourestani
    0

    This is not working for me

  • Allan Kent 1 post 71 karma points
    Dec 17, 2020 @ 06:51
    Allan Kent
    0

    I can confirm, it´s working here too.

  • Kristian Ravnevand 94 posts 214 karma points
    Apr 15, 2021 @ 13:02
    Kristian Ravnevand
    0

    Tnx for great solution. I had to modify it a bit to work on multiple language site:

    var csd = new ContentScheduleCollection();
    foreach (var c in node.CultureInfos)
    {
        csd.Add(new ContentSchedule(c.Culture, maxDate.AddDays(1), ContentScheduleAction.Expire));
    }
    node.ContentSchedule = csd;
    

    For some reason, the "*" didn't work

Please Sign in or register to post replies

Write your reply to:

Draft