Easy VSTS Integration with Dynamics 365 for Simplified ALM – Part 1

 

When managing a client solution codebase, it can be advantageous to provide a quick and simple method for client Power Users, or for your Business Analysts to synchronise their changes with your solution version control without surfacing additional privileges.

In part 1 of this blog, I will be walking you through the Visual Studio Team Services (VSTS) configuration required to make source synchronisation as simple as clicking a button — leveraging the powerful VSTS Rest Web API. In parts 2 & 3, I will show how you can leverage this VSTS setup to trigger VSTS build events from the web client directly (client side/less secure) using solely JavaScript, and from the server side (more secure) by using JavaScript to fire a custom Dynamics 365 action and custom workflow activity via the Dynamics 365 Web API.

Prerequisites

For this synchronisation scheme to work, you will require the following:

  • A version of Ribbon Workbench (developed by Scott Durow) suitable for your Dynamics 365/CRM instance. This is used to customise the application ribbon and add a clickable button for your clients to use from inside the CRM web client. For this walkthrough, I will be using the D365 version which can be found here: Ribbon Workbench.
  • Access to a CRM instance with adequate privileges to manage/alter/publish solutions. This scheme does not require the Dynamics 365/CRM instance itself to be internet-facing if you employ the client-side execution in Part 2, rather the client machine accessing it as the build is triggered client-side. If you employ the server-side execution as per Part 3, the CRM instance will require internet access.
  • A VSTS instance and associated account with sufficient privileges to queue build events. It is acceptable to use a more privileged account as the Personal Access Token (PAT) can be privilege and instance scoped/limited. However, note that those limited privileges can be leveraged against any VSTS project the user account has access to in the VSTS instance. So ideally, use a service account created for that project (you can add up to 5 basic Microsoft accounts not MSDN backed to any VSTS instance for free) if your instance is backed by an organisation Azure Active Directory please see instructions for adding the basic account as a foreign principal to it here: Add new users or users with Microsoft accounts to Azure Active Directory.

Setup VSTS for Integration

First, we need to configure VSTS to accept REST access for your particular user account. This tutorial assumes you already have an ALM build definition set up in VSTS. This can range from anywhere between simple source synchronisation to full sync, rebuild and deployment. In this barebones example, I will be leveraging a build in my personal VSTS that simply publishes customisations, exports the target solution package, unpacks it and checks it in to source control:

 

The information you will require for the VSTS REST API call is as follows:

  • The name of your VSTS account instance – In this case, my account is found at https://holmes-sms.visualstudio.com, so my account/instance name is holmes-sms.
  • The name of the ALM project whose build definition you wish to trigger – In this example, the name is VSTSIntegration.
  • The unique ID of the build definition you wish to trigger – This can be found by navigating to the variables tab as shown in the images below. The ID for this example is 7.

Note: you must open the build definition for editing to surface this information.

 

  • The Personal Access Token which we will now set up.

Set up the Personal Access Token

  • Navigate to your user account’s settings in the top right of the VSTS interface.
  • Click Security and navigate to Personal access tokens in the left pane, then click Add. You can see that I already have a token set up for this demonstration:

 

  • You will be presented with a creation window. Enter a description of the token meaningful to you (i.e. D365TriggerToken, ScriptAuthToken etc.).
  • Set the expiry of the token (90days, 180days, 1year).
  • Limit the token to the current instance that hosts the project (in my cases holmes-sms).
  • Finally, set the Authorized Scopes to “Selected scopes” and the “Build (read and execute)” privilege, as shown below. Click “Create Token” when you are finished:

 

Upon creation of the token, you will return to the token window where the token secret will be displayed. It will warn you and it is important to copy this token and store it safely before navigating away from the screen, as it is not stored in the instance after creation for security purposes.

 

This concludes the VSTS side of the setup. In the next part of the series, I will cover basic JavaScript-based use of this REST API and the Ribbon Workbench setup required.