Updated media property with BeforeSave event not displaying
Hey guys,
I have a simple task to populate the value of a label property on a media item via the BeforeSave event. I have this bit working fine but the frustrating part is, the property does not display the value until you refresh the media item. i.e. you need to click on the media item link in the tree again.
Here is the code if anyone wants to try! If there is no solution might make a simple 'Auto-label' package, that would extend the Label datatype but allow a dev to enter text mixed with dynamic property data, which would be used to populate the property when a certain event was triggered (a bit like Standard Values I guess, but this cannot be used for Media Types AFAIK) In my case I need to publish a specific download URL for media items of a certain type.
//Derive the class from umbraco.BusinessLogic.ApplicationBase
public class CustomEvents : umbraco.BusinessLogic.ApplicationBase
{
//Constructor - wire up the custom events by creating new Umbraco event handlers
public CustomEvents()
{
//Document.BeforePublish += new Document.PublishEventHandler(Document_BeforePublish);
Media.BeforeSave += new Media.SaveEventHandler(Media_BeforeSave);
Media.New += new Media.NewEventHandler(Media_New);
}
private void Media_BeforeSave(Media sender, SaveEventArgs e)
{
try
{
if (sender.ContentType.Alias == "DownloadFile")
{
//Get the download Url property
string downloadUrl = sender.getProperty("downloadUrl").Value.ToString();
if (string.IsNullOrEmpty(downloadUrl) || downloadUrl.Contains("not published"))
{
// Set label with the direct download URL value
downloadUrl = string.Format("http://{0}/download/{1}/", HttpContext.Current.Request.Url.Host, sender.Id.ToString());
string propertyValue = string.Format("<a href=\"{0}\" target=\"_blank\">{0}</a>", downloadUrl);
sender.getProperty("downloadUrl").Value = propertyValue;
}
Log.Add(LogTypes.Custom, sender.Id, "The media item " + sender.Text + " download URL was set to " + downloadUrl);
}
}
catch (Exception ex)
{
Log.Add(LogTypes.Error, sender.Id, "The media item " + sender.Text + " has caused a AfterSave event error: " + ex);
}
}
private void Media_New(Media sender, NewEventArgs e)
{
try
{
if (sender.ContentType.Alias == "DownloadFile")
{
sender.getProperty("downloadUrl").Value = "This item is not published";
}
}
catch (Exception ex)
{
Log.Add(LogTypes.Error, sender.Id, "The media item " + sender.Text + " has caused a New event error: " + ex);
}
}
}
Updated media property with BeforeSave event not displaying
Hey guys,
I have a simple task to populate the value of a label property on a media item via the BeforeSave event. I have this bit working fine but the frustrating part is, the property does not display the value until you refresh the media item. i.e. you need to click on the media item link in the tree again.
I'd like to solve this usability issue, I read this topic which seems to be the sasme issue http://our.umbraco.org/forum/developers/api-questions/6778-BeforeSave-forcing-property-values-to-be-displayed but was thinking maybe there was a less hacky way to make it work? If not I will propose it on the issue tracker.
Here is the code if anyone wants to try! If there is no solution might make a simple 'Auto-label' package, that would extend the Label datatype but allow a dev to enter text mixed with dynamic property data, which would be used to populate the property when a certain event was triggered (a bit like Standard Values I guess, but this cannot be used for Media Types AFAIK) In my case I need to publish a specific download URL for media items of a certain type.
is working on a reply...