Skip to content

Instantly share code, notes, and snippets.

@islaytitans

islaytitans/FollowTreelist.cs Secret

Created Nov 1, 2015
Embed
What would you like to do?
Follow target for a treelist field in Sitecore
public class FollowTreelist : Command
{
public override void Execute(CommandContext context)
{
ID id = ID.Null;
bool isId = false;
var fieldId = context.Parameters["fieldId"];
if (!string.IsNullOrEmpty(fieldId))
{
var form = System.Web.HttpContext.Current.Request.Form;
if (form != null)
{
string rawValue = form[string.Format("{0}{1}", fieldId, Constants.Selected)];
if (string.IsNullOrEmpty(rawValue))
{
rawValue = form[string.Format("{0}{1}", fieldId, Constants.AllSelected)];
}
string targetId = rawValue.Substring(rawValue.LastIndexOf("|", StringComparison.InvariantCultureIgnoreCase) + 1);
isId = ID.TryParse(targetId, out id);
// BUG fix - ID.TryParse fails unformatted ID string passed by FIELDID_all_selected
if (!isId)
{
try
{
id = new ID(targetId);
isId = true;
}
catch (Exception e)
{
isId = false;
}
}
}
}
Sitecore.Context.ClientPage.SendMessage(this, "item:load(id=" + (isId ? id.ToString() : string.Empty) + ")");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment