简体   繁体   English

如何通过C#从TFS获取所有工作项

[英]How to get all work items from tfs via c#

Good day! 美好的一天!

I try to get all WorkItems from Collection at TFS (2010). 我尝试从TFS(2010)的集合中获取所有WorkItems。 I found blog with some code example, but it is not working well: 我找到了带有一些代码示例的博客 ,但效果不佳:

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

This line of code returns an error- cannot connect to TFS (the same code connects well). 此行代码返回错误-无法连接到TFS(同一代码连接良好)。 Ok, i try to rewrite this code and try this (another work example from msdn): 好的,我尝试重写此代码并尝试执行此操作(来自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();
    }

So, i get error: cannot cast object type of "Microsoft.TeamFoundation.Proxy.CommonStructureService" to "Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore". 因此,我得到了一个错误:无法将“ Microsoft.TeamFoundation.Proxy.CommonStructureService”的对象类型转换为“ Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore”。

Please, help me hot to get all work items from Tfs. 请帮我忙,从Tfs获取所有工作项目。

This line 这条线

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

is clearly mistaken. 显然是错误的。 Solution is simple: 解决方法很简单:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM