TFS API获取工作项数据

时间:2015-01-13 10:05:34

标签: c# tfs tfs-sdk

我坚持使用TFS API的简单问题,当我获得工作项目时,我无法获得workitem Effort数据,其想法是通过特定标准从TFS获取工作项,然后更改Effort数据并将其保存回TFS。现在我可以获得工作项目,并按字段更新任何数据,但我发现如何获得Effort并更新它。

enter image description here

查询从TFS获取数据:

SELECT * FROM WorkItems WHERE [System.WorkItemType] = 'Task' AND [Assigned to] = 'name' 
ORDER BY [System.WorkItemType], [System.Id]

这是获取字段的代码

public void getDataFromTfs()
{
    Console.WriteLine("Getting data from TFS to Store");
    Console.WriteLine("*********************");
    setQuery();
    Console.WriteLine("Query" + byTasksModified.ToString());
    Console.WriteLine("*********************");
    Console.ReadLine();
    credentials = new System.Net.NetworkCredential("xxxxx", "xxxxxx", "xxxxx");
    TfsTeamProjectCollection teamProjectCollection =
    new TfsTeamProjectCollection(new Uri(@"LINK HERE"), credentials);
    teamProjectCollection.EnsureAuthenticated();
    Store = (WorkItemStore) teamProjectCollection.GetService(typeof(WorkItemStore));
    WIC = Store.Query(byTasksAssignedTo);
    Console.WriteLine("Data fetched into Store");
    foreach (WorkItem workItem in WIC)
    {
        Console.WriteLine("ID: {0}", workItem.Id);
        Console.WriteLine("Title: {0}", workItem.Title);
    }
}

这里我通过特定查询得到了我需要的所有任务,然后我有了拉取特定任务的方法

public void getSpecificWorkItemData()
{
    workItem = Store.GetWorkItem(64);
    Console.WriteLine("ID: {0}", workItem.Id);
    Console.WriteLine("Title: {0}", workItem.Title);

    workItem.Open();
    Console.ReadLine();
}

然后我有任务的更新方法

public void updateWorkItem()
{
   workItem.Fields["Assigned to"].Value = "NAME";

   if (workItem.IsValid() == false)
   {
       Console.WriteLine("Item is not valid");
   }
   else
   {
        try
        {
            workItem.Save();
        }
        catch (ValidationException exception)
        {
            Console.WriteLine("Error saving work item!");
            Console.WriteLine(exception.Message);
        }
        Console.WriteLine("Item Saved");
        workItem.Close();
        Console.ReadLine();
    }
}

1 个答案:

答案 0 :(得分:6)

这取决于您的流程模板哪些字段可用。现在您正在查询“任务”工作项,默认情况下“工作”组中包含以下字段。请注意,任务没有“努力”字段,还有其他:

Process Template    | Field (s)
Scrum               | Remaining Work
MSF Agile           | Remaining Work, Completed Work, Original estimate
MSF CMMI            | Remaining Work, Completed Work, Original estimate

您所使用的Effort字段在Scrum流程模板中定义,并且由需求类别中的所有工作项使用(默认情况下为Product Backlog Item和Bug):

Process Template    | Field (s)
Scrum               | Effort
MSF Agile           | Story Point
MSF CMMI            | Size

如果要更新Scrum Process模板中工作项的Effort字段,最好查询工作项类型类别“Requirement Category”中的工作项:

SELECT * 
    FROM WorkItems 
    WHERE [System.TeamProject] = @project  
        AND  [System.WorkItemType] IN GROUP 'Requirement Category'  
    ORDER BY [System.Id]

如果要更新剩余工作,已完成工时或原始估算字段,则需要使用任务:

SELECT id, Microsoft.VSTS.Scheduling.CompletedWork, Microsoft.VSTS.Scheduling.RemainingWork, Microsoft.VSTS.Scheduling.OriginalEstimate
    FROM WorkItems 
    WHERE [System.TeamProject] = @project  
        AND  [System.WorkItemType] IN GROUP 'Task Category'  
    ORDER BY [System.Id]

然后根据TFS Field Reference中定义的Ref名称更新它们是最安全的方法:

workItem.Fields["Microsoft.VSTS.Scheduling.CompletedWork"].Value = 123;
workItem.Fields["Microsoft.VSTS.Scheduling.RemainingWork"].Value = 123;
workItem.Fields["Microsoft.VSTS.Scheduling.OriginalEstimate"].Value = 123;