Double Down with CRM 2016: Introducing Two New Features - Web API and Autocomplete

Microsoft Dynamics CRM 2016 introduced two new CRM client-side API additions: Web API and Autocomplete. In today's blog, we will go over both of these new features and show you how they can be used. Let's get started!

Web API

The Web API is replacing the Organization Service. It implements OData v4, which allows the same operations as the Organization Service without needing CRM SDK assemblies. This is a BIG deal, as complex applications can be created across a variety of platforms and programming languages without the limitation of OData v2 CRUD only operations. Actions and Functions are operations performed via HTTP requests. The Organization Service (OData v2 REST endpoint) has been deprecated for CRM 2016.

Here is where you find the new Web API URL: Settings à Customizations à Developer Resources.

Web API and Autocomplete

Want to learn more about the new Web API? Check out this blog post!

Autocomplete

Autocomplete was added to the API in Dynamics CRM 2016. It provides a quick and easy way to pick a value from a predefined list, improves the user experience in forms, and helps with data quality in the system.

In out-of-the-box CRM, the Address fields are all plain text. Country, State and Zip Code fields are good candidates for autocomplete fields. In the steps below, we will demonstrate how to retrieve Countries from a custom entity via the Web API and display it on an autocomplete field. Autocomplete data source is not limited to custom entities. External sources can be used as well.

The setup: Country entity with Name and Country ISO Code fields:

Web API and Autocomplete

Register the loadAutoComplete function in the Form Load event:

Web API and Autocomplete

The code: The function below retrieves all Country names and codes, sorted by country name, form the custom po_country entity and calls the autoCompleteCountries function passing the list of countries as a parameter.

Web API and Autocomplete

The function autoCompleteCountries is implemented with the code below. The autocomplete feature is simple to use, but please note that two methods and one event handler are needed: addOnKeyPress, showAutoComplete and hideAutoComplete.

Web API and Autocomplete

This is what it all looks like in action:

Web API and Autocomplete

Check out the video here to see it all in action:


That's all for the blog today! Keep coming back for more information on the great new features of Microsoft Dynamics CRM.

Happy CRM'ing!

How to Register a Dynamics Application with ADFS

During a recent project, we began developing an application that would use the WebAPI. The application is for a client that is using Dynamics 365 On-Premises. For this setup, we used ADFS 4.0 and Dynamics 365. There is a lot of documentation from Microsoft on this process, if you are familiar with CRM development there are usually some slight differences for doing things in a Dynamics Online versus On-Prem environment. However, sometimes the documentation for on-premises misses a step or two.

The majority of the steps to get an application registered with Active Directory can be found here on MSDN, we're just missing one small detail that can cause some non-descript ADFS errors like this:

In the article, it fails to mention the fact that we need to grant Application Permissions to the application within ADFS. Fortunately, this is a pretty easy thing to do. Just open a PowerShell prompt on your ADFS server and enter the following:

Grant-AdfsApplicationPermission -ClientRoleIdentifier "clientid" -ServerRoleIdentifier "Dynamics URL" -ScopeNames openid

After running the command, you should get a token the next time that you attempt it.

Once we started using the Web API with our API testing console, we examined the token and observed that the token was not issuing a refresh token to use. Microsoft recommends refreshing the token with every call, so this was a problem. Fortunately, this was a problem that a few minutes of research could solve; see the PowerShell below to allow the issuance of the refresh token.

Set-AdfsRelyingPartyTrust -TargetName "RelyingPartyFromADFS" -IssueOAuthRefreshTokensTo AllDevices

If you're still not receiving a refresh token as part of an authentication response after making this change, make sure that the SSOLifetime parameter is greater than the TokenLifetime by running the Get-ADFSProperties PowerShell. A refresh token will not be issued otherwise.

For more Dynamics 365 troubleshooting, how-tos, and tips – check out our blog!

Happy Dynamics 365'ing!