Hi,
here is an example on how to add "EDITOR" widget to the page via Kentico API:
// Create new instance of the Tree provider
TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);
// Initialize variables
string culture = "en-US";
string aliasPath = "/Articles/Static";
string widgetName = "DesignElement";
string zoneID = "zoneA";
// Get document
TreeNode staticNode = tree.SelectSingleNode(CMSContext.CurrentSiteName, aliasPath, culture);
if (staticNode != null)
{
// Get widget
WidgetInfo insertWidget = WidgetInfoProvider.GetWidgetInfo(widgetName);
// Get PageInfo of the document
PageInfo docPageInfo = CMSWebPartPropertiesPage.GetPageInfo(staticNode.NodeAliasPath, staticNode.DocumentPageTemplateID, culture);
// Get template instance
PageTemplateInstance templateInstance = CMSPortalManager.GetTemplateInstanceForEditing(docPageInfo);
// Get page template info
PageTemplateInfo pti = templateInstance.ParentPageTemplate;
// Add widget to the page into "Editor Widget Zone" with ID = "ZoneA"
WebPartInstance newWidget = PortalHelper.AddNewWidget(insertWidget.WidgetID, zoneID, WidgetZoneTypeEnum.Editor, false, templateInstance);
// Set custom widget properties
newWidget.SetValue("Title", "Sample title");
// Save the changes
CMSPortalManager.SaveTemplateChanges(docPageInfo, templateInstance, WidgetZoneTypeEnum.Editor, ViewModeEnum.Edit, tree);
}
Here is an example on how to add "USER" widget to the page via Kentico API:
// Create new instance of the Tree provider
TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);
// Initialize variables
string culture = "en-US";
string aliasPath = "/Articles/Static";
string widgetName = "DesignElement";
string zoneID = "zoneA";
// Get document
TreeNode staticNode = tree.SelectSingleNode(CMSContext.CurrentSiteName, aliasPath, culture);
if (staticNode != null)
{
// Get widget
WidgetInfo insertWidget = WidgetInfoProvider.GetWidgetInfo(widgetName);
// Get PageInfo of the document
PageInfo docPageInfo = CMSWebPartPropertiesPage.GetPageInfo(staticNode.NodeAliasPath, staticNode.DocumentPageTemplateID, culture);
// Get template instance
PageTemplateInstance templateInstance = CMSPortalManager.GetTemplateInstanceForEditing(docPageInfo);
// Get page template info
PageTemplateInfo pti = templateInstance.ParentPageTemplate;
// Add widget to the page into "Editor Widget Zone" with ID = "ZoneA"
WebPartInstance newWidget = PortalHelper.AddNewWidget(insertWidget.WidgetID, zoneID, WidgetZoneTypeEnum.User, false, templateInstance);
// Set custom widget properties
newWidget.SetValue("Title", "Sample title");
// Save the changes
CMSPortalManager.SaveTemplateChanges(docPageInfo, templateInstance, WidgetZoneTypeEnum.User, ViewModeEnum.LiveSite, tree);
}
Just for an information, widgets can be stored on DB level in different tables depending how they are used.
Here is a brief summary:
Dashboard page widgets (CMSDesk, CMS Site Manager)
-- CMS_Personalization (PersonalizationDashboardName, PersonalizationWebParts)
User personalization widgets (used on LIVE site)
-- CMS_Personalization (PersonalizationDocumentID, PersonalizationWebParts)
Page editor widgets (document "Page" tab)
-- CMS_Document (DocumentWebParts)
Page editor widgets (page template "Design" tab)
-- CMS_PageTemplate (PageTemplateWebParts)
Inline widgets (used in "Editable text" web part on "Page" tab)
-- CMS_Document (DocumentContent)
Document Group widgets
-- CMS_Document (DocumentGroupWebParts)
Hope that helps.
Miro [Kentico Consulting Services]