0

I am writing a VSTS dashboard widget used for Work Item Tracking

However I am running into a problem when using the getWorkItem() function. I want to get the ids of all the Features under a given Epic (I already know the epic ID). I am confident that if I set the expand paremeter of getWorkItem() to "All" I will get a list of all the Features and their respective ids. Unfortunately I do not know how to define the "type" of expand parameter and how to properly pass it as a value to the getWorkItem() function.

Here is my code:

VSS.require(["VSS/Service", "TFS/Dashboards/WidgetHelpers", "TFS/WorkItemTracking/RestClient"],
        function (VSS_Service, WidgetHelpers, TFS_Wit_WebApi) {
            WidgetHelpers.IncludeWidgetStyles();
            VSS.register("myapp", function () {
                var fetchData = function (widgetSettings) {
                    const epicID = 123456;
                    // Get a WIT client to make REST calls to VSTS
                    return VSS_Service.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient).getWorkItem(123456, null, null, All).
                        then(
                            //Successful retrieval of workItems
                            function (workItems) {
                                $('#myText').text(JSON.stringify(workItems));
                                console.log(workItems);
                                // Use the widget helper and return success as Widget Status
                                return WidgetHelpers.WidgetStatusHelper.Success();
                            },
                            function (error) {
                                // Use the widget helper and return failure as Widget Status
                                return WidgetHelpers.WidgetStatusHelper.Failure(error.message);
                            });
                }

Here is the VSTS reference for expand It explains what the values can be, but doesn't say how to pass it into the getWorkItem() function.

I would like to set the optional expand parameter of the function to "All" but don't know its type and how to properly define and use it.

Community
  • 1
  • 1
user8463863
  • 187
  • 1
  • 4
  • 19

2 Answers2

0

Based on the source code, it is the enum, so you can specify the integer (e.g. 4) in getWorkItem function.

export enum WorkItemExpand {
    None = 0,
    Relations = 1,
    Fields = 2,
    Links = 3,
    All = 4,
}
starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
0

Using the enum is good, but you can also pass in the value from the 'TFS/WorkItemTracking/Contracts' module. You can find the reference here (shows the module path, the 'class', and the enums):

The link above comes from the TFS WorkItemTracking API Reference, which can be found here:

Here's how you can add it in your code:

  1. Declare the 'TFS/WorkItemTracking/Contracts' module
  2. Pass the module into the callback ('_Contracts' in the example below)
  3. Use '_Contracts' as needed

Here is your code, updated to use the Contracts module:

VSS.require([
    "VSS/Service", 
    "TFS/Dashboards/WidgetHelpers", 
    "TFS/WorkItemTracking/RestClient",
    "TFS/WorkItemTracking/Contracts"],
    function (VSS_Service, WidgetHelpers, TFS_Wit_WebApi, _Contracts) {

    WidgetHelpers.IncludeWidgetStyles();
    VSS.register("myapp", function () {
        var fetchData = function (widgetSettings) {
            const epicID = 123456;
            // Get a WIT client to make REST calls to VSTS
            return VSS_Service.getCollectionClient(TFS_Wit_WebApi.WorkItemTrackingHttpClient).
                getWorkItem(123456, null, null, _Contracts.WorkItemExpand.All).
                then(
                    //Successful retrieval of workItems
                    function (workItems) {
                        $('#myText').text(JSON.stringify(workItems));
                        console.log(workItems);
                        // Use the widget helper and return success as Widget Status
                        return WidgetHelpers.WidgetStatusHelper.Success();
                    },
                    function (error) {
                        // Use the widget helper and return failure as Widget Status
                        return WidgetHelpers.WidgetStatusHelper.Failure(error.message);
                    });
        }
    });
});

Hope that helps!

Nathan Bills
  • 93
  • 1
  • 5