Deleting (or destroying) a Global List with the TFS API

One of the most used features in TFS Work Items are Global Lists. We use global lists very frequently because it save a lot of time in maintaining list data amongst different Team Projects.

What we also do very often is adding and removing items to and from a global list.

This article from Shai Raiten explains how to do this

http://blogs.microsoft.co.il/blogs/shair/archive/2010/03/08/tfs-api-part-23-create-global-list-xml-way.aspx

Basically it comes to the point that you export the Global List XML, modify the XML and import it back.

However, deleting a Global List is not possible. Because the Import/Export methods are set up in a way  that you can also add or update 1 list instead of everything, you can never delete a list. If you don’t send the XML of the particular list, it is not updated.

So what to do if you want to delete a Global List programmatically?

To delete all Global List you also need the WorkItemStore object. You can get this by getting the service from the TfsTeamProjectCollection object.

   1: TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(""));

   2: WorkItemStore store = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));

Then you need to create a special XML package that you can send to the SendUpdatePackge method of the WorkItemStore object.

   1: string globalListName = "Test Global List";

   2:     

   3: XmlElement returnElement;

   4: XmlDocument root = new XmlDocument();

   5:  

   6: //Create a package element

   7: XmlElement newChild = root.CreateElement("Package");

   8: root.AppendChild(newChild);

   9:  

  10: //Create a Destroy Global List element

  11: XmlElement element2 = root.CreateElement("DestroyGlobalList");

  12: element2.SetAttribute("ListName", "*" + globalListName);

  13: element2.SetAttribute("ForceDelete", true.ToString(CultureInfo.InvariantCulture));

  14: newChild.AppendChild(element2);

  15:  

  16: //Send Update to Work Item Store

  17: store.SendUpdatePackage(newChild, out returnElement, false);

This will do the trick. Your Global List will be deleted !

Happy Deleting !

(Thanks to Mark v/d Berg for this great tip !)

 

Download a working sample here

Advertisements

12 Responses to “Deleting (or destroying) a Global List with the TFS API”

  1. I am unable to find the SendUpdatePackage method. Where is it?

  2. Doesn’t show up in my view. What version of .NET are you using? Could you provide a sample solution?

    • Just download the sample
      Solution from shai raiten’s blog. Link is on top of my blog post. My code uses the same assemblies. I tested it with TFS 2012 assemblies.

      • I’ve downloaded and opened the solution in both VS 2012 and VS 2010 with no luck in either. The solution on Shai Raiten’s blog seems to want the VS2010 assemblies but that method just doesn’t appear. So far this is the only solution I’ve found to programmatically delete lists. Any other help you can provide would mean a lot.

      • I created a sample solution for you. It is downloadable here (http://sdrv.ms/15ExW6p). The link is also in the post. I added the needed references in the lib folder. You only need TeamFoundation.Client and TeamFoundation.WoritemTracking.Client in order to make it work. You can check with the command line witadmin exportgloballist /collection:URI if the delete succeeded. The GUI in Visual Studio does not refresh correctly. Make sure you have Visual Studio 2012 + Team Explorer installed on your machine. The assemblies need to be on your PC as well. I did not modify the code for deleting the Global List. It is exact the code as it is stated in my Blog. Good luck..

  3. It worked! I don’t quite understand what I was doing wrong but I’ve never seen an instance yet where IntelliSense did not show me the method I was looking for. I guess I just assumed that since it was undocumented and not showing up that I was doing something wrong. Thanks again!

  4. Your link is dead/i cannot get the project. And i have the same problem as the other person that posted the problem with SendUpdatePackage. I only have 2010 DLLs on my machine, and i need to be able to code a solution that implements a “DestroyGlobalList” method. Is there a 2010 version of this that would work? I tried using the ImportGlobalLists function with the destroy package in it but it keeps telling me that the elements (Package or DestroyGlobalList) are not valid…

    • Hi,

      I tested the link in both my article and comments and it just works. It redirects to OneDrive and preselects the GlobalListSample.zip. The SendUpdatePackage is in the WorkItemStore object (as I wrote in the comment). The sample solution might give an answer. Since I do not have 2010 installed anywhere I cannot test or guarantee that it works. However, the assemblies that I use are also present in 2010 so there is a chance.

      Just to be sure here is another link. http://1drv.ms/1lqoyMF

      • Thank you. That last link worked. In case you are wondering, 2010 libs don’t have SendUpdatePackage (). But: 2010 has the WorkItemServer service which offers up the Update () method which will take the same definition and do the work properly 🙂

Trackbacks/Pingbacks

  1. Deleting (or destroying) a Global List with the... - August 6, 2013

    […] One of the most used features in TFS Work Items are Global Lists. We use global lists very frequently because it save a lot of time in maintaining list data amongst different Team Projects. What we…  […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: