using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Server;
using System.Xml;
public class TeamServer
{
private TeamFoundationServer tfs;
private ICommonStructureService commonStructureService;
public enum NodeType : int
{
/// <summary>
/// Node index of the NodeInfo[] array containing the Iteration paths
/// </summary>
Iteration = 0,
/// <summary>
/// Node index of the NodeInfo[] array containing the Area paths
/// </summary>
Area = 1,
}
public TeamServer(string serverName)
{
tfs = TeamFoundationServerFactory.GetServer(serverName);
commonStructureService = (ICommonStructureService)tfs.GetService(typeof(ICommonStructureService));
}
/// <summary>
/// Adds either an iteration path or area path.
/// </summary>
/// <param name="structuredElementPath">The structuredElementPath would typically take the format \AreaPathParent\AreaPathNew (i.e. not including the ProjectName\Area) root path.</param>
/// <param name="projectName">Name of the project.</param>
/// <param name="nodeType">Type of the node.</param>
/// <returns>The <see cref="NodeInfo"/> of the path that was created or that already exists</returns>
/// <remarks>This method is recursive. That is, if it can't find the parent path then it will create it</remarks>
public NodeInfo AddAreaOrIterationPathToProject(string structuredElementPath, string projectName, NodeType nodeType)
{
NodeInfo retVal;
string rootNodePath = Constants.BackSlash + projectName + Constants.BackSlash + nodeType.ToString();
#region Check for existence of this area path already
string newPath = rootNodePath + structuredElementPath;
try
{
retVal = commonStructureService.GetNodeFromPath(newPath);
if (retVal != null)
{
return retVal; //already exists
}
}
catch (Exception ex)
{
if (ex.Message.Contains("Invalid path."))
{
//ignore this as it just means the path we are creating,
//has not yet been created hence continue on in this method
}
else
{
throw ex;
}
}
#endregion
int lastBackSlashPos = structuredElementPath.LastIndexOf(Constants.BackSlashChar);
string newAreaPathName = structuredElementPath.Substring(lastBackSlashPos + 1);
string newAreaPathDir = (lastBackSlashPos == 0 ? string.Empty : structuredElementPath.Substring(0, lastBackSlashPos));
string newAreaPathDirFromRoot = rootNodePath + newAreaPathDir;
NodeInfo previousPath = null;
try
{
previousPath = commonStructureService.GetNodeFromPath(newAreaPathDirFromRoot);
}
catch (Exception ex)
{
if (ex.Message.Contains("Invalid path."))
{
//ignore this as it just means the path we are creating,
//has not yet been created hence continue on in this method
previousPath = null;
}
else
{
throw ex;
}
}
if (previousPath == null)
{
//recurisvely call this method to create the parent paths.
previousPath = AddAreaOrIterationPathToProject(newAreaPathDir, projectName, nodeType);
}
string newPathUri = commonStructureService.CreateNode(newAreaPathName, previousPath.Uri);
return commonStructureService.GetNode(newPathUri);
}
}