Working with event-handlers

Sometimes its needed to add some custom functionality to a simple Vault event like a checkIn of a file or a commit of an item. Since Vault 2012 this can be realized by creating an event handler. The way of how to create an event-handler is described in the Vault API reference (see the topic: How to handle events). An event-handler will be used from every application that is using a vault-connection (VaultExplorer, Inventor, AutoCad, Microsoft Office). Every application will load your event-handler assembly and run the same events, when they gets fired.

When working with event-handlers I quickly faced two issues:

1) The event-handler code should run in every application, but not in the JobProcessor:

Because all the applications with a Vault-connection are loading the event-handler assembly, the quickest solution is two handle the application in your OnLoad function like in the example. The JopProcessor.exe will load the event-handler assembly, but he will not fire the events.

public void OnLoad()
{
  if (!System.Environment.GetCommandLineArgs()[0].Contain("JobProcessor.exe"))
  {
    var eventhandl = new EventHandler<CheckinFileCommandEventArgs>(CheckinFileEvents_Post);
    DocumentService.CheckinFileEvents.Post += eventhandl;
  }
}

2) How to get the Vault-Connection in the event-functions:

In the event-handler class you need also the WebServiceManager object in order to talk  with the Vault server. For performance reason I decided to put the WebServiceManager as a static resource. And when you have the object, you will face an other problem later: after some time your WebserviceManager will not work any more, because the server is changing the ticket (see the Vault API docu for this). But there is a quick solution also for this:

static private WebServiceManager _webSvcMgr = null;
void CommitItemEvents_addJob(object sender, CommitItemCommandEventArgs e)
{
  SetWebSrvManager(sender);
  Item[] items = e.ReturnValue;
  foreach (Item item in items)
    ...
}

To get the WebServiceManager we can use the sender-object. I created a SetWebSrvManager-function for this. The _webSvcMgr object will be set only the first time, or if the ticket or the userId gets changed.

private void SetWebSrvManager(Object sender)
{
  var webSrvCreds = (new WebServiceCredentials(sender as IWebService));
  if (_webSvcMgr == null || _webSvcMgr.SecurityService.SecurityHeader.UserId != (webSrvCreds.SecurityHeaderInfo.UserId) ||
   _webSvcMgr.SecurityService.SecurityHeader.Ticket != (webSrvCreds.SecurityHeaderInfo.Ticket))
  {
    _webSvcMgr = new WebServiceManager(webSrvCreds);
  }
}

I hope this might help some of you…

MW

About weiss92

Software Developer
This entry was posted in Vault API. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s