Purplegoldfish
Purplegoldfish

Reputation: 5294

"There is no working folder mapping" error when accessing TFS

I'm working on an application that can create and add items to TFS.

My files get created correctly and in the correct location, however the Workspace.PendAdd method only works for one specific workspace, other workspaces give me the "There is no working folder mapping" error.

The method I am using is PendAdd(string filepath, bool recursive) so I pass in a directory to be added and would expect to add both the directory and its files as a pending add in TFS.

Both workspaces are identical in everything but name, neither have any files currently checked out and neither contain any files.

From my google searches I have found that there may be an issue when adding files to a workspace mapped to $/ but the workspace I am using is mapped to $/TFSFiles/ and so far nothing else seems likely.

The code for my PendAdd method is :

 private IEnumerable<PendingChange> PendAddFileToTfs(string newFilename)
      {
         var previousPendingChanges = new List<PendingChange>(_selectedWorkspace.GetPendingChanges());

         var changesAdded = _selectedWorkspace.PendAdd(Path.GetDirectoryName(newFilename), true);

         var pendingAdds = new List<PendingChange>(_selectedWorkspace.GetPendingChanges());

         var itemsToCheckIn = pendingAdds.Except(previousPendingChanges);

         return itemsToCheckIn;
      }

The method fails at the _selectedWorkspace.PendAdd line. I debugged and verified it is the correct local path and correct workspace.

Can someone point me in the right direction here?

EDIT:

The _selectedWorkspace variable is set by the user.

I get a list of all available workspaces via this code:

_workspaces = _versionControl.QueryWorkspaces(null, _versionControl.AuthorizedUser,environment.MachineName);

I then show a list of workspaces in a combobox and allow the user to select one.

This calls this method:

 public void SetWorkspace(string workspaceName)
      {
         _selectedWorkspace = _workspaces.FirstOrDefault(ws => ws.Name.Equals(workspaceName));
      }

Upvotes: 1

Views: 2877

Answers (1)

You need to create a local workspace before you can add pendAdd files to TFS. I am not sure where _selectedWorkspace is coming from but it looks like it is not configured properly. Although I don't have a c# version to hand I do have a version in PowerShell that calls c#... should give you the way to go.

function New-TfsTeamProjectRootFolder {
    Param(
        [Microsoft.TeamFoundation.Client.TfsTeamProjectCollection] $TfsCollection,
        [Microsoft.TeamFoundation.Server.ProjectInfo] $TfsTeamProject,
        [String] $GlobalEntryValue
    )
    $TempWorkspaceName = "Create-TfsTeamSourceFolder"
    $TempFolder = "c:\temp\$TempWorkspaceName"
    $ServerFolder = "$/$($TfsTeamProject.Name)"
    $TfsVersionControl = Get-TfsVersionControlServer $TfsCollection
    try {
        $workspace = $TfsVersionControl.GetWorkspace($TempFolder )
    } catch {
        $workspace = $TfsVersionControl.CreateWorkspace($TempWorkspaceName);
        $workspace.Map($ServerFolder, $TempFolder); 
    }
    $NewFolder = "$TempFolder\$GlobalEntryValue";
    try {
        $SourceItem = $TfsVersionControl.GetItem("$ServerFolder/$GlobalEntryValue")
    } catch {
        New-Item -ItemType Directory -Force -Path $NewFolder;
        $workspace.PendAdd($NewFolder, $true); 
        $pendingChanges = $workspace.GetPendingChanges(); 
        $changesetNumber = $workspace.CheckIn($pendingChanges, "Added folder for '$GlobalEntryValue'"); 
        $SourceItem = $TfsVersionControl.GetItem("$ServerFolder/$GlobalEntryValue")
    }
    $workspace.Delete()
    Return $SourceItem
}

Again I am not sure why your code is not working as I think the issue is in the greater context than we can see in the example above.

Upvotes: 2

Related Questions