Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TFS "Copy" value from one field to another

Tags:

tfs

workitem

I needed to change a work item field from PlainText -> String. As I could not change the type on the Work Item, creating a new field and updating it's value from the other field is my approach.

I have tried the "Bulk Edit Selected Work Items.." from TFS/Web but I'm not sure if you may reference another field value in that template.

How may I set [Work Item].[FieldNew].Value = [Work Item].[FieldOriginal].Value ??

Is this even possible without having to use the TFD API?

enter image description here

The reason why I need to change the item field type from PlainText to String is that I want to have a query with a column operator to test if the field has value or not.

For a plainText field the only allowed operator is Contains/Does Not Contain. May I override this to allow a ">" ? enter image description here

like image 485
Kman Avatar asked Jun 14 '12 10:06

Kman


Video Answer


2 Answers

KMoraz's solution does not work for me either, because the HTML field becomes read-only when exported to Excel. Therefore, I used a Powershell script to copy the value of one field into another (just replace the "$wiFieldNewValue" variable with the source field you are copying)

Code reference: Bulk update TFS work items using Powershell

Link to code

Embedded code:

#This script sets a specific field to a specified value for all work items in a specific project

Function LoadTfsAssemblies() {
Add-Type –AssemblyName "Microsoft.TeamFoundation.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
Add-Type -AssemblyName "Microsoft.TeamFoundation.WorkItemTracking.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

}

##### SETTINGS
#The TFS Team Project Collection to connect to
$tfsUri = "http://tfsserver:8080/tfs/DefaultCollection"

#The TFS Team Project from which to select the work items
$tfsProject = "Test Project"

#The work item type of the work items to update
$wiType = "Test Case"

#The reference name of the field to update
$wiFieldRefName = "Microsoft.VSTS.Common.Priority"

#The value to set the field to
$wiFieldNewValue = "1"
##### END SETTINGS

LoadTfsAssemblies
$tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsUri)
$tfs.EnsureAuthenticated()
if($tfs.HasAuthenticated)
{
Write-Output "Successfully authenticated to TFS server [$tfsUri]"
$workItemStore = $tfs.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])
$query = "SELECT [System.Id], [System.Title] FROM WorkItems WHERE [System.TeamProject] = '{0}' AND [System.WorkItemType] = '{1}'" -f $tfsProject, $wiType
Write-Output("Using query [$query]")

$workItems = $workItemStore.Query($query)
Write-Output("Going to update [{0}] work items" -f $workItems.Count)
$successCount = 0
$failureCount = 0
ForEach($wi in $workItems) {
Write-Output("Updating work item [{0}]" -f $wi.Title)

try {
$wi.Open()
$wi.Fields[$wiFieldRefName].Value = $wiFieldNewValue
Write-Output("Set field [{0}] to [{1}]" -f $wiFieldRefName, $wiFieldNewValue)
$validationMessages = $wi.Validate()

if($wi.IsValid() -eq $true)
{
$wi.Save()
Write-Output("Successfully updated work item [{0}]" -f $wi.Title)
$successCount++
} else {
Write-Error("Work item is not valid!")
ForEach($validationMessage in $validationMessages)
{
Write-Error("Error: {0}" -f $validationMessage)
}
$failureCount++
}
} catch {
Write-Error("Couldn't set field [{0}] to [{1}] for work item [{2}]" -f $wiFieldRefName,$wiFieldNewValue,$wi.Title)
Write-Error $_
$failureCount++
}
}

Write-Output("Finished!")
Write-Output("Successfully updated: {0}" -f $successCount)
Write-Output("Failed to update: {0}" -f $failureCount)

} else {
Write-Error("Couldn't authenticate to TFS server [$tfsUri]")
}
like image 119
Langston Avatar answered Nov 11 '22 19:11

Langston


I'ts possible via Excel.

  1. Create a query with both old and new field columns visible.
  2. Export the query to Excel.
  3. Copy and paste the data from old field column to the new field.
  4. In Excel, from the Team menu click Publish to update the changes in TFS.
like image 21
KMoraz Avatar answered Nov 11 '22 18:11

KMoraz