Welcome to EMC Consulting Blogs Sign in | Join | Help

Merrick Chaffer's Blog

Creating Area Paths and Iteration Paths in Team foundation server

Recently I had to automate the creation of areapaths and iteration paths via the Team foundation server Object model. After hours of searching this out on the msdn discussion forums, I thought I'd list the completed solution here.

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);

    }

}

Published 08 November 2006 15:50 by merrick.chaffer
Filed under: ,

Comments

 

La masa, el ladrillo, la bota, el bocadillo... said:

Casualidades tiene la vida... justo depués de escribir sobre las iteraciones y las áreas en Team Foundation

December 17, 2006 19:51
 

Blog de .NET - FinderIT » Blog Archive » Importando ??reas e iteraciones a Team Foundation Server said:

March 22, 2009 18:17
Anonymous comments are disabled

This Blog

Syndication

News

Powered by Community Server (Personal Edition), by Telligent Systems