Save Template Changes bombing out

Julio Paige asked on February 17, 2022 11:39

I'm trying to make a bunch of pages on the fly vi a scheduled task, and when making the pages, make things a little easier for our client by adding a load of widgets into zones. I found some code that apparently worked on v10, but running it in v12 it is blowing up with a null exception.

This is the code I have lifted:

private bool AddWidgetsToPage(string providerName, TreeNode document, out string message)
{
    string[] widgetsToAddCodeNames = { "ConsentModal" };
    TreeNode staticNode = _treeProvider.SelectSingleNode(document.DocumentNodeID);

    if (staticNode != null)
    {
        foreach (var widgetName in widgetsToAddCodeNames)
        {
            WidgetInfo insertWidget = WidgetInfoProvider.GetWidgetInfo(widgetName);

            PageInfo docPageInfo = CMSWebPartPropertiesPage.GetPageInfo(staticNode.NodeAliasPath, staticNode.DocumentPageTemplateID, LocalizationContext.CurrentCulture.CultureCode);

            PageTemplateInstance templateInstance = CMSPortalManager.GetTemplateInstanceForEditing(docPageInfo);

            PageTemplateInfo pti = templateInstance.ParentPageTemplate;

            WebPartInstance newWidget = PortalHelper.AddNewWidget(insertWidget.WidgetID, "main", WidgetZoneTypeEnum.Editor, false, templateInstance);

            CMSPortalManager.SaveTemplateChanges(docPageInfo, templateInstance, WidgetZoneTypeEnum.Editor, ViewModeEnum.Edit, _treeProvider);
        }
    }
    return true;
}

What I am finding at the moment is it is blowing up on the "CMSPortalManager.SaveTemplateChanges" command and i can't see why. Templateinstace is populated with a few properties, one containing the expected addition of the widgets I have added, and docPageInfo appears to be a valid document.

Has anyone had this code working on v12?

Recent Answers


Dmitry Bastron answered on February 21, 2022 06:36

Hi Julio,

What do you mean by "blowing up"? Are there any error messages in Kentico even log? Can you see the exception in the code maybe? Exception and stack trace can help you to find out what the problem is.

0 votesVote for this answer Mark as a Correct answer

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