Asked by:
TFS JAVA SDK 14.0.3 - How to fetch a remote file from server and make changes and check in again

Question
-
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 code 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?
- Moved by Michael WhartonMVP Tuesday, April 24, 2018 11:33 AM wrong forum
- Moved by Jack Zhai-MSFTMicrosoft contingent staff Monday, April 30, 2018 7:25 AM TFS issue
Sunday, April 22, 2018 9:33 AM
All replies
-
Hi AnsarSamad,
Welcome to the MSDN forum.
Refer to your description, it seems your issue is about the TFS. Per this announcement, we can get advice on Stack Overflow, since our forum is to discuss the VS IDE, thank you for your understanding.
Generally, we cannot edit the remote file directly, you need to create a workspace and checkout the remote file to local workspace, then edit and check in the pending changes.
Please check if this thread helps: https://stackoverflow.com/questions/38117179/how-to-check-in-a-file-into-tfs-using-java-sdk
Best regards,
Sara
MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com
- Edited by Sara LiuMicrosoft contingent staff Wednesday, April 25, 2018 8:38 AM
Wednesday, April 25, 2018 8:34 AM