Search code examples
c#tfstfs-sdk

TFS API Pulling changesets of a certain IterationId


I am trying to pull work item information from Team Foundation Server. I have a working program that pulls all WorkItems. I am attempting to only pull WorkItems that belong to a specific iteration (IterationId).

One way to do this that I have tried... is to produce a list of all the WorkItems and then filter out lines that have an IterationID that matches the user given iteration. The problem with this is that it takes a very long time to pull all changes. Most of my iterations only contain 5-10 WorkItems.

My program that pulls all changes:

Uri tfsUri = new Uri(projectCollection.Uri.ToString());
        VersionControlServer controlServer = projectCollection.GetService<VersionControlServer>();
        var hHistory = controlServer.QueryHistory("$/MyPath/",
                                                    VersionSpec.Latest,
                                                    0,
                                                    RecursionType.Full,
                                                    String.Empty,
                                                    null,
                                                    VersionSpec.Latest,
                                                    int.MaxValue,
                                                    false,
                                                    false,
                                                    true);
foreach (Changeset change in hHistory)
{
    cfChangedFiles = change.Changes;
    foreach (WorkItem wi in change.WorkItems)
    {
        strInput = Encoding.GetEncoding(1252).GetString(GetStreamAsByteArray(cfChangedFiles[i].Item.DownloadFile()));
        Console.WriteLine("Iteration ID: " + wi.IterationId.ToString());
        Console.WriteLine("Title: " + wi.Title.ToString());
        Console.WriteLine("ChangesetID: " + change.ChangesetId.ToString());
        Console.WriteLine("SourceText:\t\t" + strInput); //EDIT NEW
        //...Print Other Things...
    }
}

I have also tried pulling the information using WorkItemStore.Query:

WorkItemStore workItemStore = (WorkItemStore)projectCollection.GetService(typeof(WorkItemStore));
// Run a query.
WorkItemCollection queryResults = workItemStore.Query(
    "Select [State], [Title] " +
    "From WorkItems Where [System.IterationId] ='73'");
foreach(WorkItem wi in queryResults)
{
    Console.WriteLine("Iteration ID: " + wi.IterationId.ToString());
    /*...Print Other Things...
}

The problem with the above code is that I cannot pull certain things (like the Source Text).


Similar Question: TFS API - slow foreach changeset iteration - This question is similar in that the OP's program loads very slowly. However, the answer given is already implemented in my program. This also still pulls all changes.


Solution

  • I found a solution to my question. The 6th and 7th parameters of QueryHistory are the starting ChangesetID and ending ChangesetID.

    Uri tfsUri = new Uri(projectCollection.Uri.ToString());
        VersionControlServer controlServer = projectCollection.GetService<VersionControlServer>();
        var hHistory = controlServer.QueryHistory("$/MyPath/",
                                                    VersionSpec.Latest,
                                                    0,
                                                    RecursionType.Full,
                                                    String.Empty,
                                                    VersionSpec.ParseSingleSpec(changeset.ChangesetId.ToString(), null),
                                                    VersionSpec.ParseSingleSpec(changeset.ChangesetId.ToString(), null),
                                                    int.MaxValue,
                                                    false,
                                                    false,
                                                    true);
    

    I used WIQL to select all ChangesetIDs that correspond to a given IterationID. Then, I looped through the above code for each ChangesetID. This allowed my application to only load the changes for my given ChangesetID.