+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to get correct order of Page controls in c#

How to get correct order of Page controls in c#

5 posts, 0 answered
  1. Ricardo
    Ricardo avatar
    29 posts
    Registered:
    06 Nov 2012
    19 Jun 2013
    Link to this post
    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
  2. Tim
    Tim avatar
    258 posts
    Registered:
    22 Jun 2011
    19 Jun 2013 in reply to Ricardo
    Link to this post
    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.
  3. Ricardo
    Ricardo avatar
    29 posts
    Registered:
    06 Nov 2012
    20 Jun 2013 in reply to Tim
    Link to this post
    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
                    }
            }



  4. Stefani Tacheva
    Stefani Tacheva avatar
    718 posts
    Registered:
    06 Dec 2016
    24 Jun 2013
    Link to this post
    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
  5. Ricardo
    Ricardo avatar
    29 posts
    Registered:
    06 Nov 2012
    25 Jun 2013
    Link to this post
    Thanks Stefani, that is very useful indeed!
5 posts, 0 answered