Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@islaytitans
Created November 3, 2015 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save islaytitans/f05c7fe166bf64e8a3bf to your computer and use it in GitHub Desktop.
Save islaytitans/f05c7fe166bf64e8a3bf to your computer and use it in GitHub Desktop.
Get the Item selected in a Grouped Droplist Sitecore Field
public class FollowGroupedDroplist : Command
{
public override void Execute(CommandContext context)
{
ID id = ID.Null;
Item item = context.Items[0];
var targetName = WebUtil.GetFormValue(context.Parameters["fieldId"]);
if (item != null && !string.IsNullOrEmpty(targetName))
{
foreach (Field field in item.Fields.Where(Utility.IsGroupedDroplistField))
{
if (field.HasValue && field.Value.Equals(targetName) && !string.IsNullOrEmpty(field.Source))
{
var grandChildren = new List<Item>();
Item sourceItem = item.Database.GetItem(field.Source);
if (sourceItem != null)
{
grandChildren = (from c in sourceItem.Children
select c.GetChildren()).SelectMany(g => g).ToList();
}
else
{
List<Item> children = Utility.GetItemsFromQuery(field.Source, item).ToList();
grandChildren = (from c in children
select c.GetChildren()).SelectMany(g => g).ToList();
}
Item targetItem = grandChildren.FirstOrDefault(c => c.Name == targetName);
if (targetItem != null)
{
id = targetItem.ID;
break;
}
}
}
}
Sitecore.Context.ClientPage.SendMessage(this, "item:load(id=" + (id != ID.Null ? id.ToString() : string.Empty) + ")");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment