Example of updating ParentProject via REST API using Powershell?

I'm looking for an example of updating the ParentProject for a Project using the REST API via Powershell or anything .NET I suppose would work too.

https://www.jetbrains.com/help/teamcity/rest/projectapi.html#setParentProject

I'm following these docs. I'm using a credential token that matches my login and I have permission to update the projects in question, but I'm either getting 500 responses or 400 responses. I think I'm just not formatting the request body properly as I can retrieve the project in question via the API, but then when I try to update that data and PUT it to the server it fails.

I'm basically doing a GET of the project then updating that data and PUTting it back.

   
    $headers = @{
        Authorization="Bearer $tcAccessToken"
    }

$projectResponse = Invoke-restmethod -Uri "https://TCURI/app/rest/projects/id:$projectId" -Headers $headers -ContentType 'text/plain'

$projectResponse.project.parentProjectId = "newParent"

Invoke-RestMethod -Method 'PUT' -Uri "http://TCURI/app/rest/projects/id:$projectResponse.project.id/parentProject" -Body $response -Headers $headers -ContentType 'application/xml'

That's basically what is being done, though some of the formatting might be off since I'm trying to recreate it here in this text box.

2 comments
Comment actions Permalink

Hi! Something like this should work:

$TCURI = "teamcity:8111"
$tcAccessToken = "xxx.yyy.zzz"
$headers = @{
  Authorization="Bearer $tcAccessToken"
}

$projectId = "projectToMove"
$newParent = "newParent"

$newParentXml = @"
  <project id="$newParent"></project>
"@
Invoke-RestMethod -Method 'PUT' -Uri "https://$TCURI/app/rest/projects/id:$projectId/parentProject" -Body $newParentXml -Headers $headers -ContentType 'application/xml'

When TeamCity returns HTTP 500, it would also typically include a message that would hint you why it couldn't fulfill the request, e.g.:

Project cannot be moved because it uses some VCS roots that are not accessible in the target project: newParent

 

1
Comment actions Permalink

Thanks, that worked. It was really just not knowing what XML it was expecting on the update. I kept sending the wrong body. In the end it was very simple, but it wasn't obvious to me from the docs.

0

Please sign in to leave a comment.