Created
June 22, 2014 17:33
-
-
Save jarnohenneman/bcf04cfb6b79241b1c22 to your computer and use it in GitHub Desktop.
A simple event extension on the component save initiated, part of blogpost: http://jarnohenneman.com/2014/06/BinaryUpload-event/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Tridion.ContentManager.ContentManagement; | |
using Tridion.ContentManager.ContentManagement.Fields; | |
using Tridion.ContentManager.Extensibility; | |
using Tridion.ContentManager.Extensibility.Events; | |
using Tridion.Logging; | |
namespace Trial.and.Error.BinaryUploadEvent | |
{ | |
[TcmExtension("BinaryUploadEvent")] | |
public class BinaryUpload : TcmExtension | |
{ | |
public BinaryUpload() | |
{ | |
Subscribe(); | |
} | |
public void Subscribe() | |
{ | |
EventSystem.Subscribe<Component, SaveEventArgs>(OnBinaryUpload, EventPhases.Initiated); | |
} | |
private static void OnBinaryUpload(Component comp, SaveEventArgs args, EventPhases phase) | |
{ | |
//Logger.Write("[Events.ComponentSave]", "ComponentSave has been called for " + comp.Title, LoggingCategory.General); | |
String schema = "Multimedia"; | |
String field = "altText"; | |
if (comp.ComponentType == ComponentType.Multimedia) | |
{ | |
if (comp.Schema.ToString().Equals(schema) && comp.Metadata != null) //[1] | |
{ | |
ItemFields metaFields = new ItemFields(comp.Metadata, comp.MetadataSchema); | |
if (metaFields.Contains(field) && String.IsNullOrEmpty(metaFields[field].ToString())) //[2] | |
{ | |
TextField altText = metaFields[field] as TextField; | |
altText.Value = comp.Title; | |
comp.Metadata = metaFields.ToXml(); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment