Extending umbraco contour, creating a custom prevalue source type

Yet another post in the Contour series, in this one I’ll outline how I created a new prevalue source type.

By default there are a number of prevalue source types in contour (fetch prevalues from a database table, from umbraco documents, from umbraco datatype prevalues, …), but it’s also possible to extend Contour and create your own fieldtypes, datasource types, field setting types, …. and of course prevalue source types.

So imagine we have a simple text file containing all cities in Belgium and of course we don’t want to manually create a cities dropdown and start copy pasting the contents. Instead we’ll setup a new prevalue source where I can upload the textfile and then hook this prevalue source to the dropdown we wish.

The code to do this (the custom prevalue source type) is actually pretty simple, by first looking at the contour shared sourcecode (which contains the sourcecode for all default elements in contour I have the code in a couple of minutes) here it is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Forms.Core;
using System.IO;
namespace Contour.Addons.ScoreCalclulator
    public class GetValuesFromTextFile: FieldPreValueSourceType 
            description = "File containing the prevalues (seperated by linebreak)", 
            control = "Umbraco.Forms.Core.FieldSetting.File")]
        public string TextFile { get; set; }
        public GetValuesFromTextFile()
            this.Id = new Guid("35C2053E-CBF7-4793-B27C-6E97B7671A2D");
            this.Name = "Get values from textfile";
            this.Description = "Upload textfile that contains the prevalues (seperated by linebreak)";
        public override List<PreValue> GetPreValues(Field field)
            List<PreValue> result = new List<PreValue>();
                int sort = 0;
                TextReader tr = 
                    new StreamReader(HttpContext.Current.Server.MapPath(TextFile));
                string[] values = tr.ReadToEnd().Split(‘\n’);
                foreach (string value in values)
                    if (!string.IsNullOrEmpty(value.Trim()))
                        PreValue pv = new PreValue();
                        pv.Id = sort;
                        pv.Value = value;
                        if (field != null)
                            pv.Field = field.Id;
                        pv.SortOrder = sort;
            catch (Exception ex)
                    "Get values from textfile provider: " + ex.ToString());
            return result;
        public override List<Exception> ValidateSettings()
            List<Exception> exs = new List<Exception>();
            if (string.IsNullOrEmpty(TextFile))
                exs.Add(new Exception("’TextFile’ setting not filled out’"));
            return exs;


The important things to notice here is that we have a single setting which is the TextFile settings and it’s using the Umbraco.Forms.Core.FieldSetting.File control so it will render an upload control.

Then in the GetPrevalues method we’ll simpy read the file and loop trough all values(which should be seperated by a linebreak).

And that’s it, if I now compile this code and place the assembly in the bin directory of the umbraco installation I will be able to select the new prevalue source type when creating a new prevalue source


And once I select the type I’ll be able to upload the text file


Now once the new prevalue source has been setup I’ll be able to add a new field to my form that uses this source.


And after adding the field I should be able to see the prevalues (even in the form designer)


Download the sourcecode here:

And the text file with cities I used:

6 Comments so far

  1. Angel on October 26th, 2010

    Forgive me but I’m new to compiling and have been receiving the error: No rule to make target `”../Documents’ - would you be able to help?

  2. Matt Brailsford on March 5th, 2011

    Hey Tim, I’m using this in a project to easily populate dropdowns. It works great, but when I use recordField.ValuesAsString() it just returns the index of the selected item. Is there a way to get it to return the actual value?

  3. TimNape on April 14th, 2011


    Does every field with a SQL database source get the data directly from that database?

    In that case it will mean multiple calls to the database for fields that can be returned from one view or table.

    Is there a way I can populate fields from one data access class - Mojoportal I think has something like that.

  4. Minna Stavis on March 11th, 2012

    Ik werd verteld in om de organisatie geheel te wijten aan afwijzing te voorkomen. Mensen zouden mij vertellen , ‘ je niet graag een normale baan en een normaal gezin hebben? ‘ Ik denk dat dat zou kunnen advies voor sommige mensen , maar ik wilde handelen.
    Vroeg of laat winnende trades zijn de types die denken dat ze kunnen.

  5. James Drever on March 20th, 2012

    This is fantastic Tim, but one point: in the GetValuesFromTextFile class you need to dispose of the TextReader, otherwise it locks the file and you can’t then later update it. Thanks, James.

  6. Dewi on May 4th, 2012

    Hi Tim, Do you know the issue with Contour when we want to create form tied to database and put prevalue field item, it give an Server Error in ‘/’ Application… ? How should we resolve that? I put in umbraco forum for one week but nobody replied yet.. I had a look for the same problem but cant find the solution..

Leave a Reply