-
-
Save islaytitans/f05c7fe166bf64e8a3bf to your computer and use it in GitHub Desktop.
Get the Item selected in a Grouped Droplist Sitecore Field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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