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

by Ewald Hofman 21. July 2009 03:38

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. This part continues on that base library and covers modifying the work item types based on XPath expressions.

The work item type definition consists of three parts. The first part is the the declaration of the fields that are available in the work item. The second part is the layout of the work item when it is presented in Visual Studio, or Team System Web Access. The last part is the work flow for the work item. The definition of the work item type is xml based.

Modifying work item types can be done by means of changing the attributes of existent nodes, by adding new nodes, or by removing nodes. The last (removing nodes) is not covered in this series. This part of the post will handle changing the attribute values.

  1. Start by adding a new class to the MergeWitModifications project that is created in the first part. Rename the class to XPathModification.
  2. Paste the following code into the XPathModification.cs file.
  3. namespace MergeWitModifications
    {
        public  class XPathModification
        {
            /// <summary>
            /// Constructor.
            /// </summary>
            /// <param name="xPath">The XPath expression to the attribute that should be modified.</param>
            /// <param name="newValue">The new value for the attribute.</param>
            public XPathModification(string xPath, string newValue)
            {
                XPath = xPath;
                NewValue = newValue;
            }
     
            public string XPath { get; private set; }
            public string NewValue { get; private set; }
     }
    }
  4. You have now created a class that holds the information to be able to change an attribute. Now add a new class that holds the list of all modifications.
  5. Add a new class to the project and call it Modifications.cs
  6. Paste the following code into the Modifications.cs file.
  7. using System.Collections.Generic;
    namespace MergeWitModifications
    {
        public  class Modifications
        {
            public Modifications()
            {
                XPathModifications = new List<XPathModification>();
            }
     
            /// <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; }
        }
    }
  8. Now it is time to create the logic to modify the work item type.
  9. Navigate to the Start method in the AutoModifyWit class and add the modifcations parameter to the method.
  10. Add a new method to the AutoModifyWit class called
  11.         /// <summary>
            /// Executes the modifications to the work items.
            /// </summary>
            /// <param name="modifications">The list of modifications that must
            /// be applied to the work item type.</param>
            /// <param name="teamProjects">The list of team projects that must be 
            /// modified. Pass null or the empty array when you want to update all 
            /// team projects.</param>
            /// <param name="workItemTypes">The list of work item types that must be 
            /// modified. Pass null or the empty array when you want to update all 
            /// work item types.</param>
            public void Start(Modifications modifications, string[] teamProjects, string[] workItemTypes)
  12. Add a new method to the class that will actually change the attribute value. To do this, navigate to the end of the AutoModifyWit class and add the following method
  13.         /// <summary>
            /// Performs the modification to the work item type definition.
            /// </summary>
            /// <param name="doc">The xml document that represents the work item type.</param>
            /// <param name="modification">The modification info.</param>
            private static void ExecuteModification(XmlDocument doc, XPathModification modification)
            {
                // Get the attribute at the XPath location
                var node = doc.SelectSingleNode(modification.XPath);
     
                if (node != null)
                {
                    // When found, modify the value of the attribute
                    node.Value = modification.NewValue;
                }
            }
  14. Navigate now to the lines
  15. ///////////////////////////////////
    /// 
    /// TODO: Make the modifications to the work item type
    /// handled in part 2.
    /// 
    ///////////////////////////////////
  16. And add the following code

  17. foreach (var modification in modifications.XPathModifications)
    {
        ExecuteModification(doc, modification);
    }
  18. Now that you have finalized the library to be able to modify a work item type, the unit test must be modified to test the code
  19. Navigate to the StartAutoModifyWit unit test that contains our "UI code".
  20. Change the call to the target.Start method to
  21. var modifications = new Modifications();
     
    target.Start(modifications, teamProjects, workItemTypes);
  22. To make the Start and Finish date writable for example you can add the following lines
  23. modifications.XPathModifications.Add(new XPathModification("//Control[@FieldName='Microsoft.VSTS.Scheduling.StartDate']/@ReadOnly", "False"));
    modifications.XPathModifications.Add(new XPathModification("//Control[@FieldName='Microsoft.VSTS.Scheduling.FinishDate']/@ReadOnly", "False"));

You have now added the functionality to modify existing elements in the work item type. The next post covers the ability to add new elements (field, layout and/or work flow) to the work item type.

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