Copied to clipboard

Flag this post as spam?

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


  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 26, 2010 @ 16:24
    Bo Damgaard Mortensen
    0

    Send e-mail to member before node expires

    Hi all,

    I'm currently working on an umbraco website where the content is mostly handled by the members of the site. Some of the nodes are by default set to expire (be unpublished) i.e. 14 days after the node has been made and published. I need to automatically send the member an email when there is i.e. 2 days left before the node is being unpublished so the member is able to go and refresh the nodes expiration date.

    I've looked into notifications, but there's no options for unpublishing sadly :( 

    Does anyone know how to do this? I can only imagine that it has been done before!

    Thanks in advance,

    Bo

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Nov 26, 2010 @ 16:31
    Matt Brailsford
    0

    Hi Bo,

    You'll probably want to register an event handler for the Document.AfterUnPublish method. This is usualy done by hooking up the even during application load. Check this wiki page out for more info:

    http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events

    Matt

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Nov 26, 2010 @ 16:35
    Matt Brailsford
    0

    Ooops, just realised you wanted to catch it several days before.

    Hmmm, the only thing I could think then is to create a scheduled task and check the Documents yourself using the API, and checking the UnPublish date.

    You could use Richards Task Schedular package to hit a page and check whether the nodes are due to expire, then send out an email:

    http://our.umbraco.org/projects/developer-tools/taskscheduler

    Matt

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 26, 2010 @ 16:52
    Bo Damgaard Mortensen
    0

    Hi Matt, thanks a lot! That's brilliant :)

    It seems though that the BeforeUnPublish event would be a better option for me. The only question is have now is, where am I going to place the AppBase.cs class (or the dll?)?

    Also, it says: "... automatically when umbraco loads" So basically if no one is logging into the umbraco backend before the node is set to expire/unpublish, the event will never be fired, or am I wrong on this one?

    Thanks again! 

    All the best,

    Bo

     

     

     

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Nov 26, 2010 @ 17:02
    Matt Brailsford
    0

    Hi Bo,

    You can place the class anywhere in you solution, Umbraco automatically looks for anything the extends ApplicationBase (not sure if it works in App_Code though, maybe give it a try).

    Basically Umbraco will load these at ApplicationStart and keep them in memory for as long as the app is running (app pool recycle) so as long as you have a steady stream of visitors so that the app doesn't completley go to sleep, you should be ok (failing that just use an uptime service to ping your website).

    Hope that helps.

    Matt

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 26, 2010 @ 17:31
    Bo Damgaard Mortensen
    0

    Hey Matt,

    Thanks a lot, appreciated! I think I will tinker around with the event BeforeUnPublish a bit, i'm just not sure when it will actually fire - the "Before.." seems to be an unknown factor for me ;)

    If the applicationstart fires whenever a visitor enters the site, it shouldn't be a problem keeping it alive! Else I will just point a cronjob at it.

    / Bo

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 26, 2010 @ 18:33
    Bo Damgaard Mortensen
    0

    Hmm, it dosn't seem to actually fire, the BefureUnpublish event and I don't like having control over when "before" is..

    I've thought about making a usercontrol that checks all the nodes that's got an expiration date on them and if there's i.e. 2 days left, get the related member and send them an e-mail. I would then place this usercontrol on the MasterPage so it's being called everytime someone hits the page. The expiration date will be a DateTime picker document property on the nodes.

    Any thoughts on this solution?

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Nov 26, 2010 @ 18:41
    Matt Brailsford
    0

    Hi Bo,

    Yea, the Before is quite literal, it's the split second before it's unpublished.

    Your way sounds ok if you have a steady stream of visitors, but I'm not keen on it being "hidden" in the master doc type. Personally, If I was going to "check all the nodes yourself" route, I'd setup a specific page for it so when you hit it, it runs the check, then use Richards package to actualy schedule it as you should only need it to run once a day (http://our.umbraco.org/projects/developer-tools/taskscheduler)

    Matt

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 26, 2010 @ 18:46
    Bo Damgaard Mortensen
    0

    Right, so just to clarify this: I can make a hidden page (well, hidden to the visitors) where my usercontrol will be placed on and then set Richards scheduler to run that page every day? 

    If, lets say, that no one is visiting the site for a few days (which would be highly unlikely, but i'm talking worst case now), then the scheduler will not be able to run because the application is "sleeping". Am I right about this? :)

    All the best,

    Bo

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 26, 2010 @ 22:05
    Bo Damgaard Mortensen
    0

    ... and this leads me to the following question: how do I get the datetime in C# via doc.getProperty("theDateTimeProperty") ? I have tried to cast it to a DateTime, but I get an error saying "Invalid cast"

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Nov 26, 2010 @ 22:10
    Matt Brailsford
    0

    Hey Bo,

    You should be able to get a string representation from the .Value property:

    doc.getProperty("theDateTimeProperty").Value

    You should just be able to convert that, but if not, you may need to parse it.

    Matt

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 26, 2010 @ 23:40
    Bo Damgaard Mortensen
    0

    Hmm, the struggle with dateTime is going on ;)

    What I get back from umbraco is the following format: 26/11/2010 00:00:00 and when I try to output a simple DateTime.Now in a textbox it gives me the current date and time in the same format. However, when I try to use any of the DateTime.Parse methods, I get the following error:

    String was not recognized as a valid DateTime.

     

    I honestly fail to see the logic in that :/

    What I have now is this:

     

    string expirationDate = doc.getProperty("spotPeriode").Value.ToString();
                            txtTest.Text += DateTime.Now.ToString();
                            DateTime expDate = DateTime.Parse(expirationDate);
                            DateTime now = DateTime.Now;
                            TimeSpan difference = expDate - now;
    

    Edit: I've also tried the Convert.ToDateTime(); but with the same result..

     

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 27, 2010 @ 01:52
    Bo Damgaard Mortensen
    0

    Nevermind, I figured it out :) It's incredible how ~10 minutes off from the screen can give you a boost of clarity, hehe. 

    The problem was, that I was looping through all the nodes of a specific alias and I wanted to grab the Date Time picker property from them. And there was the problem: if any of them don't have a date specified, well, then the value is empty and thus not a valid string to parse through ;)

    Solved with this simple check:

    if (!expirationDate.Equals(string.Empty))
    {
        DateTime expDate = DateTime.Parse(expirationDate);
    }
Please Sign in or register to post replies

Write your reply to:

Draft