i validate a Workitem where I changed the state to an allowed Value. Still it says the value is wrong.
Set Values and function call:
internal void closeWI(int tfsWIID)
{
Log.logger.Info("Closing Item in TFS with ID: " + tfsWIID);
Dictionary<string, object> valuesToUpdate = new Dictionary<string, object>();
valuesToUpdate.Add("System.State", "Closed");
valuesToUpdate.Add("System.Reason", "Work done");
putWorkItem(valuesToUpdate, tfsWIID);
}
Generic function to apply all field changes:
private int putWorkItem(Dictionary<string, object> valuesToUpdate, int tfsID = 0)
{
using (var projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(Config.tfsInnovationUri))
{
var wiStore = projectCollection.GetService<WorkItemStore>();
Project project = wiStore.Projects.GetById(currentTFSProjectID);
if (wiStore != null && wiStore.Projects != null)
{
WorkItem wi = tfsID != 0 ? wiStore.GetWorkItem(tfsID) : new WorkItem(project.WorkItemTypes["Verification Issue"]);
Log.logger.Info(tfsID != 0 ? "Updating TFS WI with ID: " + tfsID : "Creating new TFS WI");
foreach (KeyValuePair<string, object> field in valuesToUpdate)
wi.Fields[field.Key].Value = field.Value;
wi = validateData(wi);
wi.Save();
return wi.Id;
}
}
return 0;
}
The Validation:
public WorkItem validateData(WorkItem wi)
{
var valErrors = wi.Validate();
foreach (Field error in valErrors)
{
Log.logger.Error(wi.Id + " Error: " + error.Name + " val: <" + error.Value + "> allowed values are: ");
foreach (string allowedVal in error.AllowedValues)
Log.logger.Info(allowedVal);
}
return wi;
}
And the output:
2017-07-19 17:51:11,469 [INFO] <closeWI> Closing Item in TFS with ID: 25694
2017-07-19 17:51:13,578 [ERROR] <validateData> 25694 Error: State val: <Closed> allowed values are:
2017-07-19 17:51:13,594 [INFO] <validateData> Active
2017-07-19 17:51:13,594 [INFO] <validateData> Closed
2017-07-19 17:51:13,594 [INFO] <validateData> Integrated
2017-07-19 17:51:13,594 [INFO] <validateData> Integration skipped
2017-07-19 17:51:13,594 [INFO] <validateData> Work Complete
2017-07-19 17:51:13,594 [ERROR] <validateData> 25694 Error: Reason val: <Work done> allowed values are:
2017-07-19 17:51:13,609 [INFO] <validateData> Cancelled
2017-07-19 17:51:13,609 [INFO] <validateData> Customer accepted
2017-07-19 17:51:13,609 [INFO] <validateData> Duplicate
2017-07-19 17:51:13,625 [INFO] <validateData> User Mistake
2017-07-19 17:51:13,625 [INFO] <validateData> Work done
[...]
So as shown above the Value is in the Allowed Values list but it still validates wrong. There are more Errors like changed Date is not set and changed By is not working too, allthough there is a correct user.
I can assure it is working when i change fields like assignedTo or the Description. And it was working in another project to change the state. So it has nothing to do with Permissions.
I didn't try:
new WorkItemStore(_tfs, WorkItemStoreFlags.BypassRules);
because it should work normaly but i will try this in a few minutes.
Anybody got an idea?
To change the work item state, it also requires bypassing rules. Take a look at this question: How to change workflow state of the newly created TFS work item through API?
Just as the solution you mentioned, set BypassRules
property of the WorkItemStore
object to true. to do this, you have to instantiate the work item store in the following way:
var workItemStore = new WorkItemStore(collection, WorkItemStoreFlags.BypassRules);
Note: you need some special permissions to be able to use the BypassRules flag. Your account needs to be in the Project Collection Service Accounts group (or the Team Foundation Service Accounts).
If you don't know how, please follow this tutorial: How to add a user to Project Collection Service Account in TFS / VSO