0

Is there any way to update the contents of a file in a project on your visual studio team services account using HTTP verbs, similar to how it is done here with github https://developer.github.com/v3/repos/contents/#update-a-file.

 var json={
        "comment": "Update scripts.json",
        "changes": [{
            "changeType": 2,
            "item": {
                "path": "$/ExtensionsTest/scripts.json",
                "contentMetadata": { "encoding": 65001 },
                "version": 47
            },
            "newContent": {
                "content": "[ {\"hello\" : \"Test\"} ]",
                "contentType":"RawText"
            }
        }]
    };
   $.ajax({
        type: 'POST',
        url: 'https://xxxxx.visualstudio.com/_apis/tfvc/changesets?api-version=3.0-preview.2',
        contentType: 'application/json',
        data: JSON.stringify(json),
        cache: false,
        dataType: "json",
        beforeSend: function (xhr) {
                    xhr.setRequestHeader("Authorization", "Basic " +    btoa("my UserName" + ":" + "my PW"));
        }
    }).done(function (data) {
        console.log(data);
    });
};

The code above is what I am using and I get a 400 error with it. Any suggestions on what I am doing wrong.

D.Grace
  • 3
  • 1
  • 4
  • What's detail code? Try to make the test via POSTMAN tool (Chrome) to verify the request. On the other hand, you can try to capture requests via developer tool (https://msdn.microsoft.com/en-us/library/gg130952(v=vs.85).aspx) (open web access >code , update and save a file and capture requests.) – starian chen-MSFT Jan 20 '17 at 02:07
  • When I check the request using the dev tool, trying to POST to the URL you referenced in step 2 of using TFVC I get a 400 status. I am not sure how to reproduce the call using postman.I am just using ajax with type : "POST", url: (step 2 url), data: (what you have in JSON data but with my content and file path, header: Authorization, and contentType: application/json is there something I am missing – D.Grace Jan 20 '17 at 15:45
  • What's the details of request that you captured via dev tool? What's the detail of JSON data? Provide your detail code here. – starian chen-MSFT Jan 23 '17 at 02:57
  • I put my code in my answer below – D.Grace Jan 23 '17 at 14:39
  • How do you want to update scripts.json? The value of content (newContent>content) should be the whole content in scripts.json and should be included in Double quotation marks ("[ {\"hello\" : \"Test\" } ]" – starian chen-MSFT Jan 24 '17 at 01:37
  • O so I need to include the current content of scripts.json and the new content added on to it. Do you think that would fix the 400 error. – D.Grace Jan 24 '17 at 04:03
  • Yes, you need to specify the whole content of the file and included in "". Also, use "RawText", "Edit" instead (Double quotation marks). – starian chen-MSFT Jan 24 '17 at 06:58
  • What's the result now? – starian chen-MSFT Jan 25 '17 at 03:25
  • I will be able to let you know very soon, just got caught up in other things should know by the end of today. – D.Grace Jan 25 '17 at 17:01
  • POST https://{accountname}/DefaultCollection/_apis/tfvc/changesets?api-version=3.0 400 () I still get this message when I try it...I updated the code in my question to match what I changed it to – D.Grace Jan 26 '17 at 00:41
  • You need to define a variable to store JSON, then convert it to JSON string by using JSON.stringify function. I updated my answer, please check it (works fine for me). – starian chen-MSFT Jan 26 '17 at 02:01
  • I used your solution and I got a 404 error instead of a 400, which is strange because when I follow my url link it lists all my changesets. – D.Grace Jan 26 '17 at 22:59
  • Update: I get a cross origin error in Chrome now, and in Firefox I don't get any errors however it doesn't seem to be working. I updated the code above – D.Grace Jan 26 '17 at 23:30
  • So, what's the detail result in firefox? – starian chen-MSFT Jan 27 '17 at 04:45
  • Nothing comes up in the console in Firefox but it also doesn't seem to work because it isn't triggering the .done function and the file on the vsts isn't being updated – D.Grace Jan 27 '17 at 14:28
  • Provide the details of request that you captured via develop tool. – starian chen-MSFT Jan 29 '17 at 09:31
  • It says the status code is a 302 – D.Grace Jan 30 '17 at 05:46
  • Can you provide details of related (update file) requests? (request method, request url, request data, response status, response data) – starian chen-MSFT Jan 30 '17 at 06:02
  • Request URL: https://xxxxxxx.visualstudio.com/_apis/tfvc/changesets?api-version=3.0-preview.2 Request Method: POST I can't find request data Response status: 302 Found – D.Grace Jan 31 '17 at 02:10
  • What's the result if you try it with IE? Can you create a new project, then add my account to it (support-v-stache@hotmail.com)? – starian chen-MSFT Feb 01 '17 at 01:28
  • It doesn't work at all in IE...also I have the data now: {"comment":"Update scripts.json","changes":[{"changeType":2,"item":{"path":"$/ExtensionsTest/scripts .json","contentMetadata":{"encoding":65001},"version":47},"newContent":{"content":"[ {\"hello\" : \"Test \"} ]","contentType":"RawText"}}]}.....I am still getting a 302 in firefox – D.Grace Feb 01 '17 at 18:14
  • Update it is giving me a 401 unauthorized error if I use my email as my username in the beforeSend function, but if I use my display name it gives me a 302...I am using my PAT as my password – D.Grace Feb 01 '17 at 18:17
  • Try to use Alternate authentication credentials and check the result. BTW, the username can be empty if you are using PAT. – starian chen-MSFT Feb 02 '17 at 01:56
  • Ok I completely cleared out the username field so it looks like this btoa(PAT)..and I still get a 302...I am not sure I understand why since I was abut to get the data from the json file using the same authorization..I am just not able to POST – D.Grace Feb 02 '17 at 17:28
  • I got it working! thank you very much! – D.Grace Feb 02 '17 at 20:07
  • Glad to hear that the issue has been solved, you can mark my answer as answer. – starian chen-MSFT Feb 03 '17 at 01:36

1 Answers1

1

Try these ways:

If you are using GIT:

  1. Get commit ID value: Request method: GET; URL [collection url]/[team project name]/_apis/git/repositories/[repository name]/commits?api-version=1.0&branch=master&$top=1
  2. Update file content: Request method: Post; URL: [collection url]/[team project name]/_apis/git/repositories/[repository name]/pushes?api-version=3.0-preview.2; Content Type: application/json;

JSON data:

{
    "refUpdates": [{
        "name": "refs/heads/master",
        "oldObjectId": "[step 1 commit ID]"
    }],
    "commits": [{
        "comment": "Updated BuildLog.cs",
        "changes": [{
            "changeType": 2,
            "item": {"path": "/BuildLog.cs"},
            "newContent": {
                "content": "using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class BuildLog
    {
        public int count;
        public string[] value6;
    }
}
",
                "contentType": 0
            }
        }]
    }]
}

If you are using TFVC:

  1. Get changeset ID: Request method: GET; URL: [collection url]/_apis/tfvc/changesets?api-version=1.0&$top=1
  2. Update file content: Request method: Post; URL: [collection url]/_apis/tfvc/changesets?api-version=3.0-preview.2; Content Type: application/json;

Json data:

{
    "comment": "Updated Class1.cs",
    "changes": [{
        "changeType": 2,
        "item": {
            "path": "$/Scrum2015/ClassLibraryB/ClassLibraryB/Class1.cs",
            "contentMetadata": {"encoding": 65001},
            "version": [step changeset id]
        },
        "newContent": {
            "content": "using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibraryB
{
    public class Class1
    {
        string sgs = \"\";
        public void T()
        {
            ClassLibraryA.Class1 c = new ClassLibraryA.Class1();
            c.TestOther2();
        }
    }
}
",
            "contentType": 0
        }
    }]
}

Note: You need to parse quotes if file content contains quotes (\”test\”), the same as other special charters.

On the other hand, you can achieve that through vso-node-api, more information, you can refer to this thread: TFS Rest API check-in to Version Control

Update1:

Refer to this code to modify your code:

  var json={
                    "comment": "Updated tt.json",
                    "changes": [{
                        "changeType": 2,
                        "item": {
                            "path": "$/Scrum2015/Buildtest/CoreSolutionDemo/WebApplication1/tt.json",
                            "contentMetadata": { "encoding": 65001 },
                            "version": 754
                        },
                        "newContent": {
                            "content": "[ {\"hello\" : \"Test2\"} ]",
                            "contentType": "RawText"
                        }
                    }]
                };
                $.ajax({
                    type: 'POST',
                    url: 'https://XXX.visualstudio.com/_apis/tfvc/changesets?api-version=3.0-preview.2',
                    contentType: 'application/json',
                    data: JSON.stringify(json),
                    cache: false,
                    dataType: 'json',
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("Authorization", "Basic " + btoa("name" + ":" + "password or PAT"));
                    },
                }).done(function (data) {
                    var s1 = "ss";

                }).error(function (e) {
                    var s = "ss";
                });
            })
Community
  • 1
  • 1
starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
  • Thank you for your answer, so far I have it all set up but when I run it I am getting a 400 error. I am using TFVC and I am using ajax...I put the json data under the data: in my ajax call as well. Do you have any idea what I could be doing wrong. – D.Grace Jan 19 '17 at 17:22