0

I need to create VSTS work items using Visual Studio Team Services Client for Node.js (vso-node-api), Please provide any samples on this?

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Bandara
  • 780
  • 8
  • 29

2 Answers2

5

I created a simple code sample to get and create work item with it for your reference, see following section for details:

/// <reference path="typings/index.d.ts" />

import * as vm from 'vso-node-api/WebApi';
import * as wa from 'vso-node-api/WorkItemTrackingApi';
import * as wi from 'vso-node-api/interfaces/WorkItemTrackingInterfaces';
import * as vss from 'vso-node-api/interfaces/Common/VSSInterfaces';

var collectionUrl = "https://xxxxxx.visualstudio.com";

let token: string = "Yourpersonalaccesstoken";

let creds = vm.getPersonalAccessTokenHandler(token);

var connection = new vm.WebApi(collectionUrl, creds); 

let vstsWI: wa.IWorkItemTrackingApi = connection.getWorkItemTrackingApi();

async function getWI() {
    let wiid: number = 1;
    let workitem: wi.WorkItem = await vstsWI.getWorkItem(wiid);

    console.log(workitem.url);
}

getWI();

async function createWI() {
    let wijson: vss.JsonPatchDocument = [{ "op": "add", "path": "/fields/System.Title", "value": "Task created from Node JS" }];
    let project: string = "Project";
    let witype: string = "Task";
    let cWI: wi.WorkItem = await vstsWI.createWorkItem(null, wijson, project, witype);
    console.log(cWI.id);
}

createWI();
Eddie Chen - MSFT
  • 29,708
  • 2
  • 46
  • 60
  • Thanks very much for the response Eddie, My project uses ES5 version, so I'm getting error when I add the vso-node-api node package and build to ES5, It seems 'Promise' is not available in ES5, Do you know any workaround for this? – Bandara Sep 22 '16 at 15:12
  • @BandR No, I don't know any workaround for this. Sorry. – Eddie Chen - MSFT Sep 23 '16 at 01:10
  • I'm getting unauthorized 401 error even when valid credentials are entered, Any idea why? "Error: Failed Request: Unauthorized(401) - \n at processResponse (c:\vso-test\node_modules\vso-node-api\RestClient.js:59:18)\n at c:\vso-test\node_modules\vso-node-api\RestClient.js:147:13\n at HttpClient.request.callback (c:\vso-test\node_modules\vso-node-a... – Bandara Sep 23 '16 at 12:14
  • @BandR What credential did you use? – Eddie Chen - MSFT Sep 24 '16 at 02:02
  • I used the token from below, it worked 'let auth = tl.getEndpointAuthorization("SYSTEMVSSCONNECTION", false);' 'var token = auth.parameters["AccessToken"];' – Bandara Sep 26 '16 at 12:04
  • @BandR I use the personal access token. I will try with ur code tomorrow and let u know the result. – Eddie Chen - MSFT Sep 26 '16 at 12:24
  • @BandR I cannot reproduce your issue, the token generated with your code works at my side. – Eddie Chen - MSFT Sep 27 '16 at 00:52
  • HI Eddie, I'm unable to create work items on TFS 2015.2 environment with this code, I have initiated a request in following, [TFS Work Item Creation Issue](http://stackoverflow.com/questions/40147345/work-item-creation-issue-via-rest-api-in-tfs-2015-2?noredirect=1#comment67580763_40147345 ) Can you help me with this? – Bandara Oct 21 '16 at 07:43
0

Use the token retrieved using vsts-task-lib

import tl = require('vsts-task-lib/task');
let auth = tl.getEndpointAuthorization("SYSTEMVSSCONNECTION", false);
var token = auth.parameters["AccessToken"];
riQQ
  • 9,878
  • 7
  • 49
  • 66
Bandara
  • 780
  • 8
  • 29