a·gen·tic a·gil·i·ty

PowerShell TFS 2013 API #2 - Adding to a GlobalList

Learn how to use PowerShell and the TFS 2013 API to automate adding items to a GlobalList by exporting, editing, and re-importing global lists as XML.

Published on
3 minute read
Image
https://nkdagility.com/resources/Y2XTGIaY_Os
Subscribe

Using the TFS 2013 API along with a little PowerShell we can add a ‘team field’ to our global list.

I have been working a lot with PowerShell recently and I have been stuck by its flexibility even when calling standard .NET API’s.  You should start with g eting the TFS Collection which will give you basic connectivity and imports required to get started. If we want to use ’team field’ we may want to automate some of the activities that we need to make it happen slickly. You will have created a Global List for your ’team field’ and you will want to add new entries. You can add them manually, or you can hit the TFS API to give you a leg up…

In order to add an entry to a global list we unfortunately need to export all of the global lists locally as XML, edit it and then upload it back in. I have been trying to create as many reusable functions as possible in my PowerShell exploits and I am building up a rather hearty set of components. I have not yet figured out how to create reusable components that can be easily imported but I have figured out functions:

 1function Add-TfsGlobalListItem {
 2    Param(
 3        [parameter(Mandatory=$true)][Microsoft.TeamFoundation.Client.TfsTeamProjectCollection] $TfsCollection,
 4        [parameter(Mandatory=$true)][String] $GlobalListName,
 5        [parameter(Mandatory=$true)][String] $GlobalEntryValue
 6        )
 7    # Get Global List
 8    $store = Get-TfsWorkItemStore $TfsCollection
 9    [xml]$export = $store.ExportGlobalLists();
10
11    $globalLists = $export.ChildNodes[0];
12    $globalList = $globalLists.SelectSingleNode("//GLOBALLIST[@name='$GlobalListName']")
13
14    # if no GL then add it
15    If ($globalList -eq $null)
16    {
17        $globalList = $export.CreateElement("GLOBALLIST");
18        $globalListNameAttribute = $export.CreateAttribute("name");
19        $globalListNameAttribute.Value = $GlobalListName
20        $globalList.Attributes.Append($globalListNameAttribute);
21        $globalLists.AppendChild($globalList);
22    }
23
24    #Create a new node.
25    $GlobalEntry = $export.CreateElement("LISTITEM");
26    $GlobalEntryAttribute = $export.CreateAttribute("value");
27    $GlobalEntryAttribute.Value = $GlobalEntryValue
28    $GlobalEntry.Attributes.Append($GlobalEntryAttribute);
29
30    #Add new entry to list
31    $globalList.AppendChild($GlobalEntry)
32    # Import list to server
33    $store.ImportGlobalLists($globalLists)
34}

Figure: Adding to a GlobalList with PowerShell

Here you can see that we are first getting the Work Item Store service, which is where all of the magic around Work Item Tracking occurs. Once we have that we need to export the XML using the “ExportGlobalLists” (#9) method which effectively just pucks up the entire XML tree for the global lists. We can then parse and edit it like any other piece of XML. We can find the list that we want, as all of the lists are exported, using a little XPath (#11)  and determine wither the required global list even exists. If it does not then my script goes ahead and adds one (#14-21) so that we don’t get an error. If this is the first time that you are added and element to a list it only makes sense that you would want the list to exist so creating it is not a stretch.

Once we have the list, wither it is a new or existing one, we can go ahead and create and add the new element (#24-27.) Once we have everything in place we can import the entire set of global lists back into the server using the Import method.

Software Development
Subscribe

Related Blog

No related videos found.

Connect with Martin Hinshelwood

If you've made it this far, it's worth connecting with our principal consultant and coach, Martin Hinshelwood, for a 30-minute 'ask me anything' call.

Our Happy Clients​

We partner with businesses across diverse industries, including finance, insurance, healthcare, pharmaceuticals, technology, engineering, transportation, hospitality, entertainment, legal, government, and military sectors.​

Epic Games Logo

Epic Games

MacDonald Humfrey (Automation) Ltd. Logo

MacDonald Humfrey (Automation) Ltd.

Kongsberg Maritime Logo

Kongsberg Maritime

Jack Links Logo

Jack Links

Sage Logo

Sage

Ericson Logo

Ericson

Capita Secure Information Solutions Ltd Logo

Capita Secure Information Solutions Ltd

Milliman Logo

Milliman

Freadom Logo

Freadom

Akaditi Logo

Akaditi

Workday Logo

Workday

Healthgrades Logo

Healthgrades

Boeing Logo

Boeing

Slaughter and May Logo

Slaughter and May

Emerson Process Management Logo

Emerson Process Management

Lean SA Logo

Lean SA

Schlumberger Logo

Schlumberger

Big Data for Humans Logo

Big Data for Humans

New Hampshire Supreme Court Logo

New Hampshire Supreme Court

Royal Air Force Logo

Royal Air Force

Nottingham County Council Logo

Nottingham County Council

Ghana Police Service Logo

Ghana Police Service

Washington Department of Transport Logo

Washington Department of Transport

Department of Work and Pensions (UK) Logo

Department of Work and Pensions (UK)

MacDonald Humfrey (Automation) Ltd. Logo

MacDonald Humfrey (Automation) Ltd.

Hubtel Ghana Logo

Hubtel Ghana

Xceptor - Process and Data Automation Logo

Xceptor - Process and Data Automation

Akaditi Logo

Akaditi

Graham & Brown Logo

Graham & Brown

Brandes Investment Partners L.P. Logo

Brandes Investment Partners L.P.