The Tridion Event System is used to automatically perform actions for us when an event is ‘triggered’, such as saving a Component or Publishing a page. While programming our Event System solutions we sometimes need to specify the name of a Schema, WebDAV URL path of a Component, or (gasp) a Tridion URI. We don’t want this in our code and it’s better to have it in a config file. However, the Event System is a ‘Class’ project type, and we can’t simply put an app.config file in the folder and expect it to work. In this article I’ll show a small code sample that will allow you to read values from a config file.

Background

This original code comes from the Mihai’s Google Code solution here: https://code.google.com/p/yet-another-tridion-blog/source/browse/trunk/Yet+Another+Event+System/Com.Mitza.YAES/ConfigurationManager.cs

However, in Mihai’s article the code is fairly complex and has lots of functionality I didn’t need, and instead I prefer a more simple approach for getting the config file and getting a key. So, I’ve give the code a haircut and present to you just the bits you need to store the config values outside the Event System.

Setup:

1. Create a config file and add your appSettings:

2. Name the configuration file the same as the Event System DLL with ‘.config’ at the end. Put the config file in the same place as the DLL. This might be in the Debug folder on your Dev system or in the Bin folder on the Tridion server. For example, TridionEventSystem.dll.config

Code:

Use the GetAppSetting(keyName) method to get your app.config variables.

Caching

If in your Event System code you use static variables they will be “cached” and stay in Scope as long as the Event System is running. If you restart the TCM Service Host and TCM Publisher services then the static variables will be refreshed.

I also use the .Net ObjectCache in the code to cache the values and this would be helpful in case your variables are not static in the Event System class.

External Libraries

Finally, a small note, but the Tridion Event System likes to have 1 DLL, so if you reference any external libraries, or if you create many .Net projects with the solution (that produce their own DLLs) then you will need to put those DLLs in the GAC or use ILMerge to combine them. So, in that case, when possible, use 1 .Net project for the Event System and minimize the abstraction of the layers into their own .Net projects, as is often done with .Net solutions. For example, I created a ‘Model’ .Net project for the Models to share between the Event System and another project, but in the end this proved to be too much of a headache and instead I moved the Models into the Event System project.

Happy healthy hacking!

Uploading images and PDF files into Tridion is easy, but sometimes we want to restrict the size allowed for these uploads. For example, it’s not cool to upload a 5MB image to the homepage of our website. In this article I will present an approach using the Event System to give a warning message to users when the filesize is too big. The filesize is configured in the Publication Metadata and also a default one is set in the code.

Solution Overview

1. Capture the Save Event for a Component
2. If it is a Multimedia Component and an ‘Interesting Mime Type’ then continue
3. Try to get the Allowed Filesize for the items from the Publication Metadata. If not found, then use the defaults defined in the MaxFilesizeDef.cs file
4. If the file is too big, then throw a new WrongFileSizeException. For example, “Sorry, file exceeds the allowed size for PDF files. The allowed size is 10MB.”

The Code: https://github.com/rcurlette/MMFileSizeValidator#tridionimagesizechecker

Setting up the Publication Metadata Configuration

Create a new Metadata Schema or add the following fields to your existing Metadata Schema. This is not mandatory, but if you want to change the default filesize values, then it is recommended to do this. It also allows you to set very high values (or low values) for 1 Publication.

1. Create a new Schema, Configuration.
Fields:
– maxImageSize, Text
– maxPdfSize, Text
– maxMovieSize, Text
*These fields must be named like this

2. Create a Metadata Schema with 1 ComponentLink field, ‘Configuration’, linking to the Configuration Schema we created above in step 1

3. Add Configuration Schema to Publication and fill in values.

Configuring the Default Filesizes

The valdiation is enabled for all Publications. If the Metadata is set, it uses those values. However, if no Metadata configuration setting is found on the Publication then the defaults from the code are used. Here are the defaults:

  • Image: long _maxImageSize = 4194304; (4 MB)
  • PDF: long _maxPdfSize = 20971520; (20 MB)
  • Movie: long _maxMovieSize = 41943040; (40 MB)

Configuring the Cache Settings and Configuration Manager

The Configuration Manager class from Mihai comes with a nice XML configuration. See below for it. The filename I have is EventSystemComponentSave.dll.config. This must be the same as your Event System DLL.

<configuration>
  <appSettings>
    <!-- Amount of minutes to keep a loaded Configuration dictionary in the cache -->
    <add key="SystemComponentCacheMinutes" value="1"/>

    <!-- Where to read the System Component from (Publication, Current, TcmUri or WebDavUrl). Multiple values are comma separated -->
    <add key="SystemComponentLocation" value="Publication"/>

    <!-- Which metadata field contains a Component Link to the SystemComponent. Multiple fields separated by commas -->
    <add key="SystemComponentField" value="Configuration"/>

    <!-- Search for System Component recursively (going up into the parent Folder/SG)? -->
    <add key="SystemComponentRecursive" value="false"/>

    <!-- When merging several System Components, should it override values for the same key? -->
    <add key="SystemComponentOverride" value="false"/>
  </appSettings>
</configuration>

Installing

This is the same to install any Event System DLL on your Tridion 2009 / 2011 instance.
1. Compile the assembly (Add References to the Tridion\bin\client\Tridion.ContentManager.dll and Tridion\bin\client\Tridion.Common.dll )
2. Copy EventSystemComponentSave.dll to the Tridion\bin folder (or another folder of your choice).
3.  Copy the  EventSystemComponentSave.dll.config to the Tridion\bin folder (or another folder of your choice).

4. Register it in the Tridion.ContentManager.config

<add assemblyFileName="C:\Program Files (x86)\Tridion\bin\EventSystemComponentSave.dll" />

5. Open the Services.msc and restart the ‘Tridion Content Manager Service Host’ service.
6. Test by trying to upload a large Multimedia file

Credits and Thanks

I used Dominic Cronin’s excellent ‘Image Size Checker’ (height/width) Event System code for a starting point. https://code.google.com/p/tridion-practice/source/browse/#git%2FImageSizeChecker

I used Mihai’s great ConfigurationManager code to store and retrieve the Publication Metadata settings. https://code.google.com/p/yet-another-tridion-blog/source/browse/trunk/Yet+Another+Event+System/

Summary

Adding content validation rules to the Event System is easy and also provides a better QA for the quality of content being uploaded to our website. In this example we saw how we can easily restrict the filesizes of Multimedia items being uploaded into Tridion. We also saw how easy it is to use a Configuration schema in our code and use the Configuration Manager to cache these settings. I hope this article gives you some new ideas and helps you keep the file sizes under reasonable limits in your Tridion system.