none
Validation on Work Item save workaround

    Question

  • In WorkItemChangedEvent handler I change some values on Work Item and then I save it.

    Let's say I have one field on my Work Item called F1 and it has rule validation AllowedValues --> only numbers 0 and 1 are allowed to be saved in that field.

    Is it possible to bypass this validation in handler code and save number 5 in that field?

    I don't want to remove AllowedValue rule on that filed in TFS, I would like to remove the rule in code just before saving and then reapply it again. Is it possible?

    Friday, May 10, 2013 11:48 AM

Answers

  • Hi,

    No, there is not.

    Maybe it is an option for you to add another AllowedValues entry for the field where you restrict access using FOR/NOT? Then you need to run your custom component under a different user which is member of the group specified in the "FOR". E.g.:

    <ALLOWEDVALUES>
       <LISTITEM value="0" />
       <LISTITEM value="1" />
    </ALLOWEDVALUES>
    <ALLOWEDVALUES for="CustomComponentGroup">
       <LISTITEM value="5" />
    </ALLOWEDVALUES>

    Please refer to http://msdn.microsoft.com/en-us/library/vstudio/ms194947(v=vs.100).aspx for documentation on this.

    Kind regards,

    Fokko


    ALM/TFS Consultant, Software Developer C#.NET, SQL Server specialist at Delta-N BV (http://www.delta-n.nl)



    • Edited by Fokko Veegens Friday, May 10, 2013 1:57 PM Added sample code
    • Marked as answer by SPManiac Friday, May 10, 2013 2:51 PM
    Friday, May 10, 2013 1:53 PM

All replies

  • Hi,

    It depends if you are using TFS 2012 or TFS 2010. In 2012, the WorkItemStore object has a property "BypassRules" which can be set to true. If you set this property, it shouldn't validate your AllowedValues rule, but after that, if the user opens the Work Item, he/she cannot save it, until it gets into a valid state.

    Please refer to:

    http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.workitemtracking.client.workitemstore.bypassrules.aspx

    Kind regards,

    Fokko


    ALM/TFS Consultant, Software Developer C#.NET, SQL Server specialist at Delta-N BV (http://www.delta-n.nl)

    Friday, May 10, 2013 12:47 PM
  • Thanks for quick answer.

    I'm using 2010. There is no such option in 2010?

    Friday, May 10, 2013 1:42 PM
  • Hi,

    No, there is not.

    Maybe it is an option for you to add another AllowedValues entry for the field where you restrict access using FOR/NOT? Then you need to run your custom component under a different user which is member of the group specified in the "FOR". E.g.:

    <ALLOWEDVALUES>
       <LISTITEM value="0" />
       <LISTITEM value="1" />
    </ALLOWEDVALUES>
    <ALLOWEDVALUES for="CustomComponentGroup">
       <LISTITEM value="5" />
    </ALLOWEDVALUES>

    Please refer to http://msdn.microsoft.com/en-us/library/vstudio/ms194947(v=vs.100).aspx for documentation on this.

    Kind regards,

    Fokko


    ALM/TFS Consultant, Software Developer C#.NET, SQL Server specialist at Delta-N BV (http://www.delta-n.nl)



    • Edited by Fokko Veegens Friday, May 10, 2013 1:57 PM Added sample code
    • Marked as answer by SPManiac Friday, May 10, 2013 2:51 PM
    Friday, May 10, 2013 1:53 PM
  • Hm, I've found this:

    http://stackoverflow.com/questions/11772314/edit-tfs-work-item-field-when-marked-as-read-only-using-tfs-api

    Maybe it can be accessed in this way through XML?

    Will try your solution as well. Thanks a lot.

    Friday, May 10, 2013 2:01 PM
  • Hi,

    I would not recommend the above mentioned solution. Although it may work, it is not supported by Microsoft to make modifications to TFS by calling web services directly.

    When a new version of TFS is released, the code may not work anymore. On the contrary, TFS API calls will generally work in newer versions as well (although you may need to update your references) and are supported by Microsoft (in case you run into trouble...)

    Kind regards,

    Fokko


    ALM/TFS Consultant, Software Developer C#.NET, SQL Server specialist at Delta-N BV (http://www.delta-n.nl)

    Friday, May 10, 2013 2:09 PM