Richard R
Richard R

Reputation: 13

C# TFS get the last User that changed a specific File

Based on a specific file's location and name, I would like to use C# to programmatically get the name of the last User to have changed this file in TFS. (the last change date would be nice to have)

Though not directly related, as context information, Im using the following to get the latest from TFS based on a file's local path:

        var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(LocalPath);
        using (var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri))
        {
            var workspace = workspaceInfo.GetWorkspace(server);
            GetRequest request = new GetRequest(new ItemSpec(LocalPath, RecursionType.Full), VersionSpec.Latest);
            workspace.Get(request, GetOptions.Overwrite);
        } 

thank you for any hints

Upvotes: 1

Views: 919

Answers (3)

Richard R
Richard R

Reputation: 13

I'll add what I found in case it might help someone else (it appears to work for me)

 var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(modifiedPath);

    TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(workspaceInfo.ServerUri);
    VersionControlServer vcServer = tpc.GetService<VersionControlServer>();


    using (var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri))
    {
        var workspace = workspaceInfo.GetWorkspace(server);

        QueryHistoryParameters historyParams = new QueryHistoryParameters(modifiedPath, RecursionType.Full);

        historyParams.MaxResults = 1;

        Changeset changeset = vcServer.QueryHistory(historyParams).FirstOrDefault();

        string theUser = changeset.CommitterDisplayName;

        MessageBox.Show(theUser);
    }

Upvotes: 0

Eddie Chen - MSFT
Eddie Chen - MSFT

Reputation: 29976

I created a simple code for your reference:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string tfsurl = "http://tfscollectionurl";
            TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(tfsurl));
            VersionControlServer vcs = ttpc.GetService<VersionControlServer>();
            string[] path = { "$/Path/To/Item.cs" };
            ItemSpec[] itemSpecs = ItemSpec.FromStrings(path, RecursionType.Full);
            ItemSpec itemSpec = itemSpecs.Cast<ItemSpec>().First();
            IEnumerable<Changeset> changesets = vcs.QueryHistory(itemSpec);
            Changeset latestchangeset = changesets.Cast<Changeset>().First();
            Console.WriteLine(latestchangeset.Committer);
            Console.WriteLine(latestchangeset.CommitterDisplayName);
            Console.WriteLine(latestchangeset.Owner);
            Console.WriteLine(latestchangeset.OwnerDisplayName);
            Console.WriteLine(latestchangeset.CreationDate);
            Console.ReadLine();
        }
    }
}

Upvotes: 1

DaveShaw
DaveShaw

Reputation: 52818

You can use IVersionControlStore.QueryHistory() to get the Changeset, by default results are returned most recent first, and you can request just the top ` change, there's an overload to also get the changes in that changeset.

Upvotes: 0

Related Questions