0
votes

We have TFS on premise and i want to create a backlog Item using API.

I am able to get items from TFS but i am not able to create. Any guidance please

I am using the following path for getting the items.

/tfs/DefaultCollection/_apis/wit/workitems?ids=

So after trying the solution below in Postman i get this.

Hi,

http://...:8080/tfs/DefaultCollection/_apis/wit/workitems/$Product Backlog Items?api-version=5.0

application/json-patch+json

Body:

So i tried what you mention but i get this error

                <h2>404 - File or directory not found.</h2>
                <h3>The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.</h3>
1
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers.Daniel Mann
Actually it is not debugging. TFS has an API end point to get the list of backlog items that is the path. I need to know how can i create a TFS backlog item using Restful API.user592514
You've provided practically no details about the problem. Please update your question to include a specific error and code that reproduces it.Daniel Mann

1 Answers

0
votes

You need to provide the Request Body and use the PATCH method to create a work item:

Please see Create a work item for details.

PATCH https://{instance}/DefaultCollection/{project}/_apis/wit/workitems/${workItemTypeName}?api-version={version}

Content-Type: application/json-patch+json

[
    {
        "op": "add",
        "path": { string }
        "value": { string or int, depending on the field }
    },
    {
        "op": "add",
        "path": "/relations/-",
        "value":
        {
            "rel": { string },
            "url": { string },
            "attributes":
            {
                { name/value pairs }
            }
        }
    }
]

You can also use below PowerShell sample calling the REST API to create a PBI (add other fields based on your requirement):

Param(
   [string]$baseurl = "http://server:8080/tfs/DefaultCollection", 
   [string]$projectName = "0511ScrumTFVC",
   [string]$workItemType = "Product Backlog Item",
   [string]$user = "Domain\user",
   [string]$token = "password"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

function CreateJsonBody
{

    $value = @"
[
  {
    "op": "add",
    "path": "/fields/System.Title",
    "value": "0831-PBI-Test"
  },
  {
    "op": "add",
    "path": "/fields/System.AreaPath",
    "value": "0511ScrumTFVC"
  },

  {
    "op": "add",
    "path": "/fields/System.IterationPath",
    "value": "0511ScrumTFVC\\Sprint 1"
  },

  {
    "op": "add",
    "path": "/fields/System.Tags",
    "value": "Tag0831"
  },

  {
    "op": "add",
    "path": "/fields/Microsoft.VSTS.Common.Activity",
    "value": "Development"
  },

  {
    "op": "add",
    "path": "/fields/Microsoft.VSTS.Scheduling.Effort",
    "value": "8"
  },
  {
    "op": "add",
    "path": "/fields/Microsoft.VSTS.Common.ValueArea",
    "value": "Business"
  }
]
"@

 return $value
}

$json = CreateJsonBody

$uri = "$baseurl/$($projectName)/_apis/wit/workitems/"+"$"+"$($workItemType)?api-version=2.2"
Write-Host $uri
$result = Invoke-RestMethod -Uri $uri -Method Patch -Body $json -ContentType "application/json-patch+json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

UPDATE:

Seems you are using the unsupported api-version=5.0. The api version 5.x is only supported in VSTS. For on-premise TFS 2017 the supported version is 3.x, TFS2018 is 4.x, and it's forward compatible, e.g 2.x is also supported in TFS 2017 and 2018.

So just have a try with the api-version=2.2... it works on my side.

The second error means the request body format is incorrect. You can follow my PowerShell sample and just change the correspond values for the body, then try it again.

Besides, you can reference this blog to create work items in TFS: Create Work Items in Visual Studio Online with Nintex Workflow 2010