Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

The following applies to Kentico v7.0. It probably also applies to previous versions, although this has not been tested.

Suppose you need to access the instances of widgets on a page via the api. This should, in theory, be quite easy, and in fact it is, but this is not well documented. Furthermore it can be a bit confusing because widgets are a special type of webparts and can in fact be accessed via the api using webpart-related properties.

Anyway, the following piece of code shows how to retrieve the instances of all widgets on a page:


	// Select page info of a particular document.
	CMS.DocumentEngine.PageInfo pageInfo = PageInfoProvider.GetPageInfo(
		CMSContext.CurrentSiteName, 
		CMSContext.CurrentDocument.NodeAliasPath, 
		CMSContext.CurrentDocumentCulture.CultureName, 
		null, 
		true
		);

	//Get information about the instance of the template which is used by the document.
	CMS.PortalEngine.PageTemplateInstance templateInstanceInfo = pageInfo.TemplateInstance;

	// Loop through all zones.
	foreach (CMS.PortalEngine.WebPartZoneInstance zone in templateInstanceInfo.WebPartZones)
	{
		// Only continu if the zone is an editor zone.
		if (zone.WidgetZoneType == CMS.PortalEngine.WidgetZoneTypeEnum.Editor)
		{
			// Loop through all "webparts" which are in fact widgets.
			foreach (CMS.PortalEngine.WebPartInstance widget in zone.WebParts)
			{
				string widgetInfo = widget.ControlID;
			}
		}
	}


This piece of code retrieves the widgets on the current document. But if you need to retrieve the widgets on another document, just replace the NodeAliasPath in the second parameter of the method PageInfoProvider.GetPageInfo by the NodeAliasPath of the document you are interested in.

With specials thanks to Jim and Kentico Support for their help.

 © Evident Interactive BV