Skip to content

Instantly share code, notes, and snippets.

@TimGeyssens
Created September 3, 2020 11:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TimGeyssens/5e9e156d66c3d85d0bfb24a1ae9a7504 to your computer and use it in GitHub Desktop.
Save TimGeyssens/5e9e156d66c3d85d0bfb24a1ae9a7504 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Umbraco.Community.Contentment.DataEditors;
using Umbraco.Core.PropertyEditors;
using Umbraco.Web;
namespace MyWebsite.DataSources
{
public class PropertyDataDataSource : IDataListSource
{
public string Name => "Umbraco Property Data";
public string Description => "Data source for umbraco property data coming from the website root node.";
public string Icon => "icon-globe";
public OverlaySize OverlaySize => OverlaySize.Small;
public Dictionary<string, object> DefaultValues => new Dictionary<string, object>();
public IEnumerable<ConfigurationField> Fields => new ConfigurationField[]
{
new ConfigurationField
{
Key = "propAlias",
Name = "Property alias",
Description = "The property alias",
View = "textstring"
}
};
public IEnumerable<DataListItem> GetItems(Dictionary<string, object> config)
{
var items = new List<DataListItem>();
using (UmbracoContextReference umbracoContextReference = DependencyResolver.Current.GetService<IUmbracoContextFactory>().EnsureUmbracoContext())
{
var content = umbracoContextReference.UmbracoContext.Content.GetAtRoot().First();
var propertyAlias = config["propAlias"].ToString();
var prop = content.GetProperty(propertyAlias);
if (prop == null)
throw new NullReferenceException("Property not found on node");
foreach (var value in content.GetProperty(propertyAlias).GetValue() as String[])
{
items.Add(new DataListItem
{
Name = value,
Value = value
});
}
}
return items;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment