SaveTemplateChanges bombing out

Tony Perry asked on December 10, 2021 17:42


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?



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