Search code examples
curltfsazure-devopsazure-devops-rest-apiazure-devops-extensions

Problem in using cURL and REST API of TFS to install extension


I am trying to use the REST API of TFS 2018 to install an extension on our server with the help of cURL. This process must be repeated everyday, and that's why I'd like to run it using a command with cURL.

So far I figured out how to get a list of all the installed extensions on the server with the following command. This tells me the connection and the authentication work well.

curl -u {username}:{PAT} -d "" -X GET http://{instance}/{collection}/_apis/extensionmanagement/installedextensions?api-version=4.1-preview.1

To install the extension I have read this info from microsoft doc: https://learn.microsoft.com/en-us/rest/api/azure/devops/extensionmanagement/installed%20extensions/install%20extension%20by%20name?view=vsts-rest-tfs-4.1

I try formulate a command to install an extension I found here:https://marketplace.visualstudio.com/items?itemName=benjhuser.tfs-extensions-build-tasks.

The command is:

curl -u {username}:{PAT} -H "Content-Type: application/json" -X POST http://{instance}/{collection}/_apis/extensionmanagement/installedextensionsbyname/benjhuser/tfs-extensions-build-tasks/3.0.14?api-version=4.1-preview.1

I think the publisher, id and version of the extension should be correct, because I took them from this release document: https://github.com/huserben/TfsExtensions/blob/master/BuildTasks/vss-extension.json.

The response says the extension doesn't exist. This is confusing. Could someone please give me some hints, what the problem can be? Thank you in advance.

{
  "$id": "1",
  "innerException": null,
  "message": "The requested extension 'benjhuser.tfs-extensions-build-tasks' doesn't exist.",
  "typeName": "Microsoft.VisualStudio.Services.Gallery.WebApi.ExtensionDoesNotExistException, Microsoft.VisualStudio.Services.Gallery.WebApi",
  "typeKey": "ExtensionDoesNotExistException",
  "errorCode": 0,
  "eventId": 3000
}

Solution

  • The Install Extension By Name Rest API is used to Install pre-installed extensions. You need to add the extensions locally. Please refer to the document about Install extensions for on-premises servers.

    Please try the following steps:

    1.Open the developer tool(F12) of the browser and click install button. You can find the publisher name and extension name in the URL.enter image description here 2.Check the network tab in the developer tool(F12). You can find a REST API like

    GET http://{instance}/{collection}/_gallery/items?itemName={publisher name}.{extension name}&install=true&installContext={Some context}.

    The installContext will not change, you only need to change the publisher name and extension name here every time you install a new extension: enter image description here 3.Now, you can find this extension in the local extensions. enter image description here

    After this, you can use the Install Extension By Name API to Install Extensions.

    Steps summery:

    1.curl -u {username}:{PAT} -X GET http://{instance}/{collection}/_gallery/items?itemName=benjhuser.tfs-extensions-build-tasks&install=true&installContext={installContext}

    2.curl -u {username}:{PAT} -d "" -H "Content-Type: application/json" -X POST http://{instance}/{collection}/_apis/extensionmanagement/installedextensionsbyname/benjhuser.tfs-extensions-build-tasks?api-version=4.1-preview.1

    Here is my result: enter image description here

    In addition, Azure DevOps Services can use Install Extension By Name API directly.