Monday, November 23, 2015

CRM Web API Universal Windows Platform Using C#

In this example I’m demonstrating how to use the new CRM Web API from a Universal Windows Platform (UWP) application. If you aren’t familiar with the “universal” application concept its Microsoft’s framework for writing a single application that can be run on all the modern Windows devices like phones, tablets, Xbox One, IoT, PC, Surface Hub, etc.

In the application itself the code to call the CRM Web API looks identical to the C# example I had previously posted. The real difference is that this time around we're not using ADAL to handle authentication. UWP applications can use the new WebAccountProvider which is native to Windows 10. If you’re targeting also targeting previous versions of Windows you’ll need to fall back to ADAL. You can check out a great blog post on the subject here:
http://blogs.technet.com/b/ad/archive/2015/08/03/develop-windows-universal-apps-with-azure-ad-and-the-windows-10-identity-api.aspx

Here’s what the basic authentication would look like.

//Azure Application Client ID
private const string _clientId = "00000000-0000-0000-0000-000000000000";
//CRM URL
private const string _resource = "https://org.crm.dynamics.com";
//Azure Directory OAUTH 2.0 AUTHORIZATION ENDPOINT
private const string _authority = "https://login.microsoftonline.com/00000000-0000-0000-0000-000000000000";
private static string _accessToken;

WebAccountProvider wap =
        await WebAuthenticationCoreManager.FindAccountProviderAsync(
            "https://login.microsoft.com", _authority);

WebTokenRequest wtr = new WebTokenRequest(wap, string.Empty, _clientId);
wtr.Properties.Add("resource", _resource);
WebTokenRequestResult wtrr = await 
    WebAuthenticationCoreManager.RequestTokenAsync(wtr);

_accessToken = wtrr.ResponseData[0].Token;

You can check out the code on GitHub:

Web API Examples: