Stefan
Stefan

Reputation: 720

How to get ALL fields of a WorkItem?

I'm trying to retrieve the Risks field from the tabs in TFS, however, when I print all the Fields, I can't see the Risks .

I've tried accessing it directly via WorkItem.Fields["FieldName"] butno luck.

Any ideas?

Upvotes: 1

Views: 3717

Answers (2)

KochFolie
KochFolie

Reputation: 316

I am a bit late, I guess, but since it might still help somebody, I'm going to post this anyways. Even if you haven't specified, if you're in the frontend or backend.

tl;dr: try to omit the fields parameter in the request.

Background: I wanted to provide more workitem-details in the pull requests details-view, so I created a userscript for TamperMonkey. That means I don't have "direct" access to TFS, since I am only accessing the frontend via JavaScript.

Like you, I also noticed that TFS doesn't output all fields. To solve that, I then modified the TFS ajax request with jQuery to omit the fields-parameter. Then TFS started returning all existing fields for the work item.

I found the info in the TFS documentation for work-items

fields (string)
A comma-separated list of up to 100 fields to get with each work item. If not specified, all fields are returned.

In case that actually is your use-case, I am also providing the script that I wrote to modify the ajax request:

// by Joel Richard -> http://stackoverflow.com/a/26849194/4524280
function parseParams(str) {
    return str.split('&').reduce(function (params, param) {
        var paramSplit = param.split('=').map(function (value) {
            return decodeURIComponent(value.replace('+', ' '));
        });
        params[paramSplit[0]] = paramSplit[1];
        return params;
    }, {});
}

$.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
    // Modify ajax request to return all fields... definitely not a hack :D
    if(options && options.url && options.url.indexOf('_apis/wit/workItems') >= 0) {
        var parsedData = parseParams(options.data);
        delete parsedData.fields;
        options.data = $.param(parsedData);
    }
});

$(document).ajaxComplete(function(event, request, settings) {
    // trigger after ajax is complete to get values
    if(settings && settings.url && settings.url.indexOf('_apis/wit/workItems') >= 0 && request.responseJSON) {
        var workItemsData = request.responseJSON.value;
        // -> workItemsData.fields contains all existing fields
    }
});

Just for the protocol: I don't think anyone should use $.ajaxPrefilter in "normal" use-cases, but in this case, I didn't have much options available.

Upvotes: 0

PatrickLu-MSFT
PatrickLu-MSFT

Reputation: 51183

You can use WIQL Queries to get the values of all fields. Here is a list of all Work item field index . Below is a sample with how to get all work items and all fields for a particular project:

using Microsoft.TeamFoundation.WorkItemTracking.Client;

Query query = new Query(
     workItemStore, 
     "select * from issue where System.TeamProject = @project",
     new Dictionary<string, string>() { { "project", project.Name } }
);

var workItemCollection = query.RunQuery();
foreach(var workItem in workItemCollection) 
{
   /*Get work item properties you are interested in*/
   foreach(var field in workItem.Fields)
   {
      /*Get field value*/
      info += String.Format("Field name: {0} Value: {1}\n", field.name, field.Value);
   }
}

Upvotes: 1

Related Questions