Copied to clipboard

Flag this post as spam?

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


  • Mila Pandurska 17 posts 86 karma points
    Oct 11, 2018 @ 11:18
    Mila Pandurska
    0

    Handle Automatically Unpublish Event

    Hi, I have nodes which has expire date. When this date comes I want to send an email notification to some people. I was thinking of handling the Umbraco.Core.Services.ContentService.UnPublished method, but this method is not called when the unpublish is done automatically.

    Can you give me idea how I can achieve my requirements?

    Regards Mila

  • Dan Diplo 1405 posts 5297 karma points MVP 2x c-trib
    Oct 11, 2018 @ 14:42
    Dan Diplo
    0

    You can get a list of content pages that are going to expire (unpublish) using the Content Service ie.

    ApplicationContext.Services.ContentService.GetContentForExpiration();
    

    So you could maybe use that?

  • Mila Pandurska 17 posts 86 karma points
    Oct 11, 2018 @ 18:59
    Mila Pandurska
    0

    Hi, Dan, I created Umbraco scheduled task and call

    var specialsToExpire = ApplicationContext.Services.ContentService.GetContentForExpiration();
    

    but specialsToExpire is always null.

  • Dan Diplo 1405 posts 5297 karma points MVP 2x c-trib
    Oct 11, 2018 @ 19:13
    Dan Diplo
    0

    According to the documentation that should give you all content that has an expire date greater than today's date. You do have some content scheduled to unpublish, don't you?

  • Mila Pandurska 17 posts 86 karma points
    Oct 11, 2018 @ 19:32
    Mila Pandurska
    0

    Hi, Dan, I checked the code of Umbraco 7.7.9 and 7.12.3 and it seems that there is discrepancies: Here is what the IContentService definition is:

    /// <summary>
            /// Gets a collection of <see cref="IContent"/> objects, which has an expiration date greater then today
            /// </summary>
            /// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
            IEnumerable<IContent> GetContentForExpiration(); 
    

    and the ContentService implementation is:

    /// <summary>
        /// Gets a collection of <see cref="IContent"/> objects, which has an expiration date less than or equal to today.
        /// </summary>
        /// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
        public IEnumerable<IContent> GetContentForExpiration()
        {
            using (var uow = UowProvider.GetUnitOfWork(readOnly: true))
            {
                var repository = RepositoryFactory.CreateContentRepository(uow);
                var query = Query<IContent>.Builder.Where(x => x.Published && x.ExpireDate <= DateTime.Now);
                return repository.GetByQuery(query);
            }
        }
    

    it seems that the method GetContentForExpiration returns nodes which expiration date is less than or equal today.

    Regards Mila

  • Dan Diplo 1405 posts 5297 karma points MVP 2x c-trib
    Oct 12, 2018 @ 07:51
    Dan Diplo
    0

    Hi. That's interesting. Could be a bug or maybe they fixed a bug?

    There is also the related method called GetContentForRelease() which is supposed to get content with a release date greater than today. Out of interest, does that work?

  • Mila Pandurska 17 posts 86 karma points
    Oct 12, 2018 @ 10:46
    Mila Pandurska
    0

    Hi, Dan, I didn't try GetContentForRelease() method because my content is Published. I don't have a case where the content should be released at some date. However I made my own request to get the content that expires today:

    var items = ApplicationContext.Services.ContentService.GetContentOfContentType(myDocTypeId).Where(sp => sp.ExpireDate.HasValue && sp.ExpireDate.Value.Date == DateTime.Now.Date && sp.HasPublishedVersion);
    

    I hope this approach is not taking too many server and db resources.

    Regarding the method GetContentForExpiration() I think there is a bug, because of this query:

    .Where(x => x.Published && x.ExpireDate <= DateTime.Now);
    

    If a node has expiration date <= now, then the item would not have status Published. This query will always return empty list:

    Thanks Mila

Please Sign in or register to post replies

Write your reply to:

Draft