How To Manually Access Data Stored Within Vorto Properties Within Umbraco

In this tutorial, you will learn how to access multi-language data in your controllers using Vorto manually.  It should be noted that this approach isn't the easiest or nicest approach.  - for that I recommend you check out this article - if you have some requirement where you need something slightly different than plain out of the box display the current language then manually getting the data yourself is probably your best option

How Can I Use Vorto Within A Controller

When you create a property using Vorto on a document type and use Model Builder, it will get created as an Our.Umbraco.Vorto.Models.VortoValue.  If you look at the class you'll see that it uses a collection of key-value pairs to store its data. 

This means that if you want to access the content you need to iterate through this collection, match the key (culture) to the pages current culture and then return the value:

        public string GetLocalString(VortoValue value)
        {
            if (value == null || value.Values == null)
            {
                return string.Empty;
            }

            foreach (var item in value.Values)
            {
                if (Thread.CurrentThread.CurrentCulture.Name == item.Key)
                {
                    return item.Value.ToString();
                }
            }

            return string.Empty;
        }

Getting Data From More Complex Properties

Getting a string/single value out of Vorto is simple. If you wrap say the related links property, which contains a collection of items, things get slightly more complex when you want to get the data.

For complex properties, Vorto serialises the properties and stores all the data as JSON. If you have a complex property that you want to get data from, you will need to use Json.NET and manually deserialise the data yourself. For related links, you can create a class that mirrors all the properties within the picker:

    public class RelatedLinkJson
    {
        public string caption { get; set; }

        public string link { get; set; }

        public string newWindow { get; set; }

        public string edit { get; set; }

        public string isInternal { get; set; }

        public string type { get; set; }

        public string title { get; set; }
    }

You still get the data out of Vorto the same, however, this time you use JSON.net on the property and deserialise it.

        public ILinkViewModel GetLink(VortoValue link)
        {
            if (link == null || link.Values == null)
            {
                return null;
            }

            foreach (var item in link.Values)
            {
                if (Thread.CurrentThread.CurrentCulture.Name == item.Key)
                {
                    var linkObject = JsonConvert.DeserializeObject<List>(item.Value.ToString()).FirstOrDefault();
                    return new LinkViewModel
                    {
                        Name = linkObject.caption,
                        Url = linkObject.link
                    };
                }
            }

            return null;
        }

Getting an IPublished Content Out Of A Property

When you wrap slightly more complex properties with Vorto, like nest content, content picker, you can see an Umbraco UDI being returned. In those situations, you can use this snippet (more info about Umbraco UDI here):

        public IPublishedContent GetPublishedContentFromContentPicker(VortoValue picker)
        {
            var id = GetLocalString(picker);

            Udi udi;
            if (Udi.TryParse(id, out udi))
            {
                return udi.ToPublishedContent();
            }
            return null; 
          }

Now you should have the value the content editor added for that language and you are now free to do any extra processing that you need, enjoy!

 

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top