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:
- Add a new class to the MergeWitModifications project and rename it to XPathAddition.cs.
- Add the following code to the class
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;
}
}
}
- Navigate to the Modifications class and replace the contents of the class to
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; }
}
}
- To add the logic that adds the new elements to the work item type definition, add the following method to the AutoModifyWit class
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);
}
}
- And add the code to call the ExecuteAddition method to the Start method. To do this, navigate now to the lines
///////////////////////////////////
///
/// TODO: Make the modifications to the work item type
/// handled in part 2.
///
///////////////////////////////////
-
And add the following code
foreach (var addition in modifications.XPathAdditions)
{
ExecuteAddition(doc, addition);
}
- Now update the unit test to test the possibility to add new elements to the work item types. Navigate to the StartAutoModifyWit unit test.
- Add the following line to the unit test to add the start date to all work item types on the server.
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.