Inspired by this blog post by the AIT GmbH, the following PowerShell script is created to simply create a new product backlog item in a team project in Team Foundation Server.

param (
	[string] $Url,          # the uri to the team project collection
	[string] $ProjectName,  # the name of the team project
	[string] $PBITitle,     # the title for the new product backlog item
	[string] $Area          # the area path, could be empty
)

#Load Reference Assemblies 
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client") 
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.WorkItemTracking.Client") 
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.ProjectManagement") 

$TeamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($Url) 
$TeamProjectCollection.EnsureAuthenticated()

# Service instances 

$css4 = 
   ($TeamProjectCollection.GetService([type]"Microsoft.TeamFoundation.Server.ICommonStructureService4")) -as 
     [Microsoft.TeamFoundation.Server.ICommonStructureService4] 

$teamService = 
   ($TeamProjectCollection.GetService([type]"Microsoft.TeamFoundation.Client.TfsTeamService")) -as 
     [Microsoft.TeamFoundation.Client.TfsTeamService] 

$teamConfigService = 
   ($TeamProjectCollection.GetService([type]"Microsoft.TeamFoundation.ProcessConfiguration.Client.TeamSettingsConfigurationService")) -as 
     [Microsoft.TeamFoundation.ProcessConfiguration.Client.TeamSettingsConfigurationService] 

$store = 
   ($TeamProjectCollection.GetService([type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")) -as 
     [Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore]

$project = $store.Projects[$ProjectName]

if ($project -eq $null)
{
	throw "The team project $ProjectName could not be found"
}


# Get the work item type

$WorkItemType = "Product Backlog Item"  # this will only work with projects with the Scrum template

$wit = $project.WorkItemTypes[$WorkItemType] 

if (!$wit) { 
        throw "The WorkItemType $WorkItemType could not be found in project $ProjectName" 
} 


# Create Product Backlog Item (PBI) 

Write-Host "Creating Product Backlog Item" 

$pbi = New-Object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem ($wit) 

$pbi.Title = $PBITitle 

# $pbi.Description = "Hello World."

if ($Area -ne "")
{
	$pbi.AreaPath = $Area
}

$pbi.Save()

Write-Host -ForegroundColor Green "Done."

Advertisements