Event Handler Manager (.NET)
You can add custom behavior to an object type by attaching event handlers to it. For example, you might attach an event handler that performs a specific action when a user makes an update to an object and then attempts to save it. For more information, see Develop object type event handlers.
The Event Handler Manager service contains methods for programmatically attaching event handlers to an object type, and for detaching them. It also provides helper methods that you can use for the following purposes:
- To retrieve a list of event handlers in a workspace, which could be attached to a specific object type.
- To retrieve a list of event handlers currently attached to an object type.
You can also use the Event Handler Manager through the REST API. For more information, see Event Handler Manager (REST).
Fundamentals for managing object event handlers
Click the following drop-down links to learn about the methods and classes used by the Event Handler Manager.
Attach an event handler to an object type
To attach an event handler to an object type, call the AttachAsync() method by passing the Artifact IDs of a workspace and an object type, and the ID of the event handler.
Note: In the following code sample, the eventHandlerId parameter is an identifier assigned by Relativity to the event handler. This identifier isn't the artifact ID for the event handler. The GetAttachedAsync() and GetAvailableEventHandlersAsync() methods return an EventHandlerResponse object, which has this ID property.
public static async Task Attach_Async()
{
int workspaceId = 1018486;
int objectTypeArtifactId = 1035231;
int eventHandlerId = 1982384;
using (Services.Interfaces.EventHandler.IEventHandlerManager eventHandlerManager = serviceFactory.CreateProxy<Services.Interfaces.EventHandler.IEventHandlerManager>())
{
try
{
await eventHandlerManager.AttachAsync(workspaceId, objectTypeArtifactId, eventHandlerId);
}
catch (Exception ex)
{
Console.WriteLine(string.Format("An error occurred: {0}", ex.Message));
}
}
}
Detach an event handler to an object type
To detach an event handler from an object type, call the DetachAsync() method by passing the Artifact IDs of a workspace and an object type, and the ID of the event handler.
Note: In the following code sample, the eventHandlerId parameter is an identifier assigned by Relativity to the event handler. This identifier isn't the Artifact ID for the event handler. The GetAttachedAsync() and GetAvailableEventHandlersAsync() methods return an EventHandlerResponse object, which has this ID property.
public static async Task Detach_Async()
{
int workspaceId = 1018486;
int objectTypeArtifactId = 1035231;
int eventHandlerId = 1982384;
using (Services.Interfaces.EventHandler.IEventHandlerManager eventHandlerManager = serviceFactory.CreateProxy<Services.Interfaces.EventHandler.IEventHandlerManager>())
{
try
{
await eventHandlerManager.DetachAsync(workspaceId, objectTypeArtifactId, eventHandlerId);
}
catch (Exception ex)
{
Console.WriteLine(string.Format("An error occurred: {0}", ex.Message));
}
}
}
Retrieve event handlers attached to an object type
To retrieve the event handlers attached to an object type, call the GetAttachedAsync() method by passing the Artifact IDs of a workspace and an object type. If no event handlers are available for the object type, this method returns an empty list.
public static async Task GetAttached_Async()
{
int workspaceId = 1018486;
int objectTypeArtifactId = 1035231;
using (Services.Interfaces.EventHandler.IEventHandlerManager eventHandlerManager = serviceFactory.CreateProxy<Services.Interfaces.EventHandler.IEventHandlerManager>())
{
try
{
List<EventHandlerResponse> response = await eventHandlerManager.GetAttachedAsync(workspaceId, objectTypeArtifactId);
foreach (EventHandlerResponse eventHandler in response)
{
string info = string.Format("Read Event Handler {0} with Artifact ID {1}", eventHandler.ClassName, eventHandler.ArtifactID);
Console.Write(info);
}
}
catch (Exception ex)
{
Console.WriteLine(string.Format("An error occurred: {0}", ex.Message));
}
}
}
Retrieve available event handlers for an object type
You can retrieve a list of event handlers in a workspace that are compatible with a specific object type.
The following code sample illustrates how to call the GetAvailableEventHandlersAsync() method by passing the Artifact IDs of a workspace and an object type. If no event handlers are available for the object type, this method returns an empty list.
public static async Task GetAvailableEventHandlers_Async()
{
int workspaceId = 1018486;
int objectTypeArtifactId = 1035231;
using (Services.Interfaces.EventHandler.IEventHandlerManager eventHandlerManager = serviceFactory.CreateProxy<Services.Interfaces.EventHandler.IEventHandlerManager>())
{
try
{
List<EventHandlerResponse> response = await eventHandlerManager.GetAvailableEventHandlersAsync(workspaceId, objectTypeArtifactId);
foreach (EventHandlerResponse eventHandler in response)
{
string info = string.Format("Read Event Handler {0} with Artifact ID {1}", eventHandler.ClassName, eventHandler.ArtifactID);
Console.Write(info);
}
}
catch (Exception ex)
{
Console.WriteLine(string.Format("An error occurred: {0}", ex.Message));
}
}
}