Search code examples
c#tfscheckin-policy

How to Programatically get TFS Team Project Name


I am creating a check-in policy using:

using Microsoft.TeamFoundation.VersionControl.Client;

I want to get the current TFS Team Project Name from the current workspace or the PendingCheckin

Any ideas?


Solution

  • You can use VersionControlServer.GetTeamProjectForServerPath on a pending change thus:

    var pendingChange = pendingCheckin.GetAllPendingChanges().FirstOrDefault();
    if(pendingChange != null) {
        return vcs.GetTeamProjectForServerPath(pendingChange.ServerItem);
    }
    

    This also means you can detect if pending changes span multiple team projects.


    Edit/expansion:

    Now that I think about it, you should probably use the local path, since the files you're checking in might not be on the server yet (adds).

    So you can use Workspace.GetTeamProjectForLocalPath instead:

    var workspace = pendingCheckin.GetWorkspace();
    var pendingChange = pendingCheckin.GetAllPendingChanges().FirstOrDefault();
    if(pendingChange != null) {
        return workspace.GetTeamProjectForLocalPath(pendingChange.LocalItem);
    }