Dynamics CRM Developers: Build Your Own Mobile Apps for Windows, iOS, and Android Part 4


As I promised in the last article, I will explain how to develop a Xamarin app using Visual studio. If you haven’t read previous articles yet, I recommend you read them all:

Part 1 | Part 2 | Part 3

Before starting, please note that Dynamics CRM Online 2015 Update 1 released WebAPI developer preview in CRM 7.1 (Carina) supports OData V4, so in the future you may want to build your application on top of it. We’ll address that in a future blog post.

Develop Xamarin.iOS application

For this article we’ll assume you already know what Xamarin is as well as the basics of Xamarin.iOS. For this article I used Visual Studio 2013, but you could also use Xamarin Studio.

Create a solution and add references

1. Create new project, and select iOS | iPhone | Single View App (iPhone).

image

2. Enter Name and click “OK”. I named it Crm.iOS.

3. Right click the project in solution explorer and click “Manage NuGet Packages..” to launch the NuGet Package Explorer.

4. In the NuGet Package explorer, make sure you select nuget.org on the left for the search source, then Search for: Json.NET and install it.

5. Next, change release category to “Include Prerelease” and search ADAL. Select “Active Directory Authentication Library”, and confirm the version is version 3.3 or higher, then install the package (ADAL v3.3+ has support for Xamarin.iOS).

image

6. Once you’ve installed the above components, in the solution explorer pane of Visual Studio, Right-click References and click “Add Reference”.

7. Select following additional assemblies, then press click “OK”.
- System.Net
- System.Net.Http
- System.Runtime.Serialization
- System.Xml.Linq

8. Right click the project and click Add | New Folder. Name it as CRMSDK.

9. Open browser and go to https://code.msdn.microsoft.com/Mobile-Development-Helper-3213e2e6. Download the sample and extract it all to a known location.

image

10. In the solution explorer right click the the CRMSDK Folder in the Visual Studio solution explorer under the project, select Add, Existing Item, select all the .cs files you extracted above except CRMHelper.cs .

image

11. Once completed, double click Microsoft.Xrm.Sdk,Samples.cs to open the cs file.

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

13. Compile the solution once to see if you don’t get any error message.

Change UI and add code.

Next, you update existing UI and code.

1. Double click to open MainStoryboard.storyboard.

2. Drag and drop a Button control from Toolbox to UI surface, and change the button text to “Login”. Drag and drop a Text Field as well. Name the Text Field as “txtUserName”.

image

3. Double click Login button to generate “TouchUpInside event in controller.

image

4. Add following class properties. Change serverUri to your CRM organization Uri. You will replace clientId later.

private string commonAuthority = "https://login.windows.net/common";
private string clientId = "9e36414a-af12-4d52-989e-564ea978bdd9";
private Uri returnUri = new Uri("http://crmxamarin");
private string serverUri = "https://crm2015training10.crm7.dynamics.com";
private AuthenticationResult authResult;

5. Add following using statements at the top of the code.

using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Xrm.Sdk.Samples;
using Microsoft.Crm.Sdk.Messages.Samples;
using Microsoft.Xrm.Sdk.Query.Samples;
using System.Threading.Tasks;

6. Add following method which get AccessToken by using ADAL.

public async Task Authenticate()
{
    AuthenticationContext authContext = new AuthenticationContext(commonAuthority);
    if (authContext.TokenCache.ReadItems().Count() > 0)
        authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
    authResult = await authContext.AcquireTokenAsync(serverUri, clientId, returnUri, new PlatformParameters(this));           
}

7. Add following method which get UserName by using CRM Mobile Helper.

public async Task GetUserName()
{
    OrganizationDataWebServiceProxy proxy = new OrganizationDataWebServiceProxy();
    proxy.ServiceUrl = serverUri;
    proxy.AccessToken = authResult.AccessToken;

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

    var user = await proxy.Retrieve("systemuser", result.UserId, new ColumnSet("fullname"));

    txtUserName.Text = user["fullname"].ToString();
}

8. Update TouchUpInside event like following.

async partial void UIButton5_TouchUpInside(UIButton sender)
{
    await Authenticate();
    await GetUserName();
}

9. Compile the solution to make sure you got no errors.

Register Application to Azure AD

To use OAuth 2.0, you need to register your app, which I explain the details in previous articles.
To simply the steps, I omit the steps here. If you can register application, replace the ClientId in the code above, otherwise you can use the clientid which registered to my own azure ad.

Run the application

Finally, you are able to test the application. Run the application using either device or simulator. I use simulator here.

1. Select iPhoneSimulator and select any simulator. I selected iPhone 6 iOS 8.1. Press F5 to start.

image

2. Once simulator launched and application started, click “Login” button.

image

3. SignIn page will be shown. Enter credential to connect to Dynamics CRM.

4. After you signing in, user name will be displayed to the text field.

image

Next Action

In this article, I only used WhoAmI and Retrieve method. Please try more complex scenario by your self. Xamarin.Android or even Xamarin.Forms works in similar way as it’s C# application. I will demonstrate how to build an application by using WebAPI in the next article.

References

ADAL sample for Multi-Platform https://github.com/AzureADSamples/NativeClient-MultiTarget-DotNet

Ken
Premier Mission Critical/Premier Field Engineer 
Microsoft Japan

Comments (1)

  1. Nagesh says:

    Thank you for sharing this article. Have shared the link in my social networks.

Skip to main content