Azure DevOps Rest API - 如何在工作项中获取指定用户的显示名称

Azure DevOps Rest API - How to grab assigned user's display name in work item

我想获取分配用户在 Azure DevOps 工作项上的显示名称。

// Get client
VssConnection connection = GetVssConnection(_azureDevopsOptions);
WorkItemTrackingHttpClient workItemTrackingHttpClient = connection.GetClient<WorkItemTrackingHttpClient>();

// Define fields
string startDateField = "Microsoft.VSTS.Scheduling.StartDate";
string finishDateField = "Microsoft.VSTS.Scheduling.FinishDate";
string assignedToField = "System.AssignedTo";

// Get work item
var workitemTask = await workItemTrackingHttpClient.GetWorkItemAsync(taskId);

// Get fields from work item
var startDate = workitemTask.Fields.ContainsKey(startDateField) ? workitemTask.Fields[startDateField] : 0;
var finishDate = workitemTask.Fields.ContainsKey(finishDateField) ? workitemTask.Fields[finishDateField] : 0;
var assignedTo = workitemTask.Fields.ContainsKey(assignedToField) ? workitemTask.Fields[assignedToField] : 0;

如果我没记错的话,你应该将 assignedToField 的值解析为 JObject:

JObject identity = JObject.FromObject(workitemTask.Fields[assignedToField]);

然后显示名称可用 identity["DisplayName"]