Skip to content

Instantly share code, notes, and snippets.

@blacktambourine
Last active September 27, 2017 07:57
Show Gist options
  • Select an option

  • Save blacktambourine/dfaaffa9bf7b130408a690b61fa45b33 to your computer and use it in GitHub Desktop.

Select an option

Save blacktambourine/dfaaffa9bf7b130408a690b61fa45b33 to your computer and use it in GitHub Desktop.
Custom Sitecore Field to get a dropdown list of Roles
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.HtmlControls;
using Sitecore.Security.Accounts;
using Control = Sitecore.Web.UI.HtmlControls.Control;
namespace BlackTambourine.Core.SC.FieldType
{
public class SecurityRoleList : Control
{
public IEnumerable<string> Roles()
{
return RolesInRolesManager.GetAllRoles(false)
.Where(x=> x.Name.StartsWith(@"sitecore\My Role Prefix "))
.Select(y=> y.Name);
}
public string InputId
{
get { return GetID("input"); }
}
protected override void OnLoad(EventArgs e)
{
if (!Sitecore.Context.ClientPage.IsEvent)
{
var wrapper = new HtmlGenericControl { TagName = "div" };
wrapper.Attributes["class"] = "wrapper";
var input = new HtmlGenericControl
{
TagName = "input",
ID = InputId
};
var listName = "dataList_" + InputId;
input.Attributes["value"] = Value;
input.Attributes["name"] = InputId;
input.Attributes["list"] = listName;
input.Attributes["style"] = "width:300px;";
var dataList = new HtmlGenericControl
{
TagName = "datalist",
ID = listName
};
foreach (var i in Roles())
{
var option = new HtmlGenericControl
{
TagName = "option"
};
option.Attributes["value"] = i;
option.InnerHtml = i;
dataList.Controls.Add(option);
}
wrapper.Controls.Add(input);
wrapper.Controls.Add(dataList);
Controls.Add(wrapper);
}
else
{
var value = Context.Request.Form[InputId] ?? "";
Sitecore.Context.ClientPage.Modified = Value != value;
if (value != null && value != Value)
{
Value = value;
}
}
base.OnLoad(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment