Retrieving Areas from TFS

As a matter of self-inflicted education I’ve decided to create a simple Visual Studio Add-In for exporting and importing Areas and Iterations.  I’m not sure how much practical use such a utility will receive but it offers several areas for advancing my knowledge and, hopefully, helping a few other people out along the way.

Although I haven’t completed the utility yet, I have completed some of the initial utility code.  I thought I would create a few posts as I progress in case anyone can make use of the examples.  For this post, I am posting the code used to retrieve the list of “areas” from a Team Foundation Server

With that said, here is some example code for saving the list of Areas defined for a given Team Foundation Server and Team Project.  The code is a little verbose but should be relatively simple to follow.


using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;

namespace TestConsole
{
public class ExportAreas
{
private TeamFoundationServer _teamFoundationServer;
private ICommonStructureService _commonStructureService;

///

/// Exports the “areas” defined within the specified team foundation server.
///

/// The team foundation server. /// The name of the team project to export areas from. /// The filename to save the exported areas to. public void Export(string teamFoundationServer, string projectName, string filename)
{
_teamFoundationServer =
TeamFoundationServerFactory.GetServer(teamFoundationServer);

_commonStructureService = (ICommonStructureService)_teamFoundationServer.GetService(typeof(ICommonStructureService));

List<HierarchyNode> nodeList = new List<HierarchyNode>();

// Locate root structure node for “Area”
NodeInfo areaNode = GetAreaRootNode(projectName);

//String node = _commonStructureService.CreateNode(“TEST Node…”, areaUri);

// Retrieve the Area nodes XML
XmlElement nodes = _commonStructureService.GetNodesXml(new string[] { areaNode.Uri }, true);

// Recursively build out the list of Area nodes
BuildHierarchicalList(nodes.ChildNodes[0], nodeList, 0);

// Save the areas to the designated text file
StreamWriter writer = new StreamWriter(filename, false);

foreach (HierarchyNode node in nodeList)
{
writer.WriteLine(
new string(‘\t’, node.IndentLevel) + node.Name);
}

writer.Close();
}

///

/// Recursively builds a hierarchical list based on the root node passed in.
///

/// The root node to start with. /// The target list to add each instance to. This
/// list must be instantaited on the first call. /// The indent level – pass in 0 for the initial call. private void BuildHierarchicalList(XmlNode rootNode, List<HierarchyNode> targetList, int indentLevel)
{
HierarchyNode listNode;

if (rootNode.Name == “Children”)
{
for (int index = 0; index < rootNode.ChildNodes.Count; index++)
{
BuildHierarchicalList(rootNode.ChildNodes[index], targetList, indentLevel);
}
}
else
{
listNode =
new HierarchyNode()
{
Uri = rootNode.Attributes[
“NodeID”].Value,
Name = rootNode.Attributes[
“Name”].Value,
ParentUri = GetAttribute(rootNode,
“ParentID”),
Path = rootNode.Attributes[
“Path”].Value,
ProjectUri = rootNode.Attributes[
“ProjectID”].Value,
StructureType = rootNode.Attributes[
“StructureType”].Value,
IndentLevel = indentLevel
};

targetList.Add(listNode);

if (rootNode.HasChildNodes)
{
for (int index = 0; index < rootNode.ChildNodes.Count; index++)
{
BuildHierarchicalList(rootNode.ChildNodes[index], targetList, indentLevel + 1);
}
}
}
}

///

/// Gets the specified attribute from ..
///

/// The node containing the attribute collection. /// The name of the attribute to retrieve. /// The value of the specified attribute if it exists; otherwise, an empty string is returned.
private string GetAttribute(XmlNode node, string attributeName)
{
for (int index = 0; index < node.Attributes.Count; index++)
{
if (string.Equals(node.Attributes[index].Name, attributeName, StringComparison.InvariantCultureIgnoreCase))
{
return node.Attributes[index].Value;
}
}

return string.Empty;
}

///

/// Gets the area root node.
///

/// The name of the team project to retrieve the root node from. /// A object referencing the root area node.
private NodeInfo GetAreaRootNode(string projectName)
{
ProjectInfo projectInfo = _commonStructureService.GetProjectFromName(projectName);

return LocateStructureByType(projectInfo.Uri, “ProjectModelHierarchy”);
}

///

/// Locates the desired structure based on structure type.
///

/// The project URI to retrieve the structure from. /// Type of the structure to retrieve. /// A object referencing the root of the desired structure.
private NodeInfo LocateStructureByType(String projectUri, String structureType)
{
NodeInfo[] nodes = _commonStructureService.ListStructures(projectUri);

foreach (NodeInfo node in nodes)
{
if (node.StructureType == structureType)
{
return node;
}
}

return null;
}

}

///

/// This class is used to hold information pertaining to exported areas or iterations.
///

class HierarchyNode
{
public string Uri { get; set; }
public string Name { get; set; }
public string ParentUri { get; set; }
public string Path { get; set; }
public string ProjectUri { get; set; }
public string StructureType { get; set; }
public int IndentLevel { get; set; }
}
}

To use the above sample code, just instantiate the ExportAreas class and call the Export method passing in the desired argument values.

2 thoughts on “Retrieving Areas from TFS

Comments are closed.

Related Posts