Ashish Sah
Ashish Sah

Reputation: 31

How to copy tasks while cloning Incidents in TFS2015?

When we clone any Incident(WorkItem) in TFS, everything gets copied, except the tasks which are already associated with the original incident. How do we copy the task as well? Am I missing any workflow? Do I need to add any extension/API for this?

Upvotes: 1

Views: 231

Answers (1)

PatrickLu-MSFT
PatrickLu-MSFT

Reputation: 51103

When you create a copy of the work item, you can copy some links to the existing item such as test cases which lined to the workitem. However you can't create a copy of the child items such as tasks.

For example: You created a Product Backlog Item and assigned tasks to the Product Backlog Item. When you performing Create a Copy of Work Item, it is not making a copy of the 'tasks' and linking them to the copied work item.

There has been a related uservoice, you can vote for this to get more attention:

Make Workitem templates first class citizens

https://visualstudio.uservoice.com/forums/330519-team-services/suggestions/2073749-make-workitem-templates-first-class-citizens

For now you can try to use excel and PS script to achieve what you want. More details please refer this similar question: How to copy a work item and its tasks

if ( (Get-PSSnapin -Name Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
     Add-PSSnapin Microsoft.TeamFoundation.PowerShell
}

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Common")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.WorkItemTracking.Client")

# Connect to TFS and get Work Item Store.
$tfsCollectionUrl = "https://tfs.CORP.com/tfs/group"
$tfs = Get-TfsServer -name $tfsCollectionUrl
$ws = $tfs.GetService([type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")

$storyID = 15211    # ID of the story you want to copy.

$story = $ws.GetWorkItem($storyID)
Write-Host "Cloning User Story: " + $story.Title

#Clone User Story
$cloneStory = $story.Copy()
($story, $cloneStory )
$cloneStory.Title = "COPY : " + $story.Title

# cloneStory will not have links to all the tasks that were linked to the orginal story.
# cloneStory will have two links, one to the same "feature" that the orginal was linked to, and one to the story it was cloned from.
$cloneStory.Links

# cloneStory will have 0 for an ID, because it has not yet been saved.
$cloneStory.Id
#$cloneStory.Save()
# cloneStory will now have an ID.
$cloneStory.Id
$parentID = $cloneStory.Id  # Parent ID will be used to link new cloned tasks to this story.

$links = $story.Links

# Define a Link Type to be used in the loop.
$linkType = $ws.WorkItemLinkTypes[[Microsoft.TeamFoundation.WorkItemTracking.Client.CoreLinkTypeReferenceNames]::Hierarchy]


foreach ( $link in $links )
{

    $itemID = $link.RelatedWorkItemId
    #$itemID

    $item = $ws.GetWorkItem($itemID)

    if ( ($item.Type).Name -eq "Task" )
    {
        $reportLine = "Cloning Task ID:{0} {1}" -f $itemId, $item.Title
        Write-Host $reportLine
        # Clone the Task
        # Create the Parent Link object
        # Add the Parent Link to Cloned Task
        # Save New Cloned Task

        $cloneTask = $item.Copy()
        $cloneTask.Title = "COPY : " + $item.Title
        $parentLink = new-object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemLink($linkType.ReverseEnd, $parentID)
        $cloneTask.WorkItemLinks.Add($parentLink)
        $cloneTask.save()
        $cloneTask
    }
    else
    {
        $reportLine = "Skipping: {0} is not a Task, it is a {1}" -f $item.Title, ($item.Type).Name
        Write-Host $reportLine
    }
}

Upvotes: 0

Related Questions