Azure DevOps - 通过 JsonPatchOperation 创建工作项

AzureDevOps - Creating WorkItem via JsonPatchOperation

要创建一个工作项,我需要指定它的字段,但是在我的 AzureDevOps 站点上,我究竟在哪里可以看到所有可能的“字段路径”? 我已经编辑了一个现有的工作项并向其添加了更多字段,但我似乎无法为我的 JsonPatchOperation 找到所需的“字段路径”。

有什么想法吗?提前致谢!

public static WorkItem CreateWorkItem(VssConnection connection, string title, string type, string description, string tags)
    {

        string project = "xxx";
  
        // Construct the object containing field values required for the new work item
        JsonPatchDocument patchDocument = new JsonPatchDocument();

        patchDocument.Add(
            new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path = "/fields/System.Title", <-- field path
                Value = title
            }
        );

        patchDocument.Add(
            new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path = "/fields/System.Description", <-- field path
                Value = description
            }
        );

 

        // Get a client        
        WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();

        // Create the new work item
        WorkItem newWorkItem = workItemTrackingClient.CreateWorkItemAsync(patchDocument, project, type).Result;

        Console.WriteLine("Created work item ID {0} {1}", newWorkItem.Id, newWorkItem.Fields["System.Title"]);

        return newWorkItem;
    }

您可以使用流程模板编辑器查看您组织中的所有字段。

  • 将进程编辑器安装到 VS:

  • 打开字段浏览器:

  • 检查需要的字段:

另一种方式:使用其余部分 api。

WorkItemTrackingProcessHttpClient ProcessHttpClient = Connection.GetClient<WorkItemTrackingProcessHttpClient>();

string processName = "My New Process"; //existing process
string witName = "Task"; //existing work item type
Guid procId;
string witRefName;

GetProcAndWIT(processName, witName, out procId, out witRefName);

ShowCurrentFields(procId, witRefName);



    private static void ShowCurrentFields(Guid procId, string witRefName)
    {
        var fields = ProcessHttpClient.GetAllWorkItemTypeFieldsAsync(procId, witRefName).Result;

        Console.WriteLine("{0, -20} : {1, -40} : {2, -10} : {3, -8} : {4, -8} : {5, -8}", 
            "Name", "Reference Name", "Type", "Required", "ReadOnly", "Default");


        foreach (var field in fields)
        {
            Console.WriteLine("------------------------------------------------------------------------------------------------------------");
            Console.WriteLine("{0, -20} : {1, -40} : {2, -10} : {3, -8} : {4, -8} : {5, -8}",
                field.Name, field.ReferenceName, field.Type, field.Required, field.ReadOnly, field.DefaultValue);
        }

        Console.WriteLine("------------------------------------------------------------------------------------------------------------\n\n\n\n");
    }

    private static void GetProcAndWIT(string processName, string witName, out Guid procId, out string witRefName)
    {
        procId = GetProcessGuid(processName);
        if (procId == null)
        {
            throw new Exception("Can not find process.");
        }

        witRefName = GetWITrefName(procId, witName);
        if (string.IsNullOrEmpty(witRefName))
        {
            throw new Exception("Can not find work item type.");
        }
    }

    private static Guid GetProcessGuid(string processName)
    {
        Guid newProcessGuid = Guid.Empty;

        var processes = ProcessHttpClient.GetListOfProcessesAsync().Result;

        return (from p in processes where p.Name == processName select p.TypeId).FirstOrDefault();
    }

    private static string GetWITrefName(Guid procGuid, string witName)
    {
        var wiTypes = ProcessHttpClient.GetProcessWorkItemTypesAsync(procGuid).Result;

        return (from p in wiTypes where p.Name == witName select p.ReferenceName).FirstOrDefault();
    }