Tag Archives: Xamarin Workbooks

Sitecore + Xamarin Workbooks = Interactive Sitecore learning

Xamarin Workbooks is the perfect tool/media for interactive learning. Xamarin Workbooks provides you a blend of documentation and code that is perfect for experimentation, learning, and creating guides and teaching aids. Mainly the idea was to use it to run “Xamarin code” but it also works great for all kinds of .Net libraries, like Scott Hanselmann describes in his post, Xamarin .NET Workbooks – Interactive Computing is a stellar learning tool

The page at https://developer.xamarin.com/workbooks/ is FILLED with amazing example workbooks and lessons, and it’s growing. It has section not only on C# but Android, Games, Graphics as a concept, iOS, WPF, and so much more.

And as Scott says, we can do all kind of workbooks and it’s perfect for learning – interactive learning. So lets do some Sitecoring in the Xamarin Workbooks. 😄

First we need to install and setup Xamarin Workbooks, it’s very easy. Go to Installation and Requirements and follow instructions. (It works on Mac and Windows)

Cool, lets fire it up and select “Console”.

Now you can start playing around 🙂 In order to execute the code, hit the small play button below the “code window”.

You can also reference external dll’s, locally or by Nuget package. To do it locally:
referenceDll

By Nuget package, search for packages by browsing to File > Add Package. Adding a package will automatically bring in #r statements referencing package assemblies, allowing you to use them right away.

referenceNuget

This is very cool, I hope that the Sitecore people will take a look at the Xamarin Workbooks and add it to the Sitecore documentation. I mean interactive documentation – How cool is that 🙂

I’ve created a Sitecore.Workbook at the GitHub – Sitecore-Workbooks. Feel free to download and test it out.
Sitecore.workbook

The cool thing it’s all in markdown, here is the content from the Sitecore.Workbook file 😉

Welcome to Sitecore Workbooks

Workbooks are live documents that mix text, code and results in the same document.

Look at following code,  if you notice there is a “play icon” at the bottom after each “code window”. Hit play and the code will execute 🙂

string test = "<h1>Let's do some Xamarin Workbooks</h1>";

Now we will add the AsHtml()

test.AsHtml();

Ok let’s make a 64base image. First we need some base64 data.

string image = "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0i...";

We will put the image variable into a “html string” and render it as html.

string htmlString = $"<img src="{image}" height="100" />";
htmlString.AsHtml();

Time for some sitecoring

Next we will reference an external dll, we can do that by referencing them locally. We will need the Sitecore.Kernel.dll. In order to make it work, you will need to setup/create a folder with the sitecore dll.

#r "C:\Habitat\dlls\Sitecore.Kernel.dll"

Or we can reference nuget packages. Let’s grab the NSubstitute package.

#r "NSubstitute"

Ok, time for some Sitecore coding. How about we create an Item 🙂

using NSubstitute;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Globalization;

Database db = Substitute.For();
ID itemId = ID.NewID;
Language language = Language.Current;
ItemDefinition definition = new ItemDefinition(itemId, "Some item name", ID.NewID, ID.NewID);
ItemData data = new ItemData(definition, language,Sitecore.Data.Version.First, new FieldList());
Item item = Substitute.For(itemId, data, db);

Sitecore Habitat

Next we will try some Sitecore.Habitat stuff. Lets play with Sitecore.Feature.Search, we will need a reference to the Sitecore.Feature.Search dll

#r "C:\Habitat\dlls\Sitecore.Feature.Search.dll"

Let’s try the PagedSearchResults and see/test that it works properly.

using Sitecore.Feature.Search.Models;

int page = 1;
int totalResults = 10;
int pagesToShow = 1;
int resultsOnPage =2;
PagedSearchResults searchResults = new PagedSearchResults(page, totalResults, pagesToShow, resultsOnPage);
double totalPages = Math.Ceiling(totalResults / (double)resultsOnPage);

searchResults.TotalPagesCount == totalPages;

Sitecore Mobile SDK for Xamarin

Lets start by adding the nuget package, Sitecore.MobileSDK.

#r "Sitecore.MobileSDK"
#r "Sitecore.MobileSDK.PasswordProvider.Interface"

Cool, ok guys. 🙂 Now we have the package, time to do some coding.

How about grab some code from method TestParseCorrectData in AuthenticateResponseParserTest at

To make it easier we will remove the NUnit stuff. Check out the code below, press play and see the result.

using System;
using System.Threading;
using System.Threading.Tasks;
using Sitecore.MobileSDK.API.Exceptions;
using Sitecore.MobileSDK.Authenticate;

var response = "{\"statusCode\":200,\"result\":{}}";
WebApiJsonStatusMessage message = AuthenticateResponseParser.ParseResponse(response, CancellationToken.None);
message.StatusCode.Equals(200);

Try to remove row “message.StatusCode.Equals(200);“ and hit play again

var response = "{\"statusCode\":200,\"result\":{}}";
WebApiJsonStatusMessage message = AuthenticateResponseParser.ParseResponse(response, CancellationToken.None);

That’s all for now folks 🙂