简体   繁体   中英

javascript valid patch document error

Trying to create a work item in VSTS I success. But when I want to also give values for the field System.AreaPath I am failing.

My code (working & without System.AreaPath):

  var number = "1234";
var title = "Test Title";
var caller = "Test Caller";


var assignment_group = "Test Group";
var tags = "Bug";
var area = "TestProject1\\Area 51";
var assigned_to = "Test To";



        var url = "https://[instance].visualstudio.com/DefaultCollection/TestProject1/_apis/wit/workitems/$Bug?api-version=2.2";
        var request = new sn_ws.RESTMessageV2();
        request.setEndpoint(url);
        request.setHttpMethod('PATCH');

        var password = 'somePassword';

        request.setBasicAuth(password);
        request.setRequestHeader("Accept","application/json");
        request.setRequestHeader('Content-Type','application/json-patch+json');
        request.setRequestBody("[{\"op\":\"add\",\"path\":\"/fields/System.Tags\",\"value\":\""+tags+"\"},{\"op\":\"add\",\"path\":\"/fields/System.Title\",\"value\":\"[" + number + "] " + title + "\"},{\"op\":\"add\",\"path\":\"/fields/Microsoft.VSTS.TCM.SystemInfo\",\"value\":\"Assignment Group: "+assignment_group+"\nCaller: "+caller+"\nAssigned to: "+assigned_to+"\"}]");
        var response = request.execute();

// {\"op\":\"add\",\"path\":\"/fields/System.AreaPath\",\"value\":\""+area+"\"}

Code including {\\"op\\":\\"add\\",\\"path\\":\\"/fields/System.AreaPath\\",\\"value\\":\\""+area+"\\"}

var number = "1234";
var title = "Test Title";
var caller = "Test Caller";


var assignment_group = "Test Group";
var tags = "Bug";
var area = "TestProject1\\Area 51";
var assigned_to = "Test To";



        var url = "https://[instance].visualstudio.com/DefaultCollection/TestProject1/_apis/wit/workitems/$Bug?api-version=2.2";
        var request = new sn_ws.RESTMessageV2();
        request.setEndpoint(url);
        request.setHttpMethod('PATCH');

        var password = 'somePassword';

        //,{\"op\":\"add\",\"path\":\"/fields/Microsoft.VSTS.Common.Priority\",\"value\":\"" + priority + "\"}
        request.setBasicAuth(password);
        request.setRequestHeader("Accept","application/json");
        request.setRequestHeader('Content-Type','application/json-patch+json');
        request.setRequestBody("[{\"op\":\"add\",\"path\":\"/fields/System.AreaPath\",\"value\":\""+area+"\"},{\"op\":\"add\",\"path\":\"/fields/System.AreaPath\",\"value\":\""+area+"\"},{\"op\":\"add\",\"path\":\"/fields/System.Tags\",\"value\":\""+tags+"\"},{\"op\":\"add\",\"path\":\"/fields/System.Title\",\"value\":\"[" + number + "] " + title + "\"},{\"op\":\"add\",\"path\":\"/fields/Microsoft.VSTS.TCM.SystemInfo\",\"value\":\"Assignment Group: "+assignment_group+"\nCaller: "+caller+"\nAssigned to: "+assigned_to+"\"}]");
        var response = request.execute();

// {\"op\":\"add\",\"path\":\"/fields/System.AreaPath\",\"value\":\""+area+"\"}

giving error:

{"$id":"1","innerException":null,"message":"You must pass a valid patch document in the body of the request.","typeName":"Microsoft.VisualStudio.Services.Common.VssPropertyValidationException, Microsoft.VisualStudio.Services.Common, Version=14.0.0.0, Culture=neutral, PublicKeyToken=hidden","typeKey":"VssPropertyValidationException","errorCode":0,"eventId":3000}

What am I missing?

Replace var area = "TestProject1\\\\Area 51"; to var area = "TestProject1\\\\\\\\Area 51"; .

By the way, you specified multiple area keys in the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM