Copied to clipboard

Flag this post as spam?

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


  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Apr 29, 2014 @ 13:20
    Dan Diplo
    0

    ContentService GetContentForRelease() Not Working as Expected

    I have a requirement to get a list of content nodes that have been scheduled to publish in the future. In Umbraco 6.1.6 I created a number of content pages that have the Publish At date set to a future date and then saved these (but didn't publish them). I also created a few new nodes that again had a Publish At date in the future and again where saved (but not published).

    This should then set these pages for scheduled publication in the future.

    I then tried the following code which I believe should return content due for release:

    var service = ApplicationContext.Current.Services.ContentService;
    var dueForRelease = service.GetContentForRelease();
    

    However, dueForRelease.Count() always equals zero and the collection remains empty.

    According to the ContentService source code GetContentForRelease(), "Gets a collection of IContent objects, which has a release date less than or equal to today."

    NB. I tried restarting the app pool incase it was a caching issue, but made not difference.

    So either this method doesn't work or my understanding of it is wrong. Can anyone help clarify, please?

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Apr 29, 2014 @ 13:38
    Sebastiaan Janssen
    100

    Read that comment again and then compare it to your requirement.. ;-)

    So it's working exactly as it says in the comment, it returns objects which have a release date less than or equal to today. Which is opposite of what you need. Would be super easy to add your requirement though, just create a new method and revert the query so it will be:

    var query = Query<IContent>.Builder.Where(x => x.Published == false && x.ReleaseDate >= DateTime.Now);

    Send us a Pull Request? :)

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Apr 30, 2014 @ 11:44
    Dan Diplo
    0

    Sorry, couldn't reply earlier, internet connection problems...

    Anyway, Doh! Obviously I was so convinced the method would do what I wanted that I hadn't read it properly! My bad. Though I am curious about what the purpose of GetContentForRelease() is? Wouldn't it always return nothing?

    Anyway, based on the source code and your help I managed to come up with this quick razor test that seems to work:

    RepositoryFactory _repositoryFactory = new RepositoryFactory();
    IDatabaseUnitOfWorkProvider _uowProvider = new PetaPocoUnitOfWorkProvider();
    
    using (var repository = _repositoryFactory.CreateContentRepository(_uowProvider.GetUnitOfWork()))
    {
        var query = Query<IContent>.Builder.Where(x => x.Published == false && x.ReleaseDate >= DateTime.Now);
        var contents = repository.GetByQuery(query);
    
        <h1>Contents @contents.Count()</h1>
    
        foreach (var item in contents)
        {
            <p>@item.Id @item.Name @item.ReleaseDate</p>
        }
    }
    

    So that's pretty cool, as I never knew you could so such nicely typed queries with PetaPoco.

    Though isn't there any method to do this already? I presumably the built-in Umbraco scheduler must call some method to get events that have been scheduled to publish? Am I reinventing the wheel? 

    Cheers!

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Apr 30, 2014 @ 11:51
    Morten Christensen
    1

    The background service / scheduler that publishes Content items looks for items that has to be publish now or should have been published by the time it checks.

    Imagine you create a content item that has to be published at 12:00 tomorrow. When the scheduler looks for content it has to publish today at 12:00 it should not get a list of items to be published in the future. Instead when its tomorrow at 12:00 or 12:01 for that matter it will get that one content item that it has to Publish now.

    Makes sense?

    / Morten

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Apr 30, 2014 @ 13:17
    Dan Diplo
    0

    Ahhh, yes, that makes perfect sense. So basically the scheduler will be running at an interval in the future and then needs to look back to see all the content that should have been published in the interim. (Thank God you guys write the logic and not me :p)

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Apr 30, 2014 @ 13:24
    Dan Diplo
    0

    Just to explain a bit what I'm trying to achieve to canvas your thoughts:

    The problem I have is that we have a client who needs to schedule things to publish in the future to an exact fidelity - to within a minute (they sell event tickets that need to go on sale at, say, exactly 9am and a not a minute before or after). We've found the Umbraco scheduler not quite this reliable (which is understandable given the web context it runs is).

    So what I was thinking of doing was writing a quick controller action that would get a list of the content that needs publishing and then publish whatever needs to be done. And then creating a quick console app that calls this controller (via it's URL with a secure token) and then scheduling this using Windows Task Scheduler to run every minute (or whatever) on the server.

    • So scheduler runs every min or 30 seconds
    • Scheduler calls console app that makes an HTTP request to the controller (within Umbraco context)
    • The controller then calls GetContentForRelease() and publishes any items that come back
    Does that make sense as a workflow? Any issues you see?

     

Please Sign in or register to post replies

Write your reply to:

Draft