Search code examples
visual-studiotfstfs-workitem

How can I restrict a transition in the workflow to only the user that created the work item in TFS?


This is part of a larger restriction, but the part that is tripping me up is being able to allow only the user that created a work item (the value of the "System.CreatedBy" field) to transition a work item to the Closed state. I am aware of how to restrict the transition using the "For" and "Not" clauses, but those are limited to groups. I want to restrict it to the specific creator of THIS work item. VALIDUSERS is also limited to groups (either TFS or AD). Thanks for your help.


Solution

  • I was able to find a suitable solution last night.

    This solution actually works perfectly for my need as it allows me to add a group as exempt from the rule so that members of the group, say QA, as well as the Creator are able to close the work item, while other members of the team are not.

    Reference: here (web archive link)

    As referenced:

    1. Create ClosedByValidation field and add the following rules
    <FIELD name="Closed By Validation" refname="Demo.ClosedByValidation" type="String">
        <COPY from="currentuser" /> 
        <FROZEN not="[project]\Project Administrators"/> 
    </FIELD>
    
    1. Add the following rules to Closed state
    <STATE value="Closed"> 
       <FIELDS> 
          <FIELD refname="Demo.ClosedByValidation"> 
              <COPY from="currentuser" /> 
           </FIELD> 
       </FIELDS> 
    </STATE>
    
    1. Add the ClosedByValidation field to the form, so it looks like this. Note how I’ve displayed both the “Created By” field and the “ClosedByValidation” field

    How it works

    • The ClosedByValidation field copies the “Created By” value into itself right when the work item is created.
    • It then immediately freezes the field (with the FROZEN) rule, which states that it cannot change.
      • NOTE: The FROZEN rule is conditioned to NOT apply to project administrators, giving them an override capability.
    • When the work item is Closed, then the current user is copied into the ClosedByValidation field.
    • If the ClosedByValidation’s value remains the same (the original Created By) then all is well.
    • If the ClosedByValidation’s value has changed, then the FROZEN rule displays a violation as you see in the screenshot above.