TFS API Part 23 – Create Global List (Xml Way)

08/03/2010

TFS API Part 23 – Create Global List (Xml Way)

image

Download Demo Project

Step 1: Create Project and Add Reference

Create an WPF/WinForm application and add the following references:
     Microsoft.TeamFoundation.WorkItemTracking.Client.dll
(C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.WorkItemTracking.Client.dll)
     Microsoft.TeamFoundation.Client.dll
(C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.Client.dll)

Step 2: Connect to Team Foundation Server

TeamProjectPicker tpp = new TeamProjectPicker(TeamProjectPickerMode.NoProject, false);

tpp.ShowDialog();

 

if (tpp.SelectedTeamProjectCollection != null)

{

    gl_group.IsEnabled = true;

    TfsTeamProjectCollection tfs = tpp.SelectedTeamProjectCollection;

    tfs.EnsureAuthenticated();

    this.store  = (WorkItemStore)tfs.GetService(typeof (WorkItemStore));

}

Step 3: Create GlobalList class

There is two ways for creating a GlobalList using TFS API,

1. String list looks like that: (you need to add header manually)

//builder.AppendLine("<LISTITEM value=’Lorem Ipsum 1′ />");

//builder.AppendLine("<LISTITEM value=’Lorem Ipsum 2′ />");

//builder.AppendLine("<LISTITEM value=’Lorem Ipsum 3′ />");

//builder.AppendLine("<LISTITEM value=’Lorem Ipsum 4′ />");

//builder.AppendLine("<LISTITEM value=’Lorem Ipsum 5′ />");

//builder.AppendLine("<LISTITEM value=’Lorem Ipsum 6′ />");

2. Xml Document (I used this option), this will allow you more easily to add quotes and couple more char that can be difficult to add in string format.

public GlobalList(string listname,WorkItemStore store)

{

    doc = new XmlDocument();

    CreateGlobalListHeaders(listname);

    this.store = store;

}

To make sure there is not encoding problem add ProcessingInstruction to the xml document.

private void CreateGlobalListHeaders(string listname)

{

    //Define encoding for non english languages

    XmlProcessingInstruction newPI;

    newPI = doc.CreateProcessingInstruction(ProcessingInstructionTarget, ProcessingInstructionData);

 

    doc.AppendChild(newPI);

    XmlElement GlobalRoot = (XmlElement)doc.CreateElement("gl", "GLOBALLISTS",

        "http://schemas.microsoft.com/VisualStudio/2005/workitemtracking/globallists");

    ValueList = (XmlElement)doc.CreateElement("GLOBALLIST");

    //Set global list name

    ValueList.SetAttribute("name", listname);

 

    doc.AppendChild(GlobalRoot);

    GlobalRoot.AppendChild(ValueList);

}

After you finish adding the values you will use WorkItemStore object to import the GlobalList

public bool Save()

{

    try

    {

        store.ImportGlobalLists(doc.InnerXml);

        return true;

    }

    catch (Exception ex)

    {

        throw new ArgumentException(ex.Message);

    }

}

 

image

image

Download Demo Project

Full GlobalList Class Code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml;

using Microsoft.TeamFoundation.WorkItemTracking.Client;

 

namespace TFSAPI_GlobalList

{

    public class GlobalList

    {

        private XmlDocument doc;

        private XmlElement ValueList;

        private WorkItemStore store;

 

        private const string ProcessingInstructionData = "version=’1.0′ encoding=’utf-8′";

        private const string ProcessingInstructionTarget = "xml";

 

        public GlobalList(string listname, WorkItemStore store)

        {

            doc = new XmlDocument();

            CreateGlobalListHeaders(listname);

            this.store = store;

        }

 

        public void AddValue(string value)

        {

            XmlElement Item = (XmlElement)doc.CreateElement("LISTITEM");

            Item.SetAttribute("value", value);

            ValueList.AppendChild(Item);

        }

 

        public bool Save()

        {

            try

            {

                store.ImportGlobalLists(doc.InnerXml);

                return true;

            }

            catch (Exception ex)

            {

                throw new ArgumentException(ex.Message);

            }

        }

 

        private void CreateGlobalListHeaders(string listname)

        {

            //Define encoding for non english languages

            XmlProcessingInstruction newPI;

            newPI = doc.CreateProcessingInstruction(ProcessingInstructionTarget, ProcessingInstructionData);

 

            doc.AppendChild(newPI);

            XmlElement GlobalRoot = (XmlElement)doc.CreateElement("gl", "GLOBALLISTS",

                "http://schemas.microsoft.com/VisualStudio/2005/workitemtracking/globallists");

            ValueList = (XmlElement)doc.CreateElement("GLOBALLIST");

            //Set global list name

            ValueList.SetAttribute("name", listname);

 

            doc.AppendChild(GlobalRoot);

            GlobalRoot.AppendChild(ValueList);

        }

    }

}

 

Enjoy

Add comment
facebook linkedin twitter email

Leave a Reply