So you've just installed Visual Studio 2015 along with Xamarin, downloaded your targeted Android API through the Android SDK Manager, installed a fresh new Sitecore instance in your local IIS and are willing to dig deep into the Sitecore Mobile SDK. Now what?

There are some "hidden" steps that need to be executed before you successfully connect your Android app to your Sitecore instance. I've completed them all and will share them with you now.

Summing up, you have to:

  1. Enable Sitecore Item Web API - it is disabled by default;
  2. Setup a Visual Studio solution and install Sitecore MobileSDK for Xamarin and Sitecore Mobile SDK Password Provider nuget packages;
  3. Add Internet permission to your Android Manifest file;
  4. Finally, add your Sitecore instance url to the hosts file of the Android emulator pointing to your local machine.

Okay, first things first.

Enable Sitecore Item Web API

After installing Sitecore you have to enable Sitecore Item Web API. It is disabled by default. You should enable it in <SitecoreFolder>\Website\App_Config\Include\Sitecore.ItemWebApi.config.

Simply replace the node:

Getting started with Sitecore Mobile SDK and Xamarin

with:

Getting started with Sitecore Mobile SDK and Xamarin

In order to check if Sitecore Item Web API is enabled just paste the following URL in your browser (don't forget to update the <host_name>): http://<host_name>/-/item/v1/?sc_itemid={A60ACD61-A6DB-4182-8329-C957982CEC74}.

You should get a JSON result in your browser.

For more information, please refer to the Sitecore documentation Sitecore Item Web API 1.0.0 Developer's Guide.

Setup a Visual Studio solution

Create a new Blank App (Android) by File > New > Project > Templates > Visual C# > Android > Blank App (Android).

Give it whatever name you like. I gave the name "SitecoreApp.Droid".

Install SitecoreSDK Xamarin nuget packages

Install Sitecore MobileSDK for Xamarin and Sitecore Mobile SDK Password Provider nuget packages. You can use either the Nuget Package Manager (Tools > Nuget Package Manager > Manage Nuget Packages for Solution...) or install it through Package Manager Console (Tools > Nuget Package Manager > Package Manager Console):

Getting started with Sitecore Mobile SDK and Xamarin

Please, refer to Sitecore MobileSDK for Xamarin and Password Provider for Sitecore for more information.

Add Internet permission to your Android Manifest file

Add Internet permission to your Android Manifest by right-clicking your project in the Solution Explorer > Properties (or click on your project in the Solution Explorer and ALT + ENTER).

Now open the Android Manifest tab and in the Required permissions section mark the option INTERNET. Save and rebuild your solution.

Getting started with Sitecore Mobile SDK and Xamarin

In the MainActivity class (or wherever you prefer) add the following code:

Getting started with Sitecore Mobile SDK and Xamarin

Part of this code was taken from Sitecore Mobile SDK for Xamarin > Getting Started. For some reason, the sample code that Xamarin gives does not work. If you compare their code with mine, you will notice that I replaced the call to the method WebApiVersion("v1") by Site("/sitecore/shell").

Now if you run your project you will get the following error:

Sitecore.MobileSDK.API.Exceptions.RsaHandshakeException: [Sitecore Mobile SDK] Public key not received properly

Basically, what is happening here is that your Android Emulator (I am supposing you are using one) cannot reach your Sitecore instance installed in your local IIS (in my case http://sitecorexamarin.local/). What should you do?

You have to add your Sitecore instance to the hosts file of the Android emulator pointing to your local machine.

Add your Sitecore instance url to the hosts file of the Android emulator

I found an interesting article written by Brad Curtis talking about it. The trickiest part for me was the realization that I had to run the Android Debug Bridge (adb.exe) as Administrator and use my local IP address rather than 10. 0. 2. 2 as mentioned by Brad Curtis in his article. 

Now, let's add your Sitecore instance to the hosts file of the Android emulator. Usually you find the Android Debug Bridge in C:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe. Open cmd.exe (run it as Administrator).

Getting started with Sitecore Mobile SDK and Xamarin

You should see the "remount succeeded" message. If you get a different result than that, make sure you have an Android emulator running.

Getting started with Sitecore Mobile SDK and Xamarin

You should see the "[100%] /system/etc/hosts" message. If you get a different result than that, make sure you have the folder C:\temp with the proper permissions in your local machine.

Now in the file C:\temp\hosts add the following entry (notice that I am using my IP address, you have to use yours):

Getting started with Sitecore Mobile SDK and Xamarin

Another tricky part here is that you need to add an extra line after the last entry. Otherwise the Emulator will not recognize it.

Now push the hosts file to your Android emulator.

Getting started with Sitecore Mobile SDK and Xamarin

You should see the "[100%] /system/etc/hosts" message.

Restart the emulator and browse to your entered hostname in the Android emulator browser.

Getting started with Sitecore Mobile SDK and Xamarin

Last step is to run your app again. You should see the message "Sitecore Experience Platform" at the bottom of Android emulator.

Hope this article helps you and saves you from a lot of unneeded frustration.

comments powered by Disqus