PowerShell TFS 2013 API #2 – Adding to a GlobalList

Audience

Everyone

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 geting 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:

function Add-TfsGlobalListItem {
    Param(
        [parameter(Mandatory=$true)][Microsoft.TeamFoundation.Client.TfsTeamProjectCollection] $TfsCollection,
        [parameter(Mandatory=$true)][String] $GlobalListName,
        [parameter(Mandatory=$true)][String] $GlobalEntryValue
        )
    # Get Global List
    $store = Get-TfsWorkItemStore $TfsCollection
    [xml]$export = $store.ExportGlobalLists();

    $globalLists = $export.ChildNodes[0];
    $globalList = $globalLists.SelectSingleNode("//GLOBALLIST[@name='$GlobalListName']")

    # if no GL then add it
    If ($globalList -eq $null)
    {
        $globalList = $export.CreateElement("GLOBALLIST");
        $globalListNameAttribute = $export.CreateAttribute("name");
        $globalListNameAttribute.Value = $GlobalListName
        $globalList.Attributes.Append($globalListNameAttribute);
        $globalLists.AppendChild($globalList);
    }

    #Create a new node.
    $GlobalEntry = $export.CreateElement("LISTITEM");
    $GlobalEntryAttribute = $export.CreateAttribute("value");
    $GlobalEntryAttribute.Value = $GlobalEntryValue
    $GlobalEntry.Attributes.Append($GlobalEntryAttribute);

    #Add new entry to list
    $globalList.AppendChild($GlobalEntry)
    # Import list to server
    $store.ImportGlobalLists($globalLists)
}

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.

Upcoming Training Opportunities

These are the next five classes we have, and you can check out our full public schedule of classes.

Live Virtual Applying Professional Scrum Minecraft on  12th December 2022
Virtual Beginner 0
12 - 15 Dec, 2022
14:00-17:30 GMT
Live Virtual Applying Professional Scrum Online in Minecraft on 30th January 2023
Virtual Beginner 12
30 Jan - 2 Feb, 2023
09:30-13:00 GMT

We can deliver any of our courses as private in-house training over Microsoft Teams & Mural. We also recommend training based on your accountabilities or role, you can go directly to recommended courses for Scrum MastersProduct OwnersDevelopers and Agile Leaders.

Create a conversation around this article

Share on Facebook
Share on Twitter
Share on Linkdin

Related Courses

No items found

Read more

Martin Hinshelwood (He/Him) nkdAgility.com
To my understanding there is a frustrating misunderstanding of reality when one thinks that the Product Owner can reject a single story at the Sprint Review. This is the fallacy of the rejected backlog item.
Martin Hinshelwood (He/Him) nkdAgility.com
There is no such thing as an Agile Transformation, Digital Transformation, DevOps Transformation, or any of the Whatever Transformation that you can think of or have been sold. You can’t buy agility, and you certainly can’t install it. There is no end state, no optimal outcome, No best practices. We …
Martin Hinshelwood (He/Him) nkdAgility.com
In light of the new normal and the last 20 years of technological progress, we need to re-define co-location as we no longer need to be in the same room as each other to get the 80% of communication that is non-verbal. If we are participating in an online event, …
Martin Hinshelwood (He/Him) nkdAgility.com
Gathering the metrics for Evidence-based Management in software organisations can be a strenuous task and I have a number of customers that are fretting on what to collect and from where. Here I try to create an understanding of the ‘what’ that we need to collect.

OUR TEAM

We believe that every company deserves high quality software delivered on a regular cadence that meets its customers needs. Our goal is to help you reduce your cycle time, improve your time to market, and minimise any organisational friction in achieving your goals.

naked Agility Limited is a professional company that offers training, coaching, mentoring, and facilitation to help people and teams evolve, integrate, and continuously improve.

We recognise the positive impact that a happy AND motivated workforce, that has purpose, has on client experience. We help change mindsets towards a people-first culture where everyone encourages others to learn and grow. The resulting divergent thinking leads to many different ideas and opportunities for the success of the organisation.