Xamarin – Change default navigation controller from table to view controller

By default Visual Studio will create a Navigation Control with an attached TableViewController. However if you need a different View Controller it’s quite simple to change it.

You can just delete the TableViewController, Drag and drop a new UIViewController into the StoryBoard, then (holding down Control) Click and drag a “connection” from your NavigationController to the new UIViewController you just added and select “root view”

Connecting to Azure Virtual Machine using alternative port

This is easy when using Classic mode for deploying your virtual machine. Once deployment is complete the RDP port will default to 3389.

To change this:

  1. Select Virtual machines (Classic).
  2. Select the virtual machine you want to change the port for.
  3. Select Endpoints.
  4. Change the public port for both RDP TCP and RDP UDP endpoints to the desired number. DO NOT change the private port.

Sitecore 9 – Whats New?

Sitecore revealed the latest version of their customer experience platform at the Los Vegas Symposium 2017, Sitecore 9. The following is brief overview of whats available.

The name!

Sitecore have changed the name from Customer Experience Platform to Experience Cloud.

Sitecore xConnect

xConnect is a new API which allows developers to integrate data collected by Sitecore with other third party sources. Reading data from the xDB has now been standardised via the xConnect service layer.

Sitecore Cortex

Cortex is a new machine learning engine purpose build for Sitecore. It helps discover new audiences and is able run multi-varient tests. Future development of Cortex will bring automated personalisation and is expect to make an appearance towards the latter part of 2018.

Commerce

First quarter 2018 will bring Sitecore Commerce 9. This will bring a more mature and fully integrate e-commerce solution. Build in .Net Core the new solution will replace Commerce Server. The old store front will be replaced by SXA compomants.

Cloud

Microsoft and Sitecore have improved the offerings available from the Azure Marketplace. Its now possible to pick your Sitecore version (all 8.2’s and 9) along with topology and size. While is a simple and quick way to spin up a Sitecore install your are limited to pre-made packages. For customer installs, its still advisable to use the Azure Resource Manager (ARM) templates instead.

Sitecore Forms

Web Forms For Marketers (WFFM) has finally been replaced. Sitecore Forms has new look with full drag and drop support. It also includes multipage forms and conditional logic.

Federated Authentication

Sitecore finally offers federated authentication via OWIN. This provides integration with SAML, Facebook, OAuth, WS-Federation and ADD.

SPEAK 3

Sitecore is moving away from its proprietary UI framework (SPEAK) to a commodity based strategy, Angular First.

Sitecore Installation Framework (SIF)

Installing Sitecore 9 is different from previous versions. The single executable installation method is now gone. Instead all individual services have been split into their own components. To try and simplify the installation process Sitecore created an installation framework called SIF. It combines Powershell and JSON configuration files to build Sitecore environments. By doing this Sitecore have simplified DevOps and automated deployments.

Sitecore JSS

Sitecore’s JavaScript Services is an API layer on top of Sitecore and lets you hook multiple JavaScript frameworks to Sitecore like React and Vue. Using an OData REST service and API key management, security and data abstraction are also ensured. Together with xConnect, there’s no shortage in what third-party systems or IoT devices you can connect to anymore. By the way, mind that JSS is still in Tech Preview for the Initial Release of 9.0!

Dynamic Placeholders

Until dynamic placeholders have only been available using a Sitecore module or custom code. Sitecore 9 now has built in support.

XDB SQL Support

Sitecore 9 can now write analytics data directly to a SQL Database without going via MongoDB first. In fact the initial release of Sitecore 9 has dropped MongoDB completely, however support will be included in future releases.

Sitecore Zenith and Horizon

While its not included yet, Horizon will change the way content editors interact with Sitecore. This will replace both the Content Editor and Experience Editor with a single solution, providing full drag and drop, device emulation, heat maps and machine learning.

Change a VirtualBox dynamic disk size on MacOS

When creating a dynamic disk VirtualBox asks you to specify the maximum size. If the max dynamic disk size needs changing after its initial creation we cannot use the VirtualBox interface, instead we need the VBoxManage command line tool.

1. Shutdown the VM and quit VirtualBox
2. Open the Terminal app and use the following command to navigate to the VirtualBox app directory
cd /Applications/VirtualBox.app/Contents/Resources/VirtualBoxVM.app/Contents/MacOS/
3. Now in the proper directory, you’re ready to run the resize command with the following syntax:
VBoxManage modifyhd –resize [new size in MB] [/path/to/vdi]
Example:
VBoxManage modifyhd –resize 30000 ~/Documents/VM/Windows10.vdi
4. If desired, verify the change has taken place with the showhdinfo command:
5. Launch VirtualBox and boot renewly resized VM

Reclaiming space from VirtualBox on MacOS

Dynamic disks tend to be the preferred choice for virtual disk images as they only use the physic disk space they need. However while VirtualBox is a very good free virtual machine application, the dynamic disks are much better expanding than shrinking. While commercial tools such as VMWare and Parallels have a one click/automatic way to reclaim physical disk space, VirtualBox needs a little manual intervention.

The following steps will give you some physical disk space back, its a good idea to run these after a major service pack install or after removing a large application from your virtual machine. Steps 1-5 is recommend regardless of which VM application you are running as this frees space on the virtual machine.

1. Delete unwanted files
2. Run Windows Clean Up, use the advanced option to remove windows update files.
3. Empty Recycle Bin
4. Defrag
5. Clean garbage bits and bytes using SDelete by Windows Sysinternals, after installation open cmd and type

sdelete.exe C: -z

6. Shutdown Windows VM and close VirtualBox
7. Open Terminal app and enter

cd /Applications/VirtualBox.app/Contents/Resources/VirtualBoxVM.app/Contents/MacOS/

8. Then enter

VBoxManage modifyhd –compact “[/path/to/vdi]”

Retrieving site information in Sitecore

Sitecore provides functions to get the root path and start item for the current context. So when working with multisite solutions you should always get a path relative to the Sitecore.Context.Site settings:

Response.Write("Current site: " + Sitecore.Context.Site.Name + "<br/>");
Response.Write("Database: " + Sitecore.Context.Site.Database.Name + "<br/>");
Response.Write("Content start path: " + Sitecore.Context.Site.ContentStartPath + "<br/>");
Response.Write("Home node: " + Sitecore.Context.Site.StartPath + "<br/>");
Response.Write("Language: " + Sitecore.Context.Language.GetDisplayName() + "<br/>");

The code above would give me the following output for the site at site1.mysite.com:

Current site: website_1
Database: web
Content start path: /sitecore/content/website1
Home node: /sitecore/content/website1/home
Language: English : English

Finding a Sitecore item by ID using a Solr query

By default the _group field contains the Sitecore ID for a specific item. To search for an item with an ID of {799539AB-3495-4D40-9B2F-A394A87960D6} use the following Solr query:

_template:”799539ab34954d409b2fa394a87960d6″

Please note the ID is not stored in the group field as a standard GUID. For the search to work hyphens need removing and converting to lower case.

However this will only work for an item with a single version and language. As Sitecore uses the same item for each piece of content, regardless of the number of versions or languages, the above query may return multiple results.

In this case we need to use the _uniqueid field to return the exact item representing a specific version and language. In this example we change the Solr query to:

_uniqueid:”sitecore://master/{799539ab-3495-4d40-9b2f-a394a87960d6}?lang=en&ver=1&ndx=sitecore_master_index”,

Dependency Inversion Principle, Inversion of Control and Dependency Injection

To understand Dependency injection (DI) we need understand two things. First Dependency Inversion Principle (DIP), second Inversion of Controls (IoC). We’ll start with DIP.

Dependency Inversion Principle

By conforming with the Dependency Inversion Principle guideline it allows use to write loosely coupled classes. The definition of DIP states:

1. High-level modules should not depend on low-level modules. Both should depend on abstractions.
2. Abstractions should not depend upon details. Details should depend upon abstractions.

The following is an example of a service which writes errors to a message log. The example consists of two classes, one writes messages to the error log, the other watches for errors being thrown by the system.

class WriteToErrorLog
{
	public void WriteEntry(string errorMessage)
	{
		// Write to error log here
	}
}

class SystemErrorWatcher
{
	// Handle for WriteToErrorLog to write error logs
	WriteToErrorLog writeToErrorLog = null;
	// This method will be called when the system encounters a problem
	public void Notify(string message)
	{
		if (writeToErrorLog == null)
		{
			writeToErrorLog = new WriteToErrorLog();
		}
		writeToErrorLog.WriteEntry(message);
	}
}

While this code is perfectly legit and deals with the job in hand, it violates DIP. The SystemErrorWatcher depends on the WriteToErrorLog class, which is concrete rather than an abstract class.

This wouldn’t necessarily be an issue if we stopped there. However we may want to introduce some extra functionality, such sending an email if a specific error occurs. We could create a SendEmail class who’s job is to send an emails. The handle could then be created in SystemErrorWatcher. But at any point in time we will only be using the WriteToErrorLog or SendEmail class.

DIP states we need to decouple a system so that higher level models such as the SystemErrorWatcher will depend on simple abstraction. This abstraction will be mapped to a concrete class which make things happen.

Inversion of Control

The IoC principle allows us to develop higher level modules which depend on abstractions rather than concrete classes.

So using IoC we amend the above example to decouple the SystemErrorWatcher class from the WriteToErrorLog class. To do this we need to create an interface which provides the abstraction to act upon notifications received from the SystemErrorWatcher.

public interface INofificationAction
{
	public void ActOnNotification(string message);
}

Next change the SystemErrorWatcher to use the above interface

class SystemErrorWatcher
{
           // Handle for WriteToErrorLog to write error logs
           INofificationAction doSomething = null;
           // This method will be called when the system encounters a problem
           public void Notify(string message)
           {
                  if (doSomething == null)
                  {
                         // We need to map the abstraction to the concrete class here
                  }
                   doSomething.ActOnNotification(message);
           }
}

To complete this change we now need to update the WriteToErrorLog class so it implements the new INofificationAction interface.

class WriteToErrorLog : INofificationAction
{
	public void ActOnNotification(string errorMessage)
	{
		// Write to error log here
	}
}

In addition to emails we may want to send an SMS. Now we’ve decoupled we can create two concrete classes, one for email the other SMS which both implement the INofificationAction interface.

class EmailSender : INofificationAction
{
	public void ActOnNotification(string message)
	{
		// Send email from here
	}
}
class SMSSender : INofificationAction
{
	public void ActOnNotification(string message)
	{
		// Send SMS from here
	}
}

By doing this the high level models are now dependent on lower level abstractions rather than concrete classes. This is exactly what dependency inversion principle states.

However we still have one piece of the puzzle missing, we never create the concrete type to make something happen. We could modify the class to look like this.

class SystemErrorWatcher
{
      // Handle for WriteToErrorLog to write error logs
      INofificationAction doSomething = null;
      // This method will be called when the system encounters a problem
      public void Notify(string message)
      {
            if (doSomething == null)
            {
                   // We need to map the abstraction to the concrete class here
                   writeToErrorLog = new WriteToErrorLog();
            }
            doSomething.ActOnNotification(message);
      }
}

But by doing this we’ve introduced another dependency. This is where Dependency injection comes in.

Dependency Injection

Dependency Injection is mainly used for injecting concrete implementations into a class which is using abstractions. This will allow us to reduce the coupling between classes and move the binding between interface and concrete class out of the dependent class.

DI come in three forms, Constructor Injection, Method Injection and Property Injection.

Constructor Injection

This allows use to pass in the concrete implementation to the dependent classes constructor. We then assign this the interface created with in this classes. By doing this we can pass in any concrete class which is implements the interface we need to bind to. In our example this will allow us to pass in WriteToErrorLog, EmailSender and SMSSender concrete classes as these all implement the INofificationAction interface.

class SystemErrorWatcher
{
      // Handle for WriteToErrorLog to write error logs
      INofificationAction doSomething = null;
      public SystemErrorWatcher(INofificationAction concreteImplementation)
      {
            doSomething = concreteImplementation
      }
      // This method will be called when the system encounters a problem
      public void Notify(string message)
      {
            doSomething.ActOnNotification(message);
      }
}

The calling class can now do

WriteToErrorLog writeToErrorLog = new WriteToErrorLog();
SystemErrorWatcher systemErrorWatcher = new SystemErrorWatcher(writeToErrorLog); 
systemErrorWatcher.Notify("Something has broken");

Now if we want to send an email instead of writing to the error log.

EmailSender emailSender = new EmailSender();
SystemErrorWatcher systemErrorWatcher = new SystemErrorWatcher(emailSender); 
systemErrorWatcher.Notify("Something has broken");

Method Injection

This is similar to Constructor Injection but this time we pass the concrete class to the method rather than constructor. We use this if we don’t want the dependent class to use the same concrete class for it’s entire lifetime.

class SystemErrorWatcher
{
      // Handle for WriteToErrorLog to write error logs
      INofificationAction doSomething = null;

      // This method will be called when the system encounters a problem
      public void Notify(INofificationAction concreteImplementation, string message)
      {
            this.doSomething = concreteImplementation
            doSomething.ActOnNotification(message);
      }
}

The calling class now becomes

WriteToErrorLog writeToErrorLog = new WriteToErrorLog();
SystemErrorWatcher systemErrorWatcher = new SystemErrorWatcher(); 
systemErrorWatcher.Notify(writeToErrorLog, "Something has broken");

Property Injection

If the responsibility of selection of the concrete class and invocation methods are in separate places, we need to use property injection.

Here was pass the concrete class via a setter that’s exposed by the dependent class. To do this the SystemErrorWatcher class will look like this.

class SystemErrorWatcher
{
      // Handle for WriteToErrorLog to write error logs
      INofificationAction doSomething = null;

      public INofificationAction DoSomething
      {
            get
            {
                  return action
            }
            set
            {
                  action = value
            }
      }

      // This method will be called when the system encounters a problem
      public void Notify(string message)
      {
            doSomething.ActOnNotification(message);
      }
}

The calling class

SystemErrorWatcher systemErrorWatcher = new SystemErrorWatcher();
systemErrorWatcher.DoSomething = new WriteToErrorLog();
systemErrorWatcher.Notify("Something has broken");

If we want to send an SMS.

SystemErrorWatcher systemErrorWatcher = new SystemErrorWatcher();
systemErrorWatcher.DoSomething = new SMSSender();
systemErrorWatcher.Notify("Something has broken");

Unpublish a Sitecore item

Any regular Sitecore users knows there are many ways to publish an item. However it’s not always obvious how to “unpublish” an item. Its actually quite straight forward.

1. Navigate to the item you want to unpublish using the Content Editor. From the Publish ribbon select the Change button:

In change dialog, make the item unpublishable by deselecting the Publishable checkbox:

The content editor will display are warning informing you what’s about to happen.

The final step to actually publish the item and it will disappear from the front-end but remain in the master database. This may sound strange, but as the item is already published we need to publish again to remove it from the web database.