Skip to content

Instantly share code, notes, and snippets.

@islaytitans
Created November 1, 2015 17:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save islaytitans/15979a957216038e894f to your computer and use it in GitHub Desktop.
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