Trying to use PowerShell to add a Parent Link to a

2019-06-03 16:26发布

问题:

I am trying to add a parent link when I create TFS task via powershell. However, I am only able to add a related link:

function Create-New-WorkItem($projName, $taskType, $title, $state, $assignedTo, $iterationPath, $activity, $BLItem)
    {
      $tfs = Get-TfsServer
      $ws = $tfs.GetService([type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")
      $proj = $ws.projects[$projName]
      $workitem = $proj.workitemtypes[$taskType].newworkitem()
      $workitem.open()
      $workitem.title = $title
      $workitem.state = $state
      $workitem.fields["Assigned To"].value = $assignedTo
      $workitem.iterationpath = $iterationPath
      $workitem.fields["Activity"].value = $activity
      $id = Get-Parent-Link $BLItem  
      $workitem.links.add($id.ID)
      $workitem.close()
      $workitem.save()
    }

function Get-Parent-Link($backLogItemName)
  {
    $tfs = Get-TfsServer
    $WIQL = @"
    SELECT [System.Id]
    FROM WorkItems 
    where [System.Title] = '$backLogItemName'
    "@
    return $tfs.wit.query($WIQL)
  }

How can I add the link as a parent instead of a related?

回答1:

After some trial and error I finally found a way to accomplish linking a new work item as a child to a parent item i.e. backlog item.

$ws = $tfs.GetService([type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")
$linkType = $ws.WorkItemLinkTypes[[Microsoft.TeamFoundation.WorkItemTracking.Client.CoreLinkTypeReferenceNames]::Hierarchy]

Add the workitem id of the parent you want to link the new child workitem to and create a workitemlink object:

$link = new-object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemLink($linkType.ReverseEnd, 1234)  

You can then add the link to a workitem:

$workitem.WorkItemLinks.Add($link)
$workitem.save()


回答2:

You need to create a different link type object. A good exercise of the API can be found on Shai's blog.

http://blogs.microsoft.co.il/shair/2010/02/27/tfs-api-part-22-create-link-between-work-item-parent-child-etc/

The PowerShell for this is almost identical.