Connecting Xamarin Form Application with Dynamics CRM

Xamarin is a Cross platform framework for mobile development and lets you build iOS, Android and Windows Phone applications using C#. Applications can share up to 90% of their code since, Xamarin Mobile library offers a unified API to access common resources across all three platforms and by using Xamarin.forms the UI code can also be made common. This can reduce development cost as well as time.

In this blog post, we’ll connect Dynamic CRM with Xamarin Form application using ADAL authentication and CRM mobile helper library. Once done, we shall execute the WhoAmIRequest.

1. Creating Xamarin.Forms App

Create a Xamarin.Forms app in Xamarin Studio or Visual Studio.

To do so create new project and select Blank App (Xamarin.Forms Portable). Give a proper name and click OK.

1.newProject

It will create six projects: PCL (Portable Class Library), Android, iOS, and Windows Phone.

8.projectsCreated2. Adding ADAL library

Add ADAL library version 3.5.207081303-alpha to each project. Get it from this link.

9.ADAL

3. Adding Microsoft.Crm.Sdk.Mobile sample

Add Microsoft.Crm.Sdk.Mobile sample. It is a partial re-implementation of the Microsoft Dynamics CRM SDK classes written as a Portable Class Library to facilitate development in phones. Get it from Link.

To add this library into your project, Right click the portable project and click Add | New Folder. Name it as MobileHelper.

Then right click the MobileHelper Folder, Select Add, Existing Item, select all the .cs files from the library downloaded from the link except CRMHelper.cs

10.mobilehelper

Double click Microsoft.Xrm.Sdk.Samples.cs to open the cs file.

Comment out line 35, and 666-682. There lines are for Windows Store/Phone application and Xamarin does not understand it, and Build the solution.

4. Create IAuthenticator Interface

Whenever we use platform specific functions in Xamarin form we use DependencyService. It is used to call specific functions from shared code (Portable Project). The first component is to create Interface.

The interface is used to interact with platform-specific functionality. The required functionality is defined by an interface in shared code.

Add a new interface to your PCL Project with the Authenticate method.

To add a new interface right click the PCL project then click “Add” and then click “New Item”. Select Interface from the list, give name “IAuthenticator”.

2.IAuthenticator

 

 

Add the Authenticate function to your interface.

public interface IAuthenticator

{

Task<AuthenticationResult> Authenticate(string authority, string serviceURL, string clientId, string returnURL, string username, string password);

}

5. Implementation per Platform:

The interface should be implemented per platform

Implementation of Authentication method for android

Add a new class to your android project and name it “Authenticator”.

Add the following function to the “Authenticator” class, which is the implementation of the “Authentication method” in Android as given below.

public async Task<AuthenticationResult> Authenticate(string authority, string serviceURL, string clientId, string returnURL, string username, string password)

{

var authContext = new AuthenticationContext(authority);

var platformParams = new PlatformParameters((Activity)Forms.Context);

var authResult = await authContext.AcquireTokenAsync(serviceURL, clientId, new Uri(returnURL), platformParams);

return authResult;

}

We also need to override the OnActivityResult method in the MainActivity.cs in Android project. The purpose of this method is to continue the authentication flow.

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)

{

base.OnActivityResult(requestCode, resultCode, data);

AuthenticationAgentContinuationHelper.SetAuthenticationAgentContinuationEventArgs(requestCode, resultCode, data);

}

 Implementation of Authentication method for iOS

Add a new class to your IOS project and name it “Authenticator”.

Add the following function to the “Authenticator” class, which is the implementation of the “Authentication method” in IOS as shown below.

public async Task<AuthenticationResult> Authenticate(string authority, string serviceURL, string clientId, string returnURL, string username, string password)

{

var authContext = new AuthenticationContext(authority);

var uri = new Uri(returnURL);

var platformParams = new PlatformParameters();

var authResult = await authContext.AcquireTokenAsync(serviceURL, clientId, new Uri(returnURL), platformParams);

return authResult;
      }

No need to implement function to continue the authentication flow in IOS.

Implementation of Authentication method for Windows phone

Add a new class to your Window Phone project and name it “Authenticator”.

Add the following function to the “Authenticator” which is the implementation of the “Authentication method” in Windows phone as shown below.

public async Task<AuthenticationResult> Authenticate(string authority, string serviceURL, string clientId, string returnURL, string username, string password)

{

var authContext = new AuthenticationContext(authority);

var uri = new Uri(returnURL);

var platformParams = new PlatformParameters();

var authResult = await authContext.AcquireTokenAsync(serviceURL, clientId, new Uri(returnURL), platformParams);

return authResult;

}

Windows Phone also need to implement function to continue authentication, add the following method to the App.xaml.cs file.


protected override void OnActivated(IActivatedEventArgs args)

{

#if WINDOWS_PHONE_APP

if (args is IWebAuthenticationBrokerContinuationEventArgs)

{

WebAuthenticationBrokerContinuationHelper.SetWebAuthenticationBrokerContinuationEventArgs(args as IWebAuthenticationBrokerContinuationEventArgs);

}

#endif

base.OnActivated(args);

}

6. Registration of the platform implementation

Each platform implementation of the Interface “IAuthenticator” need to be registered with the DependencyService.

All you need to do is add the assembly reference above your class definition and outside of any namespace definition that may be contained within that file as well.

For android:


[assembly: Xamarin.Forms.Dependency(typeof(XamarinWithCRM.Droid.Authenticator))]

For IOS:


[assembly: Xamarin.Forms.Dependency(typeof(XamarinWithCRM.iOS.Authenticator))]

For Windows phone:


[assembly: Xamarin.Forms.Dependency(typeof(XamarinWithCRM.WinPhone.Authenticator))]

After registration, your Authenticator Class would look like following

In case of Android:

using Microsoft.IdentityModel.Clients.ActiveDirectory;

using System.Threading.Tasks;

[assembly: Xamarin.Forms.Dependency(typeof(XamarinWithCRM.Droid.Authenticator))]

namespace XamarinWithCRM.Droid

{

class Authenticator :IAuthenticator

{

public async Task<AuthenticationResult> Authenticate(string authority, string serviceURL, string clientId, string returnURL, string username, string password)

{

var authContext = new AuthenticationContext(authority);

var authResult = await authContext.AcquireTokenAsync(serviceURL, clientId, new UserCredential(username, password));

return authResult;

}

}

}

7. Calling the interface.

Add a new class named Constants which will contain the information’s for accessing token as shown below.


namespace XamarinWithCRM

{

public class Constants

{

public static string Authority { get { return "https://login.windows.net/common"; } }

public static string ClientId { get { return "client id of your app registered in azure"; } }

public static string RedirectURL { get { return "redirect URL of your app registered in azure"; } }

public static string ServiceURL { get { return "your CRM organization URL"; } }

public static string Username { get { return "User id of your CRM Org"; } }

public static string Password { get { return "password of your CRM Org"; } }

}

}

Add a new Xaml Page to your Portable project, and name it “MyPage”.

3.xamlPage

 

 

Add the following line and comment all other code in the constructor of the App.cs file.


MainPage = new MyPage();

This will call “MyPage” when we execute our App.

Add a Button to the MyPage.

<?xml version="1.0" encoding="utf-8" ?>

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"             x:Class="XamarinWithCRM.MyPage">  <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />

<Button x:Name="myButton" Text="Sign in to CRM"></Button>

</ContentPage>

Now we have to add the Clicked method to the MyPage.xaml.cs file. In this method we will use the Get function of the DependencyService for authentication. And we will call another function from it which will execute the WhoAmIRequest.

MyPage.xaml.cs file should look like shown below.


using Microsoft.Crm.Sdk.Messages.Samples;

using Microsoft.IdentityModel.Clients.ActiveDirectory;

using Microsoft.Xrm.Sdk.Samples;

using System;

using System.Threading.Tasks;

using Xamarin.Forms;

namespace XamarinWithCRM

{

public partial class MyPage : ContentPage

{

AuthenticationResult authResult;

private OrganizationDataWebServiceProxy proxy;

public MyPage()

{

InitializeComponent();

myButton.Clicked += MyButton_Clicked;

proxy = new OrganizationDataWebServiceProxy();

}

async void MyButton_Clicked(object sender, EventArgs e)

{

try

{

var auth = DependencyService.Get&amp;lt;IAuthenticator&amp;gt;();

authResult = await auth.Authenticate(Constants.Authority,

Constants.ServiceURL,

Constants.ClientId,

Constants.RedirectURL,

Constants.Username,

Constants.Password);

}

catch (Exception ex)

{

await DisplayAlert("Error", "This: " + ex, "got it");

}

//Initializing our Proxy

proxy.ServiceUrl = Constants.ServiceURL;

proxy.AccessToken = authResult.AccessToken;

await GetLoggedInUserName();

}

public async Task GetLoggedInUserName()

{

try

{

WhoAmIResponse result = (WhoAmIResponse)await proxy.Execute(new WhoAmIRequest());

await DisplayAlert("Result \n", "User Id : " + result.UserId.ToString() + "\n OrganizationId: " + result.OrganizationId.ToString() + "\n BusinessUnitId: " + result.BusinessUnitId.ToString(), "Ok", "Cancel");

}

catch (Exception ex)

{

await DisplayAlert("Error", "This: " + ex, "got it");

}

}

}

}

In the Clicked method the DependencyService.Get<T> function will find the correct implementation of interface T, and will call the Authenticate method of that Implementation.

 

Now, we can use the Dependency Service from Xamarin.Forms to locate the Authenticate method in each platform and execute it.

The first screen should look like shown below.

4.runProject

 

 

Click the button ”Sign in to CRM”, and you will have the following screen in which the ADAL function “AcquireTokenAsync” used in the Authenticator function will ask you for your CRM Credentials.

5.signIntoCRM

 

Add you CRM credentials and Click Sign in.

In the following screenshot you can see the AccessToken.

6.accessToken

And the response of the WhoAmIRequest is shown below

11.result

 

In the next blog post we will retrieve the data from Dynamics CRM and show it in the list view.



8 Comments

Leave a Reply