Automatically merge work item type modifications into existing team projects – part 3

by Ewald Hofman 21. July 2009 04:42

When you are working in an enterprise that has a lot of team projects, it is hard to make generic modifications to the work item types. Imagine that you want to add a field to all work item types in all team projects, first in the staging environment, then in the production environment. This means a lot of modifications!

Fortunately it is possible to automate this process. A work item type is nothing more then xml, what you can modify. This post shows you how you can automate this process. The post is divided into multiple parts:

  • Part 1: Download and upload the work item type from/to the TFS server
  • Part 2: Make the modifications to the work item type
  • Part 3: Add new elements to the work item type

In the first part, the base library is created to be able to download and upload the work item types from/to the TFS server. The second part added the ability to modify the attribute values of existing work item type elements. This part shows how you can enable adding new elements to work item types.

  1. Add a new class to the MergeWitModifications project and rename it to XPathAddition.cs.
  2. Add the following code to the class
  3. namespace MergeWitModifications
    {
        public class XPathAddition
        {
            /// <summary>
            /// The class that holds the name/value pair information for an attribute
            /// that is added to the node that will be added to the work item type.
            /// </summary>
            public struct Attribute
            {
                public string Name;
                public string Value;
            }
     
            /// <summary>
            /// The xPath of the parent where the node will be placed under.
            /// </summary>
            public string XPath { get; set; }
     
            /// <summary>
            /// The name of the new node.
            /// </summary>
            public string Name { get; set; }
     
            /// <summary>
            /// The list of name/value pairs that will be added to the new node.
            /// </summary>
            public Attribute[] Attributes { get; set; }
     
            /// <summary>
            /// Constructor.
            /// </summary>
            /// <param name="xPath">The xPath of the parent where the node will be placed under.</param>
            /// <param name="name">The name of the new node.</param>
            /// <param name="attributes">The list of name/value pairs that will be added to the new node.</param>
            public XPathAddition(string xPath, string name, Attribute[] attributes)
            {
                XPath = xPath;
                Name = name;
                Attributes = attributes;
            }
        }
    }
  4. Navigate to the Modifications class and replace the contents of the class to
  5. using System.Collections.Generic;
    namespace MergeWitModifications
    {
        public class Modifications
        {
            public Modifications()
            {
                XPathModifications = new List<XPathModification>();
                XPathAdditions = new List<XPathAddition>();
            }
     
            /// <summary>
            /// The list contains all modifications to the attribute values
            /// that must be done in the work item type.
            /// </summary>
            public List<XPathModification> XPathModifications { get; private set; }
     
            /// <summary>
            /// The list contains all additions to the work item type.
            /// </summary>
            public List<XPathAddition> XPathAdditions { get; private set; }
        }
    }
  6. To add the logic that adds the new elements to the work item type definition, add the following method to the AutoModifyWit class
  7. private static void ExecuteAddition(XmlDocument doc, XPathAddition addition)
    {
        // Get the parent node 
        var node = doc.SelectSingleNode(addition.XPath);
     
        // If the parent is existent, and the node does not exist already
        if (node != null && node.SelectSingleNode(string.Format("{0}[@{1}='{2}']", addition.Name, addition.Attributes[0].Name, addition.Attributes[0].Value)) == null)
        {
            // Create the node
            var childNode = doc.CreateElement(addition.Name);
     
            // And set the attribute values
            foreach (var attribute in addition.Attributes)
            {
                var xmlAttribute = doc.CreateAttribute(attribute.Name);
                xmlAttribute.Value = attribute.Value;
                childNode.Attributes.Append(xmlAttribute);
            }
     
            // Add the created node to the parent
            node.AppendChild(childNode);
        }
    }
  8. And add the code to call the ExecuteAddition method to the Start method. To do this, navigate now to the lines
  9. ///////////////////////////////////
    /// 
    /// TODO: Make the modifications to the work item type
    /// handled in part 2.
    /// 
    ///////////////////////////////////
  10. And add the following code

  11. foreach (var addition in modifications.XPathAdditions)
    {
        ExecuteAddition(doc, addition);
    }
  12. Now update the unit test to test the possibility to add new elements to the work item types. Navigate to the StartAutoModifyWit unit test.
  13. Add the following line to the unit test to add the start date to all work item types on the server.
  14.             modifications.XPathAdditions.Add(new XPathAddition("//Group[@Label='Schedule']/Column", "Control",
                                                             new[]
                                                                           {
                                                                               new XPathAddition.Attribute() {Name = "FieldName", Value="Microsoft.VSTS.Scheduling.StartDate"}, 
                                                                               new XPathAddition.Attribute() {Name = "Type", Value="FieldControl"},
                                                                               new XPathAddition.Attribute() {Name = "Label", Value="Start Dat&e:"},
                                                                               new XPathAddition.Attribute() {Name = "LabelPosition", Value="Left"},
                                                                               new XPathAddition.Attribute() {Name = "ReadOnly", Value="False"}
                                                                           }));

The library is not able to modify existing elements and add new ones to the work item types. You have now the base library which you can extend to your own needs.

Good luck.

Tags:

VSTS 2008 | Work items

Comments are closed

Powered by BlogEngine.NET 1.6.1.0
Theme by Mads Kristensen


ClusterMap

Widget Statistics not found.

There is an error in XML document (0, 0).X

Recent comments

Comment RSS