Jameel
Jameel

Reputation: 328

Set TFS new workItem state programmatically

I'm trying to set tfs new work item state value to "approved" and getting an error.

i'm validating before i actually save my work item, however the item is not getting because of the errors. The error is because i'm setting a new work item state directly to "Approved" where as it allows only one possible value "Draft". Unfortunately it's my requirement as i'm migrating my work items from a different source which is already in a "Approved" state.

(TF237124: Work Item is not ready to save). Fields: The 'State' field has the status InvalidListValue

tfsWorkItem.Fields["System.State"].Value = "Approved";
                var invalidFields = tfsWorkItem.Validate();
                if (null == invalidFields || 0 == invalidFields.Count)
                {                        
                    tfsWorkItem.Save(); //Never comes here because of invalidFields
                }

Thanks in advance, Jameel

Upvotes: 0

Views: 1310

Answers (4)

Eddie Chen - MSFT
Eddie Chen - MSFT

Reputation: 29966

You can add your account into "Project Collection Service Accounts" and then enable bypass rule which allow you to save and change work item values without obeying the work item rules. Refer to this question for details: How to change workflow state of the newly created TFS work item through API?.

Upvotes: 0

nschonni
nschonni

Reputation: 4129

Look at the Work Item Template definition XML. In the TRASITIONS section, only the transion that has from="" is the valid starting state (the value in the to="Some state"). There can only be one valid starting state.

Upvotes: 0

Ryan Peters
Ryan Peters

Reputation: 180

Original: https://stackoverflow.com/a/8359044/4846465

You are validating the work item before you are changing it's state. Transitioning to a new state can cause Work Item Template actions/rules to be processed. These could be changing the values of some of your fields and/or adding new rules to the fields which would cause the previously valid data to be invalid.

Moving from an Open state to a Closed state might require someone to complete a "Review" field (for example) - if it's empty it cannot transission.

Try validating after the State change and see if there are any failures.

Upvotes: 0

Freddy Ayala
Freddy Ayala

Reputation: 49

You have to validate the WorkItem prior to save.

The validate() method will return an arraylist of invalid fields.

ArrayList result= wi.Validate();

Upvotes: 0

Related Questions