How can I get details of web parts/widgets on current page from code behind a specific web part?

Tom Troughton asked on August 25, 2015 12:08

I am developing a custom web part and from its code behind need to get the details of all other web parts on the current page. Specifically I need to access the type of each web part and the parameter data. Is this possible?

Correct Answer

Suneel Jhangiani answered on August 25, 2015 12:58

All Web Parts are derived from the CMSAbstractWebPart control and hence you could look at the Page Controls Collection to get all Controls of this type. The CMSAbstractWebPart exsposes the WebPartInfo class and hence you can get the type from that.

Here's a snippet that allows you to recuresively look at the Controls collection and get all controls of a specific type:

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

    public void FindChildControlsRecursive(Control control)
        foreach (Control childControl in control.Controls)
            if (childControl.GetType() == typeof(T))

    public List<T> FoundControls
        get { return _foundControls; }

You would call this like:

var controlFinder = new ControlFinder<CMSAbstractWebPart>);
var webPartsList = controlFinder.FindChildControlsRecursive(Page);
0 votesVote for this answer Unmark Correct answer

Recent Answers

Tom Troughton answered on August 25, 2015 14:19

Thank you. That's really helpful. Just for the benefit of future readers there are a couple of corrections needed in your code:

Replace childControl.GetType() == typeof(T) with childControl is T (because CMSAbstractWebPart is a base class so not picked up by your condition). And FindChildControlsRecursive returns void so your last line would throw an error.

But the principle of your answer is bang on. Thanks so much.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.