What is the Event feature in the Vault 2012 API?
For Vault 2012, we added a feature called Web Service Command Events, which allows you to run custom code before and after certain Vault operations are executed.  For example, you can perform custom operations after a file gets checked-in.


What is the scope of my event handlers?
Your handlers will hook to any application on that computer using Autodesk.Connectivity.WebServices.dll.  Starting in Vault 2012 all Autodesk Vault clients use this DLL for communication with the server.  So you can hook to applications like Vault Explorer, AutoCAD, Inventor, Civil 3D, and so on.

Let me restate, that this feature runs client side.  So if you have 100 Vault users, your event code needs to be deployed to 100 computers in order to grab all events.


What Vault products does this feature work with?
Vault Workgroup, Vault Collaboration and Vault Professional.  It is not available in base Vault.


What operations can I receive events for?
Most events can be displayed in a grid:

  File Folder Item Change Order
Add Yes Yes Yes Yes
Delete Yes Yes Yes Yes
Reserve for editing Yes (checkout) No Yes
(edit)
Yes
(edit)
Commit Changes Yes
(checkin)
No Yes
(commit)
Yes
(commit)
Change Lifecycle Yes No Yes Yes

In addition to the ones listed in the grid, you can receive events for File Download, Item Rollback Lifecycle, and Item Promote.


What are the events I can hook to for a given operation?
Each operation fires 3 events:

  1. GetRestrictions – This part allows event handlers to block the operation.
  2. Pre – This part allows event handlers to run code before the operation takes place.
  3. Post – This part allows event handlers to run code after the operation has finished.


When and where do these events fire?
Here is the basic timeline:

If restrictions are found during the GetRestrictioins phase, the operation is blocked and never continues to the Pre.  If there are no restrictions, things continue to the Pre.  Unless something unexpected occurs, Pre, Web Service call and Post should always execute.

As you can see, the events happen around the web service call.  In other words, at the layer where the client communicates with the server. 

Keep in mind these events may or may not correlate with the UI command that the user invokes. For example, a user performs a Get command on a file in Vault Explorer.  This may trigger multiple download events if an entire assembly needs to be download.  Or it may trigger no events if the file on disk is up-to-date.


What can I do in an event handler?
Inside a GetRestrictions event you can block the operation and provide the reason for the restriction.  Inside a Pre and a Post event you run extra operations, such as sending off an email or queuing up a Job.


What can't I do in an event handler?
You can't change the parameters to or from the operation.  You can see most of the parameters passed in, but you can't change them.  Likewise you see the return result, but you can't alter it.  For example, in the Pre AddFile event you can see that the file name parameter is "Part1.dwg", your code cannot change that name to "ZX-0389.dwg".

You should also avoid UI in an event handler.  You don't know what context your handler is running in.  For example, you could be running in a command line utility or a service.

Bubbling up exceptions is not a good idea.  Again, you don't know the context you are running in, so an exception might end up shutting down the parent application.  The safest thing is to wrap each event handler in a try/catch block.

Lastly, make sure you don't create and infinite recursion of events.  For example, during a Pre AddFile event, your code adds a file, which triggers an AddFile event, which causes your code to add a file, and so on.  Even if your code works fine on its own, remember that there might be other event handlers loaded.  So your handler might run and operation that triggers another handler, which runs an operation that triggers back to your handler, and so on ad infinitum. 


If the Vault server throws an error do I get a Post event?
Yes, and you can see the Exception that is thrown.


Am I guaranteed a Post event?
Not in all cases.  It's technically possible for the operation to complete on the server, but your computer loses power right before the Post event.  However during normal operation, yes, you will always get a Post event.


Is there anyway to enforce that an Event fires?
No.  If the computer doesn't have your handler installed, or your hander fails to load for some reason, the operation will go through without hitting your code.


How do I write an event handler?
A detailed list of steps can be found in the Getting Started section of the API documentation.  The RestrictOperations sample in the SDK has sample code.

Here are the basic steps:

  1. Create your DLL project
  2. Reference Auotdesk.Connectivity.WebServices.dll
  3. Add a class that implements IWebServiceExtension
  4. In the OnLoad() implementation, hook to the events that you care about.  The hooks are on the web services objects themselves.  (ex.  DocumentService.AddFileEvents.GetRestrictions )
  5. Write the event handler code
  6. Deploy


I wrote a Vault client, will event handlers be hooking to my application?
If you use Autodesk.Connectivity.WebServices.dll to communicate with the Vault Server, yes.  So your code should handle cases where operations are blocked by other event handlers.  In your app, these restrictions will bubble up as an Exception of type Autodesk.Connectivity.Extensibility.Framework.ExtensionException.


Are my handlers specific to a version of Vault?
Yes.  For example, your Vault 2012 extension can handle events from the Vault 2012 version of Autodesk.Connectivity.WebServices.dll, but not from any other version of the DLL.


Which Web Service functions fire which events?
You can see the full mapping in the Web Service Command Event Mappings of the Knowledge Base in the Vault SDK documentation.  There are no cases where a web service function fires multiple events.


Are there other event types?
Yes, but I'll cover those in another post.

Update:  Article added for Vault Explorer Command Events


Comments

19 responses to “FAQ – Events”

  1. Mike Beebe Avatar
    Mike Beebe

    I’d like to see some code depicting where/how to deal with ExtensionException if another handler adds a restriction.

  2. Will these events now allow me to generate PDfs or DXFs prior to check in from Inventor? Thanks.

  3. Yes you can. You would need to catch the event, locate the Inventor file on disk, then generate the PDF or DXF based on the local file.
    What are you planning on doing with the generated files?

  4. The plan is to generate pdf or dxf files of Inventor drawings (IDW or DWG) upon check in and placed out on a network location. I understand that users can potentially view WIP files. I was wondering if it was possible. Thanks!

  5. That should work. I suggest generating the files after the check-in completes. That way, if there is a failure, you don’t need to waste time generating the files.
    Finding the local file on disk will be a bit tricky because all you get are Vault server objects and IDs.

  6. Hi Doug Redmond,
    I want to develop a function to modify filename by some rule when file is uploaded to Vault.
    After reading your article, I find a way.pls help me check.
    1、subscribe the DocumentService post event
    2、call CheckoutFile and CheckinFile to modify filename.
    Is these step talked above right?
    pls. correct them or tell me another way.Thank you so much!

  7. Hi Doug,
    is there a way to update properties in a CheckinFileEvents.Post EventHandler (running in Inventor)?
    If it’s not a bug in my code, it seems to me that the “native” vault plugin locks the Inventor file.
    So I removed the iProperty mappings and tried checking the file out in the EventHandler to update the properties directly via Vault API. The checkout fails with 1004…
    Is there a general “lock” during those operations?
    Would be great, if you could help me with this one :)
    Best regards
    Martin

  8. I don’t know how the native Vault plugin works. However you should be able to figure out what the error is. Error 1004 is coming from the server, and the server treats the Inventor client like any other. There is no special “lock”.
    Check that the file isn’t checked out already. It’s possible that the ‘keepCheckedOut’ setting was set to true.
    Also check that the parameters to your checkout call are valid.

  9. Hi Doug,
    is there a way to know which files are Checkin-ing from Inventor without call the Web Service? My problem is: i need to know, from an undefined assembly wich parts (or sub-assembly) the designer are saving to Vault, to read their iProperties and saving them on my mirror DB. It’s enough to get the filelist of the ChecklistFiledialog that appears before Inventor Checkin files in Vault
    Thx in advance,
    Dave

  10. I’m sorry: i forgot to tell you what i’m using: Inventor 2013 with Vault Professional

  11. No, you need to make a web service call. The check in event gives you just the ID. You need to make the web service call to get the File object.

  12. Sabuj Saha Avatar
    Sabuj Saha

    Hi Doug,
    I am wanting to do some validation before the state is changed to release. How do I get state in UpdateFileLifecycleStateEvents_GetRestrictions(object sender, UpdateFileLifeCycleStateCommandEventArgs e).
    Thanks in advance

  13. The operation has not happened yet. So you can get the From state by looking up up the File object. The File MasterID is passed in with the event args.
    The ID of the To state is a field on the event args.

  14. Joeri Avatar
    Joeri

    Hi Doug, I was wondering if there is an undo checkout event? All other events work fine, but I don’t get a trigger on undo…?

  15. At the web service level, there is no event for undo checkout. If you want to log this as a wishlist item, I suggest going to the Vault Idea Station: http://forums.autodesk.com/t5/Autodesk-Vault-IdeaStation/idb-p/2

  16. Amoolya Deeven Nuthalapati Avatar
    Amoolya Deeven Nuthalapati

    Hi Doug,
    Is there any way to checkout files from Vault Using Inventor API i.e., VBA??
    Much Appriciated for youe reply.
    Thanks,
    ADN.

  17. Sorry we do not have VBA support. The recommended approach is to write a .NET plug-in to Inventor. From there, you can interface directly with the Vault DLLs, which are also .NET.

  18. Hi Doug,
    Is there any way to get logged in user name, Vault server name, and vault database name during execution of Vault web service events?

  19. Yes, you can get that information. When the event is called, cast the “sender” Object to type IWebService. The Url property has the server name and the SecurityHeader has the UserId.
    The vault name is harder to get, but I have an article with a suggested workaround:
    http://justonesandzeros.typepad.com/blog/2011/11/getting-the-vault-name-in-an-event.html

Leave a Reply to JiyuanCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading