You can read my complete guide on Microsoft Dynamics 365 for Finance & Operations and Azure DevOps.

I talked about the LCS Database Movement API in a post not long ago, and in this one I’ll show how to call the API using PowerShell from your Azure DevOps Pipelines.

What for?

Basically, automation. Right now the API only allows the refresh from one Microsoft Dynamics 365 for Finance and Operations environment to another, so the idea is having fresh data from production in our UAT environments daily. I don’t know which new operations the API will support in the future but another idea could be adding the DB export operation (creating a bacpac) to the pipeline and having a copy of prod ready to be restored in a Dev environment.

Don’t forget that the API has a limit of 3 refresh operations per environment per 24 hours. Don’t do this on a CI build! (it makes no sense either). Probably the best idea is to run this nightly with all your tests, once a day.

All these operations/calls can also be done using Mötz Jensen‘s which already support the LCS DB API. But if you’re using an Azure agent instead of a hosted one (like the Build VM) it’s not possible to install them. Or at least I haven’t found a way 🙂

EDIT: well thanks to Mötz’s comment pointing me to how to add to a hosted pipeline I’ve edited the post, just go to the end to see how to use them in your pipeline!

Calling the API

I’ll use PowerShell to call the API from a pipeline. PowerShell has a command called Invoke-RestMethod that makes HTTP/HTTPS requests. It’s really easy and we just need to do the same we did to call the API in my post.

Getting the token

To get the token we’ll use this script. Just change the variables for the ones of your project, AAD App registration, user (remember it needs access to the preview) and password and run it. If everything is OK you’ll get the JSON response in the $tokenResponse variable and from there you can get the token’s value using dot notation.

Requesting the DB refresh

This will be the call to trigger the refresh. We’ll need the token we’ve just obtained in the first step to use it in the header and the source and target environment Ids.

If it’s successful the response will be a 200 OK.

Add it to your pipeline

Adding this to an Azure DevOps pipeline is no mistery. Select and edit your pipeline, I’m doing it on a nigthly build (it’s called continuous but it’s not…) that runs after the environment has been updated with code, and add a new PowerShell task:

Calling the LCS Database Movement API in your Azure DevOps pipeline 2

Select the task and change it to “Inline”:

Calling the LCS Database Movement API in your Azure DevOps pipeline 3

Then just paste the script we’ve created in the Script field and done! You’ll get a refresh after the tests!

You can also run this on your release pipeline BUT if you do it after the deploy step remember to mark the “Wait for Completion” option or the operation will fail because the environment will already be servicing! And even then it could fail if the servicing goes over the timeout time. So… don’t run this on your release pipeline!

And that’s all. Let’s which new operations will be added to the API and what we can do with them.

Update: Use in your Azure Pipeline

Thanks to Mötz’s comment pointing me to how to add to a hosted pipeline I’ve created a pipeline which will install the tools and run the commands. It’s even easier to do than with the Invoke-RestMethod.

But first…

Make sure that in your Azure Active Directory app registration you’ve selected “Treat application as a public client” under Authentication:

Calling the LCS Database Movement API in your Azure DevOps pipeline 4

The task

First we need to install and then we can use its commands to call the LCS API:

As you can see it a bit easier to do the refresh using We get the token and pipeline the output to the Set-D365LcsApiConfig command which will store the token (and others). This also helps to not having to duplicate AppIds, users, etc. and as you can see to call the refresh operation we just need the source and target environment Ids!


Receive an email when a new post is published


  1. Pingback: Calling the LCS Database Movement API in your Azure DevOps pipeline - 365 Community

  2. Rabih SAIED Reply

    There is just a little mistake in the “Getting the token” PS script :
    $clientSecret and not $clientSecert
    Best Regards

    • Adrià Ariste Santacreu Reply

      Thanks Rabih! I’ve updated the post and fixed the typo!

    • Rabih SAIED Reply

      Instead of
      $refreshResponse = Invoke-RestMethod $listUrl -Method ‘POST’ -Headers $refreshHeader

      $refreshResponse = Invoke-RestMethod $refreshUrl -Method ‘POST’ -Headers $refreshHeader
      Best Regards

      • Adrià Ariste Santacreu Reply

        Thanks again, that’s what happens when I change the code I got in PowerShell in the WordPress editor, and then I don’t proofread the post.

  3. Mötz Jensen Reply

    I believe that these commands should be enough, for you to be able to install the during your pipeline execution:

    Install-PackageProvider nuget -Scope CurrentUser -Force -Confirm:$false
    Install-Module -Name AZ -AllowClobber -Scope CurrentUser -Force -Confirm:$False -SkipPublisherCheck

    Install-Module -Name -Scope CurrentUser -Force -Confirm:$false

    • Adrià Ariste Santacreu Reply

      Thanks, Mötz! I’ll update the post with this info!

  4. Sander Holvoet Reply

    I’m running into the following issue upon requesting the token:

    “error”: “invalid_grant”,
    “error_description”: “AADSTS65001: The user or administrator has not consented to use the application with ID ‘…’ named ‘…’. Send an interactive authorization request for this user and resource,
    “error_codes”: [

    I’ve created an App registration in Azure and added the “Dynamics Lifecycle services” permission. As far as I can tell this permission doesn’t require consent of an Azure AD admin..? Or does it?

    Do I still need to be a part of the preview (insiders) to make use of the LCS API?

    • Adrià Ariste Santacreu Reply

      Yes! You still need to join the preview with the user you’ll be using and the LCS project you want to use the API with.

  5. Pingback: Calling the LCS Database Movement API in your Azure DevOps pipeline - Dynamics 365 Finance Community

  6. Pingback: Secure your Azure Pipelines with Azure Key Vault -

  7. Pingback: Llama a la API de Movimiento de DB de LCS en tu pipeline de Azure DevOps -

  8. Pingback: LCS DB API: automating Prod to Dev DB copies -

  9. Can we export PROD database directly to LCS using JSON:
    POST /databasemovement/v1/export/project/{projectId}/environment/{environmentId}/backupName/{backupName}

  10. Pingback: Restore a data package with Azure DevOps Pipelines -

Write A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.