TFS API Part 10: Add Area/Iteration Programmatically

30/01/2009

2 comments

TFS API Part 10: Add Area/Iteration Programmatically

In my last post I talk about TFS API Part 9: Get Area/Iteration Programmatically.

In this post I will show how to add Area or Iteration programmatically ICommonStructureService.

image

First add reference for Microsoft.TeamFoundation, Microsoft.TeamFoundation.Client, Microsoft.TeamFoundation.Common.dll,Microsoft.TeamFoundation.WorkItemTracking.Client.dll
located in – C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\

Add using for:

using Microsoft.TeamFoundation.Proxy;

using Microsoft.TeamFoundation.Server;

using Microsoft.TeamFoundation.Client;

Download Demo

If the Node not exists checking if there is more then one Area/Iteration to create
(Example: Area 7\\Sub Area 7)

 

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)

{

    if (ex.Message.Contains("Invalid path."))

    {

        //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 = AddNode(NewPath, projectName, nodeType);

}

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

return css.GetNode(newPathUri);

 

image

Checking if the Node exists…

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 null; //already exists

    }

}

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;

    }

}

 

image

Download Demo

 

Add comment
facebook linkedin twitter email

Leave a Reply

2 comments

  1. ezien08/05/2012 ב 11:54

    IF you also want to see the area/iteration node permissions, this post contains the code you need:

    http://ezien.com/tfsblog/2012/05/08/tfs-api-get-area-or-iteration-permissions/