TFS

Upgrading a Custom Build from TFS 2010 to TFS 2012

While TFS 2012 contains many useful features for the development team, one of the difficulties upon upgrading is the lack of a simple way to convert pre-existing custom build templates. It’s not a difficult process by itself, it just takes time, and some trial and error.

First, the XAML from Visual Studio 2010 contains many references to the old assemblies and some duplicate entries.  The duplicates need to be removed and any version references to the 10.0 version of the assemblies need to be changed to the 11.0 version of the assemblies.  After removing the duplicates there may be no version references to update.

Second, make sure visual studio isn’t reporting any import errors in the XAML. Extra namespace may be referenced and need to be imported.

Third, if the build process is using any custom assemblies, the references in the projects need to be updated to the 11.0 version of the assemblies, and they need to be recompiled to be .NET 4.5.  Check in the modified assemblies to source control for the build controller to pick up.

Finally, I’ve ran into problems with some of the activities from the old TFS 2010 default template which is what my custom build process was based on.  The LabelSources activity seems to give a null reference error when trying to label the workspace.  However, the workspace can be be labeled using the LabelWorkspace activity which seems to work.

I can’t guarantee these are all the problems you may encounter, but hopefully having a good listing of the issues I encountered will help out some others.  Here’s a list of the blogs and msdn references that were useful in helping to upgrade the TFS builds.

http://blogs.msdn.com/b/jpricket/archive/2012/07/17/tfs-2012-cleaning-up-workflow-xaml-files-aka-removing-versioned-namespaces.aspx

http://osnabrugge.wordpress.com/2012/08/08/lessons-learned-in-upgrading-my-tfs-2010-build-templates-to-tfs-2012/

http://msdn.microsoft.com/en-us/library/dd647548.aspx

Advertisements

Creating backlog items for your scrum project in TFS 2012

In my last post, I showed how to programmatically create areas for a scrum team project in TFS 2012. In this post, I’m going to show how to add product backlog items.

Once you understand how retrieve services from a TfsTeamProjectCollection object, interacting with TFS is just a matter of knowing which services to use.  For adding work items into TFS this can be accessed through the WorkItemStore service.  This service gives you access to any work items in a team project and the ability to manipulate them.

In order to create a work item, it’s required to know what type of item it is.  In the scrum process template, items in the backlog are work items of the “Product Backlog Item” type.  It’s possible to also iterate through the work item types to see what’s available in your process as well.  If you’re working with a  different process template, there could be any variety of work item types.  After retrieving the type, it’s possible to finally create the product backlog item.

Now, in this example, I did not go through the work of actually validating the work item.  However, there is a Validate method.  When using the Validate method, if there is any validation problem with the WorkItem, it will return an error for exactly which field in the work item failed to validate.  Of course, the Save method will throw an exception if the WorkItem is invalid as well.

Here’s a sample set of code to add items to the product backlog.

public static void AddProductBacklog(TfsTeamProjectCollection projectCollection, 
    string projectName, string title, 
    string description, string areaPath=null)
{
    if (projectCollection == null)
        throw new ArgumentNullException("projectCollection");

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

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

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

    var collectionStores = projectCollection.GetService<WorkItemStore>();
    var store = collectionStores.Projects[projectName];

    if (!store.WorkItemTypes.Contains("Product Backlog Item"))
        throw new InvalidOperationException("The project " 
            + projectName + " does not support product backlog items.");

    WorkItem item = new WorkItem(store.WorkItemTypes["Product Backlog Item"]);
    item.Title = title;
    item.Description = description;

    if (areaPath != null)
        item.AreaPath = areaPath;

    item.Save();
}

I hope this helps, and get’s you a step closer to being able to use the scrum template in TFS.

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)) 
			break; 
		// 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); 
				css.ClearProjectInfoCache(); 
			} 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");

Enjoy!

Connect to Team Foundation Server 2012 via C#

Team foundation server is a great tool for managing projects.  However, sometimes working through the UI just isn’t efficient enough for large operations.  However, with new tooling comes the concern of how to port over all those work items from an older technology.  In the next few posts, I’ll be be showing some code to connect to TFS 2012, to add areas to the scrum template, and import information in the product backlog. It’s not complicated, but often time coming across the information in a single source is hard to do.

var projectCollection = new TfsTeamProjectCollection(
    new Uri("http://myserver:8080/tfs/DefaultCollection"), 
    new NetworkCredential("youruser", "yourpassword"));

projectCollection.Connect(ConnectOptions.IncludeServices);

At this point you’ll have a connection to TFS ready to go!  In the next posts, we’ll use the GetServices<> method on the projectCollection variable to retrieve specific services in order to make changes to the project collection.