AzureDevOps - 通过 JsonPatchOperation 创建工作项

发布于 2025-01-15 21:08:57 字数 1437 浏览 5 评论 0原文

要创建工作项,我需要指定其字段,但我在 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;
    }

To create a workitem I need to specify its fields but where exactly can I see all the possible "field paths" on my AzureDevOps site?
I've edited an existing workitem and added some more fields to it but I cant seem to find the needed "field path" for my JsonPatchOperation.

Any ideas? Thanks in advance!

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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甲如呢乙后呢 2025-01-22 21:08:57

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

  • 将进程编辑器安装到 VS:
    输入图片此处描述

  • 打开字段浏览器:

在此处输入图像描述

  • 检查所需字段:

在此处输入图像描述

You can use the process template editor to see all fields in your org.

  • Install Process Editor to VS:
    enter image description here

  • Open Fields Browser:

enter image description here

  • Check needed fields:

enter image description here

冬天旳寂寞 2025-01-22 21:08:57

另一种方式:使用rest 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();
    }

Another way: using the rest 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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文