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.
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.
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
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
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.
We're using Umbraco 4.7.2 in both development and production for this.
Comment author was deleted
Ah, besides the token 'upgrade to the 4.11.9', I'm not sure I can help
Can you post your code of your eventhandler ?
Can you post some more details ?
Sorry, could you possibly elaborate on what you're after by "details" - more of the code, or something else?
More code :-)
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.
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.
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!
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
is working on a reply...