0
votes

I am rendering extension on the Work item page using

<WebpageControlOptions AllowScript="true" ReloadOnParamChange="true">
  <Link UrlRoot="http://.../extension/Validate-extension/1.0.69/assetbyname/workItemNotifications.html"/>
</WebpageControlOptions>

enter image description here

Following is the html/js code:

 var workItemID = 0;
   VSS.init({
            explicitNotifyLoaded: true,
            usePlatformScripts: true
        });
 VSS.ready(function () {
            var currentContext = VSS.getWebContext();
            VSS.register(VSS.getContribution().id, function (context) {
                return {
                    // event handlers, called when the active work item is loaded/unloaded/modified/saved
                    onFieldChanged: function (args) {
                        if (!changedFields[args.id]) {
                            changedFields[args.id] = [];
                            changedFieldCount[args.id] = 0;        
                        }
                        $.each(args.changedFields, function (key, value) {
                            if (!changedFields[args.id][key]) {                              
                                changedFields[args.id][key] = value;
                                changedFieldCount[args.id]++;
                            }
                        });                     
                    },
                    onLoaded: function (args) {
                        console.log("OnloadNotification");
                        VSS.require(["TFS/WorkItemTracking/Services"], function (workItemServices) {
                            workItemServices.WorkItemFormService.getService().then(function (workItemFormSvc) {
                                if (workItemFormSvc.hasActiveWorkItem()) {
                                    console.log("Active work item is available.");
                                    workItemFormSvc.getFieldValues(["System.Id"]).then(
                                        function (value) {
                                            var val = JSON.stringify(value);
                                            $.each(value, function (key, values) {
                                                 if(key == "System.Id"){
                                                    workItemID = values;                                                                          
                                                }                                                
                                            });
                                        });                                  
                                }
                                else {
                                    console.log("Active work item is NOT available.");
                                }
                            });
                        });
                    },
                    onUnloaded: function (args) {
                    },
                    onSaved: function (args) {
                        changedFieldCount[args.id] = 0;
                        changedFields[args.id] = [];                        
                    },
                    onReset: function (args) {
                        changedFieldCount[args.id] = 0;
                        changedFields[args.id] = [];
                    },
                    onRefreshed: function (args) {
                        changedFieldCount[args.id] = 0;
                        changedFields[args.id] = [];
                    }
                };
            });            
            VSS.notifyLoadSucceeded();
        });
       $(document).ready(function () {
            $("#btnValidate").click(function () {
                var getResponse = ValidateUser();                
                 VSS.require(["TFS/WorkItemTracking/Services"], function (_WorkItemServices) {
                     var wiServiceNew = _WorkItemServices.WorkItemFormService.getService();                                          
                     wiServiceNew.setFieldValue("System.Title", "Title set from your group extension!");
                 });
            });
        });

2 things which I am trying to achieve

  1. After button click event to validate user, I have to access the Work Item fields after successful validation. Unable to access _WorkItemServices. Not able to to get the Work Item fields.
  2. When I set workItemID variable OnLoad event, it resets to 0 when a tab is clicked, value is not getting retained which is set OnLoad.
1

1 Answers

0
votes

You may try to interact with the IWorkItemFormService service. For example:

import {
  IWorkItemChangedArgs,
  IWorkItemFieldChangedArgs,
  IWorkItemFormService,
  IWorkItemLoadedArgs,
  WorkItemTrackingServiceIds
} from "azure-devops-extension-api/WorkItemTracking";

Check the sample here:

https://github.com/microsoft/azure-devops-extension-sample/blob/master/src/Samples/WorkItemFormGroup/WorkItemFormGroup.tsx