I've created an aftersave handler for my media items. This handler takes the uploaded file and sends it to Amazon S3. Once the upload to S3 is done, I want to save the URL to the remote file in my media item.
This has been some-what addressed in v4.1 (but due to overall architectual limitations it's not possible to have it 100% solved).
I've done sites where I've re-called the Save method during a Save event handler, the only way you can work around it is to have a property somewhere that you set a flag which indicates that you've already called the event handler.
Thank you very much for the quick answer, saved me writing code that I don't need.
Just to explain: setting the property will save it to the database, but I still did not see the url in my media item. However, after doing the XmlGenerate, it does show up.
Yeah the reason that the database write happens then is when people create their own DataType it's the easiest way in which to ensure that some form of "save" happens.
I'll admit that I'm not a huge fan of doing it this way, I'd much prefer each DataType to have an exposed Save method which is called, but that requires implementing breaking changes to the DataType concept which would be very bad for all of us.
4.1 will have some improvements around this though :) (I'll blog about them soon)
.Save() in an AfterSave event handler?
I've created an aftersave handler for my media items. This handler takes the uploaded file and sends it to Amazon S3. Once the upload to S3 is done, I want to save the URL to the remote file in my media item.
However, once I set the property:
You see the problem here is obvious, after the .Save(), the aftersave event is being executed again, making this an infinite loop.
Has anybody else run into this problem? What can be done to fix it?
Just so you know the property setter actually does the saving (see this post: http://www.aaron-powell.com/blog/july-2009/the-great-umbraco-api-misconception.aspx for an explanation).
This has been some-what addressed in v4.1 (but due to overall architectual limitations it's not possible to have it 100% solved).
I've done sites where I've re-called the Save method during a Save event handler, the only way you can work around it is to have a property somewhere that you set a flag which indicates that you've already called the event handler.
Errr.. Wait a minute, setting the property saves it? Not in my experience, I had to explicitly call the Save() method.
However, I should test this more thoroughly maybe.
So let me get this right: setting the property does NOT fire the event handler??
Also, I can still safely call the media.XmlGenerate method without any events firing off?
Thank you very much for the quick answer, saved me writing code that I don't need.
Just to explain: setting the property will save it to the database, but I still did not see the url in my media item. However, after doing the XmlGenerate, it does show up.
Yeah the reason that the database write happens then is when people create their own DataType it's the easiest way in which to ensure that some form of "save" happens.
I'll admit that I'm not a huge fan of doing it this way, I'd much prefer each DataType to have an exposed Save method which is called, but that requires implementing breaking changes to the DataType concept which would be very bad for all of us.
4.1 will have some improvements around this though :) (I'll blog about them soon)
is working on a reply...