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:

Here’s what the basic authentication would look like.

//Azure Application Client ID
private const string _clientId = "00000000-0000-0000-0000-000000000000";
private const string _resource = "";
private const string _authority = "";
private static string _accessToken;

WebAccountProvider wap =
        await WebAuthenticationCoreManager.FindAccountProviderAsync(
            "", _authority);

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

_accessToken = wtrr.ResponseData[0].Token;

You can check out the code on GitHub:

Web API Examples: