How to get correct order of Page controls in c#
I am getting the controls within a placeholder by doing the following:
public static List<ControlData> GetContentItems(string pageTitle, string placeHolder)
var pageManager = PageManager.GetManager();
var page = pageManager.GetPageNodes().Where(p => p.Title == pageTitle).SingleOrDefault();
var controls = page.Page.Controls.Where(c => c.PlaceHolder == placeHolder).ToList<ControlData>();
return controls;
All good, I can then get the content of each content block by doing the following:
List<ControlData> controls = GetContentItems("Home", "leftHandContentBottom");
foreach (var item in controls)
String contentHTML = item.PropertiesLang[0].Value;
Although, if the user changes the content blocks around within the same placeholder, the content will still be returned in the same order. Is there a property that I can look at in the control that gives me the order? How does sitefinity know that one control is before the other?
Thanks,
Ricardo
Ricardo,
Check out the SiblingId. That should get you the information you're looking for. (If it's the first control, I'm pretty sure it'll be an empty GUID. You'll wanna check that though...)
From the docs:
SiblingId: The preceding control Id. You must set this property for correct order of the controls in the page.
Hi,
Any chance anyone has sorted this before? I am trying to use a dictionary, but finding it quite difficult to get the correct order. Any ideas?
Struggling to make it work.
public static List<ControlData> orderContent(List<ControlData> controls)
Dictionary<Guid, Guid> d = new Dictionary<Guid, Guid>()
foreach(var el in controls)
d[el.SiblingId] = el.Id; //put data to dict by PreecedingID
List<ControlData> result = new List<ControlData>();
var prec = new Guid("00000000-0000-0000-0000-000000000000"); //get first ID
//int prec = 0;
for(int i = 0; i < controls.Count; ++i)
var actEl = d[prec]; //get next element
prec = actEl; //change prec id
result.Add(actEl); //put element into result list
Hi,
Please review the following forum thread where you will find useful information:
http://www.sitefinity.com/developer-network/forums/developing-with-sitefinity-/how-to-obtain-control-id-guid-using-api
Thanks Stefani, that is very useful indeed!