Search code examples
tfstfs-sdk

How to retrieve and save tasks(work items) from TFS2010 projects from specific collection


Thanks for the help about my earlier question answered few days back

How to retrieve TFS2010 projects from specific collection

Now, I thought that the above question will solve my problem of hitting a specific task and reading/saving the task. But I couldnt find a solution.

What I want is to have a function which take three parameters SaveWorkItem(CollectionID, ProjectID, WorkItemID)

and then we can update the workitem within the project - collection.

Any help/pointers would be highy appreciated.

Thanks.


Solution

  • To retrieve tasks

    public void RetrieveWorkItems()
        {
            var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://TFS:8080/TFS/DefaultCollection"));
            var workItemStore = tfs.GetService<WorkItemStore>();
    
            var wiqlQuery = String.Format( @"Select [State], [Title] From WorkItems Where [Work Item Type] = 'Task' Order By [State] Asc, [Changed Date] Desc"); ;
    
    
            WorkItemCollection witCollection = workItemStore.Query(wiqlQuery);
            foreach (WorkItem workItem in witCollection)
            {
                Console.WriteLine("ID: {0}", workItem.Id);
                Console.WriteLine("Title: {0}", workItem.Title);
            }
        }
    

    To enter task

    public void CrateTask()
        {
            var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://TFS:8080/TFS/DefaultCollection"));
            var workItemStore = tfs.GetService<WorkItemStore>();
            Project proj = workItemStore.Projects["ProjectName"];
            WorkItemType type = proj.WorkItemTypes["Task"];
            WorkItem workItem = new WorkItem(type);
            workItem.Title = "Task entered using API";
            workItem["Activity"] = "Configuration";
            workItem.Save();
    
        }