Copied to clipboard

Flag this post as spam?

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


  • Ed Parry 64 posts 252 karma points c-trib
    May 31, 2013 @ 12:46
    Ed Parry
    0

    SaveEventHandler code only runs the first time

    Hi,

    This is a strange problem that I'm having, and I'm not sure whether it's an Umbraco issue or something more high level.

    To explain: we have a node where a back-end user uploads a file, and then ticks a box and presses save. When they press save, if the tickbox is ticked, then we should process what they uploaded. To achieve this, I call a function using the Document.AfterSafe += new Document.SaveEventHandler(MethodCall) line, and then I check that the checkbox is ticked, and go from there.

    This all works for the first time. But when a user uploads a new file a couple of days later, the code doesn't run. The only way to get it to run, is to re-copy the .dll into the "bin" folder, and then press save.

    Does this ring a bell for anyone at all? It seems as though the .dll stops listening for the SaveEventHandler after the first time.

    (Also posted on SO: http://stackoverflow.com/questions/16858367/umbraco-saveeventhandler-code-only-runs-the-first-time)

  • Comment author was deleted

    Jun 03, 2013 @ 17:41

    Which version of umbraco?  There are two event models that can be tapped into, the V4 or the V6.  The V4 is able to be used on either v4 or v6.

  • Ed Parry 64 posts 252 karma points c-trib
    Jun 03, 2013 @ 17:44
    Ed Parry
    0

    We're using Umbraco 4.7.2 in both development and production for this.

  • Comment author was deleted

    Jun 03, 2013 @ 17:46

    Ah, besides the token 'upgrade to the 4.11.9', I'm not sure I can help

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jun 05, 2013 @ 12:26
    Dave Woestenborghs
    0

    Can you post your code of your eventhandler ?

  • Ed Parry 64 posts 252 karma points c-trib
    Jun 05, 2013 @ 12:35
    Ed Parry
    0
    Here you go, does that look correct to you?
    public class SortDB : ApplicationBase {
    Document page = new Document(2484);    
    public SortDB() {
    Document.AfterSave += new Document.SaveEventHandler(GrabXmlFile);            
    }
    public void GrabXmlFile(Document sender, SaveEventArgs e) {
    // runs the logic code
    }
    }
  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jun 05, 2013 @ 12:36
    Dave Woestenborghs
    0

    Can you post some more details ?

  • Ed Parry 64 posts 252 karma points c-trib
    Jun 05, 2013 @ 12:38
    Ed Parry
    0

    Sorry, could you possibly elaborate on what you're after by "details" - more of the code, or something else?

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jun 05, 2013 @ 13:08
    Dave Woestenborghs
    0

    More code :-)

  • Ed Parry 64 posts 252 karma points c-trib
    Jun 05, 2013 @ 13:40
    Ed Parry
    0

    public class SortDB : ApplicationBase {

    Document page = new Document(2484);    


    public SortDB() {

    Document.AfterSave += new Document.SaveEventHandler(GrabXmlFile);            

    }


    public void GrabXmlFile(Document sender, SaveEventArgs e) {

    if (sender.Id == 2484) {                

    string xmlFileLocation = "C:\\inetpub\\wwwroot\\website" + page.getProperty("fileUpload").Value.ToString();

    string processFile = page.getProperty("processFile").Value.ToString();


    page.getProperty("Messages").Value += "Node saved.\n";


    if (xmlFileLocation != null && processFile == "1") {


    page.getProperty("Messages").Value += "Process started at " + ShowSystemTime() + ".\n\n";


    ProcessXml(xmlFileLocation);


    page.getProperty("Messages").Value += "XML File Collected & Processed at " + ShowSystemTime() + ".\n\n";                        

     

    page.getProperty("processFile").Value = "0";

    }

    else {

    // No file available.

    }                

    }

    else {

    // It wasn't the correct node, ignore.

    }            

    }

    }

    There you go, that's the function that is called from the event handler, which checks the sender ID to make sure it's the correct page before continuing. It all works exactly as expected the first time, but after that it fails to run at all.


  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jun 05, 2013 @ 13:45
    Dave Woestenborghs
    100

    I think the problem is that you have a global variable called page. 

    You could just do your check sender has the correct id (2484) and use sender instead of your page variable. 

     

  • Ed Parry 64 posts 252 karma points c-trib
    Jun 05, 2013 @ 14:05
    Ed Parry
    0

    Well, I've removed the global variable and used sender in its place, and so far it looks to have done the trick! That's incredible, thank you! 

    Would you mind if I asked why you thought the global variable was causing the issue here?

    Thanks!

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jun 05, 2013 @ 14:11
    Dave Woestenborghs
    1

    I am not sure. 

    But I think when you use the global variable it's cached somehow after the first call of the eventhandler. On all subsequent calls the cached version is used, and that is a different version than you actual document that is being saved. So you are updating an older version of your document. You probably can see it when you look for differences with the rollback function

Please Sign in or register to post replies

Write your reply to:

Draft