我怎样才能检索TFS在C#中工作项列表

我怎样才能检索TFS在C#中工作项列表

本文介绍了我怎样才能检索TFS在C#中工作项列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写WPF / C#项目报告工具。我想访问我们的TFS(团队基础服务器)中的所有项目的名称,然后显示在某个项目每个工作项目的统计数据。



我已经得到了项目名称,但得到实际的工作项目是什么让我很难。下面是我到目前为止有:

 公共常量字符串tfsLocation =HTTP://无所谓; 

//从Team Foundation Server的
公开名单<获得项目名称的顶级目录;字符串> LoadProjectList()
{
VAR TPC = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(新的URI(tfsLocation));
变种workItemStore =新WorkItemStore(TPC);
VAR项目=(从Project项目workItemStore.Projects选择project.Name).ToList();

回报率的项目;
}

公共字符串GetProjectInfo(字符串targetProject)
{
字符串信息=的String.Empty;

VAR TPC = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(新的URI(tfsLocation));
变种workItemStore =新WorkItemStore(TPC);

的foreach(在workItemStore.Projects工程项目)
{
如果(project.Name == targetProject)
{
+信息=的String.Format (项目:{0} \\\
\\\
,project.Name);

信息+ =工作项类型:\\\

的foreach(在project.WorkItemTypes WorkItemType项)
{
+信息=的String.Format( - {0} \\\
,item.Name);
+信息=的String.Format( - 说明:{0} \\\
,item.Description);
+信息= - 字段定义:\\\


的foreach(在item.FieldDefinitions FieldDefinition场)
{
+信息=的String.Format( - {0} \\\
,field.Name);
}
信息+ =\\\
;
}
}
}

返回信息;
}



GetProjectInfo发回关于什么是在每个项目中一些有用的信息,但到目前为止,貌似我只看到了什么样的WorkItems对比的组成,而不是的实际的工作项目本身的定义的。我想我已经写了编程找错了地方。



从工作项目,
()
看起来它里面WorkItemTracking.Client,但不是WorkItemStore里面,我不知道哪里去访问它。



最终版本:



下面是我的功能的更新版本,它引用了以下的答案后。这正好与返回之间,新线工作项目名称的长字符串打印出来,这是所有我想获得工作(现在)。

 公共字符串GetProjectInfo(字符串targetProject)
{
字符串信息=的String.Empty;

VAR TPC = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(新的URI(tfsLocation));
WorkItemStore workItemStore =新WorkItemStore(TPC);

查询查询=新的查询(workItemStore,SELECT * FROM WorkItems对比WHERE [System.TeamProject] = @project,新的字典<字符串,字符串>(){{项目,targetProject}} );

WorkItemCollection WIC = query.RunQuery();

的foreach(WIC中的项目工作项)
{
+信息=的String.Format({0} \\\
,item.Title);
}

返回信息;
}


解决方案

您需要WIQL使用查询让你有兴趣,如实际工作项目让所有工作项目特定项目:使用Microsoft.TeamFoundation.WorkItemTracking.Client

 ; 

查询查询=新的查询(
workItemStore,
的问题,选择*其中System.TeamProject = @project,
新字典<字符串,字符串>( ){{项目,project.Name}}
);

VAR workItemCollection = query.RunQuery();
的foreach(在workItemCollection VAR的工作项目)
{
/ *获取工作项属性,你有兴趣的* /
的foreach(在workItem.Fields VAR场)
{
/ *获取字段值* /
+信息=的String.Format(字段名称:{0}值:{1} \\\
,field.name,field.Value);
}
}


I'm trying to write a project reporting tool in WPF / C#. I want to access all the project names on our TFS (Team Foundation Server), and then display statistics for each work item in a given project.

I've got the project names, but getting the actual work items is what's giving me a hard time. Here's what I've got so far:

public const string tfsLocation = "http://whatever";

// get the top list of project names from the team foundation server
public List<string> LoadProjectList()
{
    var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsLocation));
    var workItemStore = new WorkItemStore(tpc);
    var projects = (from Project project in workItemStore.Projects select project.Name).ToList();

    return projects;
}

public string GetProjectInfo(string targetProject)
{
    string info = String.Empty;

    var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsLocation));
    var workItemStore = new WorkItemStore(tpc);

    foreach (Project project in workItemStore.Projects)
    {
        if (project.Name == targetProject)
        {
            info += String.Format("Project: {0}\n\n", project.Name);

            info += "Work Item Types:\n";
            foreach (WorkItemType item in project.WorkItemTypes)
            {
                info += String.Format("-   {0}\n", item.Name);
                info += String.Format("    -   Description: {0}\n", item.Description);
                info +=               "    -   Field Definitions:\n";

                foreach (FieldDefinition field in item.FieldDefinitions)
                {
                    info += String.Format("        -   {0}\n", field.Name);
                }
                info += "\n";
            }
        }
    }

    return info;
}

GetProjectInfo sends back some helpful info about what's in each project, but so far it looks like I'm only seeing the definitions of what the WorkItems consist of, and not the actual WorkItems themselves. I think the programming I've written is looking in the wrong place.

From Microsoft's definition of WorkItem,(http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.workitemtracking.client.workitem.aspx)it looks like it's inside WorkItemTracking.Client, but not inside the WorkItemStore, and I'm not sure where to go to access it.

FINAL VERSION:

Here's the updated version of my function, after referencing the below answer. This just returns a long string of the work item names with new lines between, for printing out, which is all I'm trying to get working (for now).

public string GetProjectInfo(string targetProject)
{
    string info = String.Empty;

    var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsLocation));
    WorkItemStore workItemStore = new WorkItemStore(tpc);

    Query query = new Query(workItemStore, "SELECT * FROM WorkItems WHERE [System.TeamProject] = @project", new Dictionary<string, string>() { { "project", targetProject } });

    WorkItemCollection wic = query.RunQuery();

    foreach (WorkItem item in wic)
    {
        info += String.Format("{0}\n", item.Title);
    }

    return info;
}
解决方案

You need to use WIQL queries to get actual work items you are interested in, e.g. to get all work items for a particular project:

using Microsoft.TeamFoundation.WorkItemTracking.Client;

Query query = new Query(
     workItemStore,
     "select * from issue where System.TeamProject = @project",
     new Dictionary<string, string>() { { "project", project.Name } }
);

var workItemCollection = query.RunQuery();
foreach(var workItem in workItemCollection)
{
   /*Get work item properties you are interested in*/
   foreach(var field in workItem.Fields)
   {
      /*Get field value*/
      info += String.Format("Field name: {0} Value: {1}\n", field.name, field.Value);
   }
}

这篇关于我怎样才能检索TFS在C#中工作项列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 19:47