TFS Auto Complete Fields

In TFS you are able to customise your process template and one of the rules you can add to a field is a SuggestedValues rule, with this you can specify many single items for suggestions or/and get the suggestion items from a global list. This works great for if you have a set list of suggestions, but what if you want to pull your suggestion list based off data in TFS.

Assumptions

Reading this we are assuming that you have a understanding of the basics of customising a process template.

Create the Magic

Add a field

Add a new field to any work item called The Auto Completing Field. Make it’s reference name be Fields.The.Auto.Completing.Field. Leave the type as string and import the work item definition into TFS.

Create a Team Query

Browse to any of the interfaces where you can create a flat query. Start a new Query,

image

And for this example create your query as below.

image

For Column options select just the “The Auto Completing Field” field, and in the sort columns select the same column. Save that query in the root of Shared queries.

Creating the server plugin

The plugin you will create is very simple and you will be able to use the source attached directly. Basically what you are going to do is whenever the configured fields are changed then you will call the query mapped to that field and update a global list which you will add as a suggested list for your field after the global list is created.

Code

The code that does the logic magic is below and all the classes being called are in the source download

Uri requestUri = GetTFSUri(requestContext);

foreach (StringField field in workItemChangedEvent.ChangedFields.StringFields)
{
    if (DoesFieldNameExistsInConfiguredFields(configuredFreeTextFields, field.ReferenceName))
    {
        IGlobalList globalList = GlobalListFactory.GetGlobalList(requestUri, _configuredFreeTextAutoCompleteGobalListPrefix + field.ReferenceName.Replace(".", "_"));
        globalList.ClearList();
        IQueryRunner queryRunner = QueryRunnerFactory.CreateInstance(requestUri, workItemChangedEvent.PortfolioProject);

        WorkItemCollection workItemsFromQuery = queryRunner.ExecuteSavedQuery(configuredFreeTextFields[field.ReferenceName]);
        foreach (WorkItem wi in workItemsFromQuery)
        {
            if (wi.Fields.Contains(workItemsFromQuery.DisplayFields[0].Name))
            {
                globalList.AddToListDistinct(wi.Fields[workItemsFromQuery.DisplayFields[0].Name].Value.ToString());
            }
        }
        globalList.SaveChanges(true);
    }
}

The bits of code around the above code load configured fields from the app settings. A default example of what the config would look like for the field created in this article is below.




Final Touches

The server plugin should create the global list for you if it doesn’t exist, so if you go into TFS and alter the “The Auto Completing Field” then the global list for the field will be created, alternatively if you are using the default configuration you can import a global list upfront called AutoComplete_Fields_The_Auto_Completing_Field.

Basically after the global list is in TFS (no matter which method you use to get it there), you need to go back to your work item definition and add the SuggestedValues rule that uses the global list created as it values.

Publish the server plugin and import the changes made to the work item definition and we’re done =)

Enjoy

This plugin allows you to give users the flexibility to use free text fields and by making a auto complete type experience helps a bit to not have multiple values for the same thing that will later make accurate reporting slightly tougher.

Source

Download

Buffer