Simon Miller Team : Web Development Tags : Web Development

UMBRACO 6 AND THE SERVICECONTEXT

Simon Miller Team : Web Development Tags : Web Development

One of the highlights for me with Umbraco 6 was the new feature of being able to access the Umbraco datastore from an external application that didn’t have direct access to an Umbraco Context. Yes you could achieve this with the WebService layer in the past, but wouldn’t it be nicer if we could just use the standard Umbraco connection string?

This is now possible, though as it’s quite new and not entirely well documented yet (and in various states of being broken up until Umbraco 6.0.3) I thought I would assemble together how I managed to grant a console application complete access to the Content and Media APIs.

1. The base to get you started is a project called UmbConsole, written by Morten Christensen: https://github.com/sitereactor/umbraco-console-example/tree/master/UmbConsole

Have a look at the project to get familiar with the idea, but let’s create a new console application project inside your existing solution, where I assume you already have an Umbraco website up and running.

2. Ensure your website is updated to 6.0.3 or later. This is important as there were problems with 6.0.2 that interfere with the ServiceContext.

3. You will note that the UmbConsole project references many DLLs and has a full App.config. This is overkill. Add references to the following libraries only (again ensuring you have updated to 6.0.3):

  • Businesslogic
  • Cms
  • Controls
  • Interfaces
  • Log4net
  • Umbraco
  • Umbraco.core
  • Umbraco.datalayer
  • Umbraco.editorcontrols
  • Umbraco.web.ui
  • Umbraco.webservices

You will also need the usual references from the GAC:

  • System
  • System.Core
  • System.Data
  • System.Web
  • System.XML
  • System.XML.Linq

4. This part is very important. Ensure you have a folder called Config in the root of your project and copy the umbracoSettings.config file from your website to this folder. Without this file, the Save and Publish methods will fail.

5. Create an app.config. We really don’t need much to make this site work, just a couple of very important sections:

You need to reference a config section FileSystemProviders and add the relevant block below, like so:

 

It is necessary for your console application to know where the physical media folder is for uploading images and files to the Umbraco data store. The attribute rootPath you need to customize to point to the location of your website media folder. The attribute rootUrl leave as shown above.

Copy your umbracoDbDSN from the website web.config verbatim.

6. Copy the ConsoleApplicationBase.cs and ConsoleBootManager.cs from the UmbConsole application to your new application.

7. Examine the UmbConsole application’s Program.cs and instantiate the same services as shown there. This gives us access to the Content, ContentType and Media services.

 8. You now have the base required to work with the Umbraco ServiceContext. I won’t go into the methods available to you as they are fairly self explanatory, but the basics are:

9. Similar methods are available for the MediaService. When working with the MediaService, I did notice that the overloaded method that allows for a MemoryStream to be uploaded to Umbraco did not work for me. Instead, I needed to convert the stream to a HttpPostedFileBase, which is the other accepted overload. Your mileage may vary.

10. One last very important note I nearly ripped my hair out trying to solve (and with big thanks to Wiliam tech lead Matthew Cox for the help here). If the Save or Publish methods fail for you with cryptic errors about loading DataType XML, check the data types of your properties in Umbraco. Are any of them non-standard plugins?

If so, you need to reference the library for the property (in my instance DigibizAdvancedMediaPicker.dll) and lastly, you need to ‘jump start’ the library for inclusion in the context. Take a look in ConsoleBootManager.cs and pay attention to the lines regarding DataTypeResolver. For each non-standard property in your document types that require a library, you need to include them here. For example:

I hope this guide is useful to developers wishing to use the Umbraco Contexts from outside of their website.