API Questions on Kentico API.
Version 5.x > API > How do you find all the webparts and widgets on a page??? View modes: 
User avatar
Member
Member
matthys.pienaar-gmail - 5/4/2012 3:46:58 AM
   
How do you find all the webparts and widgets on a page???
Hello guys,

I have a template inheritance question. Lets say a user opens a page, I want to be able to find all the webparts and widgets on that page even the ones that was inherited from the parents.

I have the following code that does that for just the current page. (this is code from a webpart which is on the masterpage)

        
protected void Page_Load(object sender, EventArgs e)
{

CMS.PortalEngine.PageInfo pi = CMS.CMSHelper.CMSContext.CurrentPageInfo;

CMS.PortalEngine.PageTemplateInfo pti = pi.PageTemplateInfo;

foreach (CMS.PortalEngine.WebPartZoneInstance zone in pti.WebPartZones)
{
zone.ZoneID = zone.ZoneID;

foreach (CMS.PortalEngine.WebPartInstance webpart in zone.WebParts)
{
//Some Cide
}
}
}

What this does is, it takes the current page info and gets that page's template info. Once it has that it iterates through all the zones and for each zone it will get the webparts. After all is said and done, it would have iterated through all the webparts on the page.

The problem is, the webparts are on the masterpage and whenever I open a page that uses that masterpage I am able hit a breakpoint in the posted code, but it doesn't pick up any of the webparts on the masterpage. I don't know how to traverse upwards to see whether the parent template/masterpage has webparts which is to be inherited by the child.

I have tried to write a recursive method to search through pti.ParentPageInfo but that is null. Also this.Page.Master is null.

With all this said my question becomes, how can I get a collection of all the webparts/widgets on a given page including the inherited ones???

Thanks in advance
Matthys

User avatar
Member
Member
kentico_michal - 5/6/2012 3:52:37 AM
   
RE:How do you find all the webparts and widgets on a page???
Hi,

You would need to manually loop through all parent nodes and get page template for each of them. Once you have the parent page template, you can access its web parts. The parent node can be accessed via the CMS.TreeEngine.TreeNode.Parent property.

If the current page template does not inherit from all parents, you should take into consideration the TreeNode.NodeInheritPageLevels property that indicates from which parent templates the current template inherits.

The following code demonstrates what you need to do in order to get all web parts:

CMS.TreeEngine.TreeNode parent = CMSContext.CurrentDocument.Parent;

while (parent != null)
{
// Get template from the parent document
PageInfo parentInfo = PageInfoProvider.GetPageInfo(...);

// Get web parts
GetWebParts(parentInfo.PageTemplateInfo);

// Move to the parent
parent = parent.Parent;
}


private void GetWebParts(PageTemplateInfo pti)
{
foreach (CMS.PortalEngine.WebPartZoneInstance zone in pti.WebPartZones)
{
// ...
}
}


Best regards,
Michal Legen

User avatar
Member
Member
matthys.pienaar-gmail - 5/7/2012 1:23:55 AM
   
RE:How do you find all the webparts and widgets on a page???
Working like a champ!!! Thank you so much!