I have created an Event handler on Component save which checks the Image Size for Multimedia Components on saving. I want to read the allowed Maximum size from the Tridion configuration Component, I do not want to use Core Service for some reasons, can I read the Component in my Event System dll without using Core Service?
4 Answers
When working with the Event System, you shouldn't even use Core Service in the first place, but instead TOM.NET.
Additionally, the Component is directly available to you in the event handler, it is one of the arguments passed in the method, so no need to read it explicitly (except in some special cases).
private void ComponentSave(Component component, SaveEventArgs args, EventPhases phases)
{
string title = component.Title;
}
EDIT
The question is now completely different than before, but the main point from the original answer still stands. Do not use Core Service, but instead TOM.NET.
You need the Session object to read additional Components from the system, and you can always obtain it from the subject of the event handler. In the example above it would be:
Session session = component.Session;
Once you have the session, you need to use the GetObject
method;
Component configComponent = (Component)session.GetObject(<config component's ID or webdav>);
-
Thanks @atila for your comments, i want to read a configuration Component not the current component. I want to rescrict users to add high size images, which is configured in some other component, so for each component save i want to read this saperate configuration, even if there is another way to achieve this then its ok.. Feb 17, 2020 at 11:18
-
Use the WebDAV url. And lock the config components in a folder with the relevant access. Feb 18, 2020 at 8:42
As Atila, mentioned using core service in event system is never recommended but instead TOM.NET and to get the component "configComponent" in event system you can take the reference of the below code. Dont forget to add cache, if your config component is not updating very frequently.
private readonly Session _session;
private const string compWEBDEVURL = "/webdav/200 Global Content/Building Blocks/System/Developer/Configuration/globalconfig.xml";
public CachedComponents(Session session)
{
_session = session;
}
private static ObjectCache Cache => MemoryCache.Default;
private static Component GetConfigCompfromCache(string webDav)
{
webDav = HttpUtility.UrlPathEncode(webDav);
return Cache.Contains(webDav) ? Cache.GetConfigCompfromCache(webDav) as Component : null;
}
private Component GetConfigCompfromCache(string webDav)
{
var component = GetConfigCompfromCache(webDav);
if (component == null)
{
component = _session.GetObject(HttpUtility.UrlPathEncode(webDav)) as Component;
PushIntoCache(component);
}
return component;
}
private static void PushIntoCache(Component component)
{
Cache.Add(component.WebDavUrl, component,
new CacheItemPolicy { SlidingExpiration = new TimeSpan(0, 30, 0) });
}
In addition to the configuration Component approach, consider custom configuration settings for your add-on (a set of one or more product extensions or customizations).
See the SDL Documentation center for background on creating and uploading such custom Configuration.
For example, you might add something like this to note the maximum size of binaries in a system.
{
"configuration": {
"TCMExtension": {
"maximumSize": "1024"
}
}
}
Or if you still prefer to manage such values in a configuration Component, you might set the WebDAV URL or another way to find the item in the Content Manager.
-
Thanks @alvin your comments, is these add-ons are available in SDL Web 8 ? Feb 18, 2020 at 8:47
-
1The Add-On feature is available as of SDL Tridion Sites 9.1. Before that, configuration Components are (and still are) a good approach. You could also look into using the Content Manager configuration itself to store custom values, though that's not as portable and easy-to-maintain. Feb 18, 2020 at 12:18
As Alvin mentioned the add-on configuration approach, here is an example code on how to read the add-on configuration in the event system.
When you extend TcmExtension you have a member property called 'AddonConfiguration' which you can use. Here is one example:
private IConfigurationSection GetExtensionConfiguration()
{
if (AddonConfiguration != null)
{
var configurationRoot = AddonConfiguration.GetSection("configuration");
if (configurationRoot != null)
{
var extensionConfig = configurationRoot.GetSection("MyCoolEventHandler");
return extensionConfig;
}
}
return null;
}
You could also call this one line code example to read the config value:
var str = AddonConfiguration != null ? AddonConfiguration.GetSection("configuration").GetSection("MyCoolEventHandler").GetSection("param1").Value : "NoConfigDetected"
In this case, the configuration JSON looks like the following:
{
"configuration": {
"MyCoolEventHandler": {
"param1": "VALUE1",
"param2": 1234
}
}
}