Friday, April 02, 2010

TFS API: Creating recursive TFS areas

The following code has been founded at Shai Raiten's blog:

TFS API Part 9: Get Area/Iteration Programmatically

TFS API Part 11: Get Area/Iteration Security Settings Using IAuthorizationService

He created a fantastic serie how to work with the API!!!
Thanks Shai



Simple Call:
NodeInfo nodeInfo = this.GetAddNode("\\new area name1", "YourProject", StructureType.Area);
NodeInfo nodeInfo = this.GetAddNode("\\new area name2", "YourProject", StructureType.Area);

Recursive Calls:
NodeInfo nodeInfo = this.GetAddNode("\\new area name\\area 1", "YourProject", StructureType.Area);
NodeInfo nodeInfo = this.GetAddNode("\\new area name\\area 2", "YourProject", StructureType.Area);





public enum StructureType
{
Iteration = 0,
Area = 1
}

private NodeInfo GetAddNode(string ElementPath, string projectName, StructureType nodeType)
{
NodeInfo retVal;
string rootNodePath = "\\" + projectName + "\\" + nodeType.ToString();

//Check if this path already exsist
string newPath = rootNodePath + ElementPath;
try
{
retVal = css.GetNodeFromPath(newPath);
if (retVal != null)
{
// return existing node
return retVal;
}
}
catch (Exception ex)
{
if (ex.Message.Contains("The following node does not exist"))
{
//just means that this path is not exist and we can continue.
}
else
{
throw ex;
}
}

int BackSlashIndex = ElementPath.LastIndexOf("\\");
string Newpathname = ElementPath.Substring(BackSlashIndex + 1);
string NewPath = (BackSlashIndex == 0 ? string.Empty : ElementPath.Substring(0, BackSlashIndex));
string PathRoot = rootNodePath + NewPath;
NodeInfo previousPath = null;
try
{
previousPath = css.GetNodeFromPath(PathRoot);
}
catch (Exception ex)
{
// had to update the string from shai's original code
if (ex.Message.Contains("TF200014: The following node does not exist:"))
{
//just means that this path is not exist and we can continue.
previousPath = null;
}
else
{
throw ex;
}
}
if (previousPath == null)
{
//call this method to create the parent paths.
previousPath = GetAddNode(NewPath, projectName, nodeType);
}

string newPathUri = css.CreateNode(Newpathname, previousPath.Uri);
NodeInfo ni = css.GetNode(newPathUri);

Console.WriteLine("Creating Area: " + ni.Path.ToString());
return ni;
}

No comments:

Shared Cache - .Net Caching made easy

All information about Shared Cache is available here: http://www.sharedcache.com/. Its free and easy to use, we provide all sources at codeplex.

Facebook Badge