Storing Lists in Sitecore config
and utilizing the config Factory
Sometimes you want to store lists in sitecore config files. You could just make a settings and store your values | or , separated. This is not cleanest solutions however, and it makes it hard to add comments to your list elements. Instead you can store your lists in the sitecore config in a clean way.
The first thing you need to do is to create the config file which will store your list. Here's an example of what such a list might look like.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<tutorial>
<cities type="Website.Models.CitiesModel, Website">
<items hint="list">
<item>Amsterdam</item>
<item>Rotterdam</item>
</items>
</cities>
</tutorial>
</sitecore>
</configuration>
Next you need a model which will contain your list
namespace Website.Models
{
using System.Collections.Generic;
public class CitiesModel
{
public CitiesModel()
{
this.Items = new List<string>();
}
public List<string> Items { get; set; }
}
}
Now you will be able to retrieve your list is an object from for instance a controller
namespace Website.Controllers
{
using System.Web.Mvc;
using Website.Models;
public class YourController : Controller
{
public ActionResult Index()
{
var cities = Sitecore.Configuration.Factory
.CreateObject("tutorial/cities", true) as CitiesModel;
return this.Content(string.Join(", ", cities.Items));
}
}
}
There's more interesting things you can do with the sitecore config. Read more about it here