Creating areas for your scrum project in TFS 2012

Organizing and dividing work into logical areas in your project can be a great way to track and add traceability to a process where certain changes are made.  Adding these areas one at a time can be a time consuming process.  In this post, we’ll set up a small method to make it easier to create the areas in the project.

The first step is getting a connection open to the team project collection.  I talked about that in my previous post. Once the connection is open, we need to obtain a reference to the ICommonStructureService.  Obtaining references to particular services in TFS is done through the GetService method on the TfsTeamProjectCollection object.

So, we will do the following:

var css = projectCollection.GetService<ICommonStructureService>();

I only mention this separately because this can also be used to get various other services to perform operations against the project collection.  For example, version control and work item services can be obtained through the GetServices method.

Now that we have a reference to the css, we’ll use a couple of methods to look up if nodes are already created or if they need to be created.  Now some of this is tricky and involves going through some of the paths in the node to determine what the correct path formats are.  However, once the infrastructure is in place, that will be relatively simple.

The areas in a project are stored in a hierarchy structure in code with each node containing a reference to it’s parent and all it’s children.  So, in order to start building the areas, we need to find the root structure node for the areas. Once that’s found, we can begin adding areas.

The full set of code is here.

public static void AddArea(TfsTeamProjectCollection projectCollection, string projectName, params string[] areas) {

	if (projectCollection == null)
		throw new ArgumentNullException("projectCollection");

	if (projectName == null)
		throw new ArgumentNullException("projectName");

	// Get the structure service to define areas for the project. 
	var css = projectCollection.GetService<ICommonStructureService>(); 

	// Find the information for the project to add the area 
	var project = css.ListProjects().FirstOrDefault((proj) => proj.Name == projectName); 
	if (project == null) 
		throw new InvalidOperationException("Could not find project."); 

	// List all the structures available in the project. 
	var nodes = css.ListStructures(project.Uri); 
	var curNode = nodes.FirstOrDefault(node => node.StructureType == "ProjectModelHierarchy"); 
	if (curNode == null) 
		throw new InvalidOperationException("The project specified does not contain an Area structure."); 

	// Loop through the list of areas and create each one. 
	foreach (var area in areas) { 
		// After a null is included in the list, don't evaluate any more areas 
		// assume the list is completed. 
		if (string.IsNullOrWhiteSpace(area)) 
		// Try find the node, if it doesn't exist, create it. 
		try { 
			curNode = css.GetNodeFromPath(curNode.Path + "\\" + area); 
		catch (CommonStructureSubsystemException csse) {
			// Not a very elegant way to deal with a node not existing, but the interface 
			// does not have a check for if something exists. 
			if (csse.Message.StartsWith("TF200014")) { 
				var curNodeUri = css.CreateNode(area, curNode.Uri); 
				curNode = css.GetNode(curNodeUri); 
			} else throw; 

The project can now be added in a simple line of code. This will create an area hello with a sub-area of world.

ScrumProjectBuilder.AddArea(projectCollectionHere, "example project", "hello", "world");



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s