Brandon McClure
Brandon McClure

Reputation: 1390

Azure DevOps REST call - How to find out my identity

We recently migrated from on prem TFS to Azure Devops in the cloud. I am updating my powershell scripts that I use to automate my work item tracking, moving them from api v3 to v5 and ensuring all my authentication/endpoints are working properly. I have the authentication setup and can make some GET calls (list the repositories, run WIQL queries, etc) Now I need to create new work items using the Create endpoint.

My body looks like (and this was working on Prem, TFS 2018, REST version 3.0):

[
    {
        "op": "add",
        "path": "/fields/System.Title",
        "value": "My PBI title"
    },
    {
        "op": "add",
        "path": "/fields/System.AssignedTo",
        "value": "DOMAIN\\USERNAME"
    },
    {
        "op": "add",
        "path": "/fields/System.AreaPath",
        "value": "My\\Teams\\Path"
    },
    {
        "op": "add",
        "path": "/fields/System.IterationPath",
        "value": "backlog\\2019.03.25"
    }
]

When I POST with this body, I get a 400 response code and a error message that states: The identity value 'DOMAIN\\USERNAME' for field 'Assigned To' is an unknown identity."

If I exclude that property from my body statement I am able to create the work item, so I am trying different variations of my domain username/name (last name, first name/ first name last name) to try to get it to work/assign the item to me, but I keep getting the same error.

So my next step is to check the API documentation, and I see a Users - Get method, but that requires me to enter the userDescriptor which I assume is some sort of GUID to identify each user... but I don't know what mine is!

At this point I am stumped. There are a few other REST endpoints to get Profile/Security data, but that seems overly complicated for creating a work item.

How have you used this REST endpoint to create an item/assign it to a user via Azure-Devops in the cloud?

Upvotes: 1

Views: 704

Answers (1)

Brandon McClure
Brandon McClure

Reputation: 1390

After slamming my keyboard around some more I got it to work. the body definition should be:

{
   "op": "add",
   "path": "/fields/System.AssignedTo",
   "value": "LastName, Firstname"
}

So in my case the literal values would be:

{
       "op": "add",
       "path": "/fields/System.AssignedTo",
       "value": "McClure, Brandon"
}

Update:

I am using these REST calls in Powershell functions, and I am getting this programatically via a ADSI query, which works for most people. Some folks have very old AD accounts, and their UPNs are not configured the same as us, so I allow the user to specify their own value, although my understanding is that after all of the UPNs are brought into compliance for our Azure federated AD then this ADSI query will work for everyone!

param([string] $myIdentity = $null)

$dom = $env:userdomain
$usr = $env:username
if([string]::IsNullOrEmpty($myIdentity)){
    Write-Verbose "Getting the identity to set the tasks assigned to based on a ADSI query"
    $myIdentity = ([adsi]"WinNT://$dom/$usr,user").fullname
}
else{
    Write-Verbose "Using the identity you passed in"
}

Upvotes: 4

Related Questions