Validating TFS 2017 Workitem using TeamFoundationRequestFilter

One of the ways to enforce workitem validation is creating a Team Foundation Request Filter, In my opinion you should always try your best to enforce your rules using workitem template customization, however in case there is some business logic that you need to validate whenever a workitem is edited or added, then one of your options would be to create a Team Foundation Request Filter.

Team Foundation Request Filter lets you intercept TFS Requests to add your own logic to every request. you do that by Implementing:

The method we are interested in here is the BeginRequest, using this method we can stop the request by throwing a RequestFilterException.

TFS Clients (Web portal,VS,VSMAc, etc ) use TFS Restful APIs to communicate with the server, when you Add/Edit a workitem from the portal or from any where, you will be actually making a request with the following URL:

“/tfs/DefaultCollection/_api/_wit/updateWorkItems”

and a bunch of json objects, What we need to do here is to first check if the request is calling the mentioned url , and then parse the json and put our logic, here we are simply checking if Priority is bigger than 2 and through an Request Filter Exception with our message