我正在尝试使用C#在TFS中设置“分配给”字段的值。我设置的值为“ MYDOMAIN \\ DOMAINID”。其中:
当我尝试验证商品时,由于“ InvalidListValue”原因而失败。
但是,当我尝试通过REST Patch调用在同一工作区中设置相同的值时,它将起作用。
谁能告诉我我在这里做错了什么或如何纠正此错误?
编辑:这是示例代码:
using TF_Frmwrk_Client = Microsoft.TeamFoundation.Framework.Client;
using TF_WIT_Client = Microsoft.TeamFoundation.WorkItemTracking.Client;
public bool CreateTFSWorkItem()
{
string fieldValue = String.Empty;
TF_WIT_Client.FieldDefinition tfFieldDefinition;
TF_WIT_Client.Field tfWorkItemField;
TF_Frmwrk_Client.CatalogNode collectionNode = null;
if (_collectionNodes != null)
{
collectionNode = _collectionNodes.FirstOrDefault(c => c.Resource.DisplayName == "MyProjectCollection");
}
var tpcId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
var tpc = _configurationServer.GetTeamProjectCollection(tpcId);
TF_WIT_Client.WorkItemStore wiStore = tpc.GetService<TF_WIT_Client.WorkItemStore>();
var project = wiStore.Projects["MyTFSProject"];
var tfWorkItemType = project.WorkItemTypes["Bug"];
var tfWorkItem = new TF_WIT_Client.WorkItem(tfWorkItemType);
// Field 'Title'
tfFieldDefinition = tfWorkItemType.FieldDefinitions.TryGetByName("Title");
if (tfFieldDefinition != null)
{
tfWorkItemField = tfWorkItem.Fields.GetById(tfFieldDefinition.Id);
if (tfWorkItemField != null)
{
if (tfWorkItemField.IsEditable)
{
if (tfWorkItemField.Value != null)
{
if (!tfWorkItemField.Value.Equals("This is a title!"))
{
tfWorkItemField.Value = "This is a title!";
}
}
}
}
else
{
throw new InvalidOperationException(String.Format("TFS field not found: FieldName '{0}'.", "Title"));
}
}
else
{
throw new InvalidOperationException(String.Format("TFS field definition not found: FieldName '{0}'.", "Title"));
}
// Field 'Assigned To'
tfFieldDefinition = tfWorkItemType.FieldDefinitions.TryGetByName("AssignedTo");
if (tfFieldDefinition != null)
{
tfWorkItemField = tfWorkItem.Fields.GetById(tfFieldDefinition.Id);
if (tfWorkItemField != null)
{
if (tfWorkItemField.IsEditable)
{
if (tfWorkItemField.Value != null)
{
if (!tfWorkItemField.Value.Equals("MYDOMAIN\\MYUSER"))
{
tfWorkItemField.Value = "MYDOMAIN\\MYUSER";
}
}
}
}
else
{
throw new InvalidOperationException(String.Format("TFS field not found: FieldName '{0}'.", "Assigned To"));
}
}
else
{
throw new InvalidOperationException(String.Format("TFS field definition not found: FieldName '{0}'.", "Assigned To"));
}
string valiationMessage = string.Empty;
ArrayList invalidFields = tfWorkItem.Validate();
if (invalidFields.Count > 0)
{
StringBuilder fieldErrors = new StringBuilder();
foreach (TF_WIT_Client.Field field in invalidFields)
{
fieldErrors.AppendFormat("Field: \"<b>{0}</b>\", Value: \"<b>{1}</b>\", Validation failure reason: <b>{2}</b>.<br />",
field.Name, field.Value, field.Status);
}
valiationMessage = fieldErrors.ToString();
throw new Exception(valiationMessage);
}
tfWorkItem.Save();
return true;
}
谢谢
答案 0 :(得分:0)
您可以使用下面的C#代码示例来设置'Assigned To
'字段的值,它对我有用:(您需要指定latest revision number
来更新字段。并且值格式应为DOMAIN\\USER
)
using System;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.VisualStudio.Services.WebApi;
using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
using Microsoft.VisualStudio.Services.WebApi.Patch;
namespace UpdateFieldValue
{
class Program
{
static void Main(string[] args)
{
var myCredentials = new VssClientCredentials();
var connection = new VssConnection(new Uri(@"http://server:8080/tfs/DefaultCollection"), myCredentials);
WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();
int workitemid = 112;
JsonPatchDocument patchDocument = new JsonPatchDocument();
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Test,
Path = "/rev",
Value = "6"
}
);
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/System.AssignedTo",
Value = "Domain\\user"
}
);
Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.WorkItem result = workItemTrackingClient.UpdateWorkItemAsync(patchDocument, workitemid).Result;
}
}
}