How do I get the REST client using javascript?

1.4k Views Asked by At

Visual studios has the following typescript code on their website that I was trying to follow as an example:

import RestClient = require("TFS/WorkItemTracking/RestClient");
 // Get an instance of the client
var client = RestClient.getClient();
// Call a method on the client
// e.g. client.getResource(...).then(...);

Part of the javascript code I have is:

VSS.init({
        explicitNotifyLoaded: true,
        usePlatformStyles: true
    });
  VSS.require([
      "VSS/Controls",
      "VSS/Controls/Menus",
      "VSS/Controls/Dialogs",
      "VSS/Service",
      "TFS/WorkItemTracking/RestClient",
      "VSS/WebApi/Contracts"],
      function (VSS_Service, TFS_Wit_WebApi) {
          //get the REST client
          var witClient = VSS_Service.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient);
      }
  );

However when I do this I get an error that says that VSS_Service.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient) is not a function. Do I need to initialize VSS_Service other than the VSS.require(["VSS/Service"]?

2

There are 2 best solutions below

0
On BEST ANSWER

I made witClient and outside variable so I could use it anywhere in my program, and added Controls and Grids to the function which worked.

var witClient;
  VSS.init({
        explicitNotifyLoaded: true,
        usePlatformScripts: true, 
        usePlatformStyles: true
    });
  VSS.require([
      "VSS/Controls", "VSS/Controls/Grids",
      "VSS/Service", "TFS/WorkItemTracking/RestClient"],
      function (Controls, Grids, VSS_Service, TFS_Wit_WebApi) {
          //get the REST client
          witClient = VSS_Service;//.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient);
          witClient = witClient.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient);
      }
  );
1
On

It seems your code is correct, just not use RestClient as a var, try to modify

var RestClient = VSS_Service.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient);

to

var witClient = VSS_Service.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient);

Useful article: https://www.visualstudio.com/en-us/docs/integrate/extensions/get-started/node