Programmatically add widget to page

Ryan Anthoney asked on December 3, 2019 14:13

Hi guys,

I have a Kentico 11 solution at the moment which has some documents (e.g. Blogs for example) that have fields that are populated by an import function (scheduled task).

I'm trying to build another task that populates page/widget content using the fields from the Form Tab (these are used in multiple places and there are conditions for there being a page).

int updateCounter = 0;
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
var bNodes = DocumentHelper.GetDocuments<Blog>().Where(p => p.ListingHideLinkToPage);
var pageTemplateInfo = PageTemplateInfoProvider.GetPageTemplateInfo("OneColumn");
var pageTemplateID = pageTemplateInfo.PageTemplateId;
foreach (var b in bNodes)
    if (string.IsNullOrEmpty(b.DocumentWebParts))

        // Initialize variables
        string culture = "en-GB";
        string aliasPath = b.NodeAliasPath;
        string widgetName = "HeroBanner";
        string zoneID = "cmsZoneMain";

        // Get document
        TreeNode staticNode = tree.SelectSingleNode(b.DocumentNodeID);

        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
            // Set custom widget properties
            newWidget.SetValue("ImageLarge", b.ListingImage);
            newWidget.SetValue("Heading", $"{b.PinTitle.ToUpper()} CRUISES");

            // Save the changes  
            CMSPortalManager.SaveTemplateChanges(docPageInfo, templateInstance, WidgetZoneTypeEnum.Editor, ViewModeEnum.Edit, tree);

        port.ListingHideLinkToPage = false;

        b.NodeTemplateForAllCultures = true;

The code above 'seems' to work, nothing errors. But when I then go to the desired page in the CMS the page is not populated with the widget at all.

Is there something I'm missing? Or is there an alternative way to achieve this?


Recent Answers

Brenden Kehren answered on December 4, 2019 15:49

Can you check in the database to see if the widget is added to the page in the cms_document table? Also, are you using versioning and workflow? If so, you may need to:

  • check to see if the page is checked out
  • check it in
  • check it back out under your user
  • perform the your update
  • check the page back in
  • publish the page
0 votesVote for this answer Mark as a Correct answer

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