Search code examples
javatfstfs-sdk

TFS JAVA SDK14.0.3 - How to fetch remote file from server and make changes and check in again


I have a  project in team foundation server named master-builds . the master-build  folder has a xml file named build-main.xml i want to fetch this files from server and make some changes in it (change  the  version number ) and check in again , how  can i achieve it using TFS JAVA SDK 14.0.3 .

I tried the samples provided  in the folder but not able to achieve.

This is the code snippet to create workspace

public static Workspace createAndMapWorkspace(final TFSTeamProjectCollection tpc) {
    final String workspaceName = "SampleVCWorkspace" + System.currentTimeMillis(); //$NON-NLS-1$
    Workspace workspace = null;

    // Get the workspace
    workspace = tpc.getVersionControlClient().tryGetWorkspace(ConsoleSettings.MAPPING_LOCAL_PATH);

    // Create and map the workspace if it does not exist
    if (workspace == null) {
        workspace = tpc.getVersionControlClient().createWorkspace(
            null,
            workspaceName,
            "Sample workspace comment", //$NON-NLS-1$
            WorkspaceLocation.SERVER,
            null,
            WorkspacePermissionProfile.getPrivateProfile());

        // Map the workspace
        final WorkingFolder workingFolder = new WorkingFolder(
            ConsoleSettings.MAPPING_SERVER_PATH,
            LocalPath.canonicalize(ConsoleSettings.MAPPING_LOCAL_PATH));
        workspace.createWorkingFolder(workingFolder);
    }

    System.out.println("Workspace '" + workspaceName + "' now exists and is mapped"); //$NON-NLS-1$ //$NON-NLS-2$

    return workspace;
}

This is the snippet to add/edit files

public static void getLatest(final Workspace workspace) {
    final ItemSpec spec = new ItemSpec(ConsoleSettings.MAPPING_LOCAL_PATH, RecursionType.FULL);
    final GetRequest request = new GetRequest(spec, LatestVersionSpec.INSTANCE);
    workspace.get(request, GetOptions.NONE);
}

public static File addFile(final Workspace workspace) {

    // Create the file locally
    final File newFile = new File(ConsoleSettings.MAPPING_LOCAL_PATH, "SampleAppFile"); //$NON-NLS-1$
    writeFileContents(newFile, "", "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$

    // Pend an add
    // The encoding is passed as null and the add will detect the encoding
    // of the file
    workspace.pendAdd(new String[] {
        newFile.getAbsolutePath()
    }, false, ENCODING, LockLevel.UNCHANGED, GetOptions.NONE, PendChangesOptions.NONE);

    // Checkin the pending change
    final int cs = checkinPendingChanges(workspace, "Adding a sample file"); //$NON-NLS-1$

    System.out.println("Added file " + newFile.getAbsolutePath() + " in CS# " + cs); //$NON-NLS-1$ //$NON-NLS-2$

    return newFile;
}

public static void editFile(final Workspace workspace, final File file) {
    // Pend edit
    final ItemSpec fileSpec = new ItemSpec(file.getAbsolutePath(), RecursionType.NONE);
    workspace.pendEdit(
        new ItemSpec[] {
            fileSpec
    },
        LockLevel.UNCHANGED,
        ENCODING,

        GetOptions.NONE,
        PendChangesOptions.NONE);

    // Edit the file
    writeFileContents(file, "Edited this file at " + new Date().toString(), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$

    // Checkin the pending change
    final int cs = checkinPendingChanges(workspace, "Editing the sample file"); //$NON-NLS-1$

    System.out.println("Edited file " + file.getAbsolutePath() + " in CS# " + cs); //$NON-NLS-1$ //$NON-NLS-2$
}

These are my questions

 I was able  to successfully add a new file to the  remote server  , but am not able to edit an existing remote file .

If i call editFile followed by  addFile method it will get updated  , but i dont want to add enw file every time ,what i want is to fetch  the latest version of the buils-main.xml file from server and make changes and do the  check in.

Also after creating the  workspace in local path i couldnt see it in that path , is that the correct behavior ?

Could anyone Please help me on this?


Solution

  • When you want to edit a file that is version controlled by TFS, you need to get the file in a workspace and edit it locally, and then check in the modified file to TFS. I'm not familiar with Java code, I would add c# code for your reference.

    1. Create a workspace for the folder that you want to edit:

    -

    using Microsoft.TeamFoundation.Client;
    using System;
    using Microsoft.TeamFoundation.VersionControl.Client;
    
    namespace TestCaseProject
    {
        class Program
        {
            static void Main(string[] args)
            {
                TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("http://tfs:8080/tfs/DefaultCollection"));
                var versioncontrols = tfs.GetService<VersionControlServer>();
    
                var workspace = versioncontrols.CreateWorkspace("test", "Cece Dong");
    
                String ServerFolder = @"$/ScrumProject/TestCaseProject";
                String LocalFolder = @"E:\test";
    
                WorkingFolder workfolder = new WorkingFolder(ServerFolder, LocalFolder);
                workspace.CreateMapping(workfolder);
    
                workspace.Get();
    
    
            }
        }
    }
    
    1. Use Workspace.PendEdit Method to edit file that have got from TFS to workspace.

    2. Use Workspace.CheckIn Method to check in the pending changes.

    Check the blog below for more details:

    https://blogs.msdn.microsoft.com/buckh/2012/03/10/team-foundation-version-control-client-api-example-for-tfs-2010-and-newer/

    Or you can refer to this case to use TFS rest api to update files:

    Updating a file using REST Api Visual Studio Team Services