Create TFS work items from XML file

13/10/2012 02:38

#input parameters
$sourceFilePath="c:\temp\TFSdefaultTasksXMLshort2.xml"

#input parameters
$parentId=391
$areaPath=“ComponentFactory\12. External Video"
$iterationPath=“ComponentFactory\Release 1\Sprint 12"
$defaultPriority=1000

#takes component name from areaPath (e. g. External Video)
$prefix=($areaPath.Split('\')[1]).split('.')[1].Trim()

#load assemblies
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.TeamFoundation.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.TeamFoundation.WorkItemTracking.Client")

#get TFS structure, project
[psobject] $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer(“https://yourServer/tfs/defaultCollection")
$WorkItemStore=$tfs.TfsTeamProjectCollection.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])
$project=$WorkItemStore.Projects["ComponentFactory"]
$type=$project.WorkItemTypes["Task"]

#create parent-child relationship
$linkType = $WorkItemStore.WorkItemLinkTypes[[Microsoft.TeamFoundation.WorkItemTracking.Client.CoreLinkTypeReferenceNames]::Hierarchy]

#xml structure, schema only, not used in script
$s="
 
   
   
   
   
 
"


#get xml file
$xml=[xml](Get-Content -path $sourceFilePath)


function CreateTaskInTFS
   {
    param(
   
    [string]$itemTitle,
    [string]$itemDescription,
    [int]$priority,
    [int]$time,
    [string]$areaPath,
    [string]$iterationPath
   
    )
    #print information on screen
    write-host "Title:" $itemTitle
    write-host "Desc:"$itemDescription
    write-host "Priority: "$priority
    write-host "Time: "$time
    write-host "Area: "$areaPath
    write-host "Iteration Path: "$iterationPath
    write-host "Link: "$link
 
 #define new instance of link object
 $link = new-object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemLink($linkType.ReverseEnd, $parentId) #391 is id of parent
   
 #create Task
 $item = new-object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem $type
 
 #set properties
 #properties from common file - different for each item
 $item.Title = $itemTitle
 
 #fill custom fields
 $item.Fields["Description HTML"].Value = $itemDescription
 $item.Fields["Backlog Priority"].Value = $defaultPriority
 $item.Fields["Remaining Work"].Value = $time
 
 #properties from input parameters - common for all items
 $item.AreaPath = $areaPath
 $item.IterationPath = $iterationPath
 $item.WorkItemLinks.Add($link)
 
 #save task
 $item.Save() 
   
 
}
#Call function for each task item in XML file
$i=1
foreach($xmlTask in $xml.Tasks.Task)
{
    write-host ""
    write-host "-----------"
    write-host "Item " $i
    write-host "-----------"
    write-host "Parent Id: "$parentId
    $taskTitle=[string]::format("{0}: {1}", $prefix, $xmlTask.Title);
    CreateTaskInTFS -itemTitle $taskTitle -itemDescription $xmlTask.Description -priority $xmlTask.Priority -time $xmlTask.Effort -areaPath $areaPath -iterationPath $iterationPath
    $i++
  
}