首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AzureDevOps -通过JsonPatchOperation创建WorkItem

AzureDevOps -通过JsonPatchOperation创建WorkItem
EN

Stack Overflow用户
提问于 2022-03-21 09:55:19
回答 2查看 304关注 0票数 0

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

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

代码语言:javascript
复制
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;
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-24 08:56:39

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

  • 安装过程编辑器到VS:

  • 开放字段浏览器:

  • 检查所需字段:

票数 0
EN

Stack Overflow用户

发布于 2022-03-24 09:02:56

另一种方式:使用rest。

代码语言:javascript
复制
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();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71555662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档