How to get correct order of Page controls in c#

Posted by Community Admin on 04-Aug-2018 14:39

How to get correct order of Page controls in c#

All Replies

Posted by Community Admin on 19-Jun-2013 00:00

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

Posted by Community Admin on 19-Jun-2013 00:00

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.

Posted by Community Admin on 20-Jun-2013 00:00

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
                
        



Posted by Community Admin on 24-Jun-2013 00:00

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


Note that the IDs of the placeholders are generated dynamically but they remain static once the control has been added to the page layout. In other words you can inspect your newly added (or already existing) layout control and check its placeholderid property, which can be used then in the code for adding a control to page.

For you r convenience I've recorded a short demonstrative video, I hope you find it useful

Regards,
Stefani Tacheva
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 25-Jun-2013 00:00

Thanks Stefani, that is very useful indeed!

This thread is closed