c# - 如何通过 C# 从 tfs 获取所有工作项

标签 c# tfs workitem

美好的一天!

我尝试从 TFS (2010) 的集合中获取所有工作项。 我找到了 blog有一些代码示例,但效果不佳:

ICommonStructureService Iss = (ICommonStructureService)projCollection.GetService(typeof(ICommonStructureService));

这行代码返回错误 - 无法连接到 TFS(相同的代码连接良好)。 好的,我尝试重写这段代码并尝试这个(来自 msdn 的另一个工作示例):

private static String _tfsURI ="http:tfsServer:port/tfsUrl"
static void Main(string[] args)
    {
        // Connect to Team Foundation Server
        //     Server is the name of the server that is running the application tier for   Team Foundation.
        //     Port is the port that Team Foundation uses. The default port is 8080.
        //     VDir is the virtual path to the Team Foundation application. The default path is tfs.
        Uri tfsUri = (args.Length < 1) ?
            new Uri(_tfsURI) : new Uri(args[0]);

        TfsConfigurationServer configurationServer =
            TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);

        Console.WriteLine("Get the catalog of team project collections \n");
        // Get the catalog of team project collections
        ReadOnlyCollection<CatalogNode> collectionNodes = configurationServer.CatalogNode.QueryChildren(
            new[] { CatalogResourceTypes.ProjectCollection },
            false, CatalogQueryOptions.None);
        // List the team project collections
        foreach (CatalogNode collectionNode in collectionNodes)
        {
            Console.WriteLine(collectionNode.Resource.DisplayName);

            // Use the InstanceId property to get the team project collection
            Guid collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
            TfsTeamProjectCollection teamProjectCollection = configurationServer.GetTeamProjectCollection(collectionId);


                    ICommonStructureService Iis = (ICommonStructureService)teamProjectCollection.GetService(typeof(ICommonStructureService));
                 ProjectInfo[] projInfo = Iis.ListAllProjects();
              //  WorkItemStore wis = (WorkItemStore)teamProjectCollection.GetService();

                 var wis = (WorkItemStore)teamProjectCollection.GetService(typeof(ICommonStructureService));

            foreach (var p in projInfo)
                 {

                     Console.WriteLine("Name:" + p.Name + "Status" + p.Status);
                     var wic = wis.Query(" SELECT [System.Id], [System.WorkItemType]," +
            " [System.State], [System.AssignedTo], [System.Title] " +
              " FROM WorkItems " +
              " WHERE [System.TeamProject] = '" + p.Name +
               "' ORDER BY [System.WorkItemType], [System.Id]");

                     Console.WriteLine("wic.Count:"+wic.Count);

                     foreach (var wi in wic)
                     {

                         Console.WriteLine(wi.Id);
                         Console.WriteLine(wi.Title);
                     }

                 }

            // Print the name of the team project collection
               Console.WriteLine("Collection: " + teamProjectCollection.Name);

            // Get a catalog of team projects for the collection
            ReadOnlyCollection<CatalogNode> projectNodes = collectionNode.QueryChildren(
                new[] { CatalogResourceTypes.TeamProject },
                false, CatalogQueryOptions.None);

            // List the team projects in the collection
            foreach (CatalogNode projectNode in projectNodes)
            {
                Console.WriteLine(" Team Project: " + projectNode.Resource.DisplayName);

            }
        }
        Console.ReadLine();
    }

所以,我收到错误:无法将“Microsoft.TeamFoundation.Proxy.CommonStructureService”的对象类型转换为“Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore”。

请帮我从 Tfs 获取所有工作项。

最佳答案

这一行

var wis = (WorkItemStore)teamProjectCollection.GetService(typeof(ICommonStructureService));

显然是错误的。解决方案很简单:

var wis = teamProjectCollection.GetService<WorkItemStore>();

关于c# - 如何通过 C# 从 tfs 获取所有工作项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21374822/

相关文章:

c# - 我如何访问 ASP.NET MVC 3 模型中的虚拟目录?

c# - 收到错误 : Contract requires Duplex, 但绑定(bind) 'BasicHttpBinding' 不支持它或未正确配置以支持它

tfs - VisualStudioOnline ServiceHooks 是否可用于本地 TFS 实例?

c# - 通过 Visual Studio Team Services 和 Team Foundation Server 的 REST API 概述从 Microsoft vso WorkItem 访问评论

tfs - 向工作项类型添加新的初始状态 Bug - Web Access 任务板显示错误 TF400917

Azure Devops ODATA : Request details of linked items,如何检索TargetWorkItem的AreaPath

c# - 如何在 wpf 中更改数据网格的单个单元格颜色?

c# - 为什么 Microsoft.AspNetCore.Http.StatusCodes 没有设计为枚举?

tfs - 如何在 TFS 2012 中将审核请求与多个变更集相关联

visual-studio-2012 - 如何使 TFS 反射(reflect)文件系统更改,而不是解决方案资源管理器