Monday, October 16, 2017

How to get all webparts on a specific page in Kentico


Use following class to find all the webparts on a specific page.

public class VlineControlFinder<T> where T : Control
{
    private List<T> _foundControls = new List<T>();

    public List<T> FindChildControlsRecursive(Control control)
    {
        foreach (Control childControl in control.Controls)
        {
            if (childControl is T)//(childControl.GetType() == typeof(T))
                _foundControls.Add((T)childControl);
            else
                FindChildControlsRecursive(childControl);
        }

        return _foundControls;
    }

    public List<T> FoundControls
    {
        get { return _foundControls; }
    }
}
How can we use the class and the method?

var controlFinder = new VlineControlFinder<CMSAbstractWebPart>();
var overrideMacrosWebPart = controlFinder.FindChildControlsRecursive(page).Where(o => o.TitleInfo == "OverrideMacros").SingleOrDefault();

No comments:
Write comments
Recommended Posts × +