Custom PageTemplateInfoProvider

Mark Nally asked on March 20, 2014 05:44

We have created a custom PageTemplateInfoProvider with the purpose of setting the PageTemplateInfo.Webparts property to some bespoke xml. We have created a class inherited from PageTemplateInfoProvider and over-ridden the GetPageTemplateInfoInternal function.

This all works correctly most of the time with this over-ridden function being called each time a page is displayed. However intermittently this method doesn't get called when a page is loaded. This leads to the wrong widgets being displayed on a page. This can happen for a few pages and then it starts to work again.

There doesn't seem to be any pattern for when it stops working and there is nothing in the event log.

Any Ideas?

Recent Answers


Juraj Ondrus answered on March 25, 2014 04:51

Hi,

Would it be possible to share your code and also how and when are you registering the custom provider so we can get better picture of what exactly are you doing?

Thank you!

Best regards,
Juraj Ondrus

0 votesVote for this answer Mark as a Correct answer

Mark Nally answered on March 25, 2014 11:17 (last edited on March 25, 2014 11:17)

Below is the full code for the over ridden file. As I aid in my initial post the GetPageTemplateInfoInternal is usually being called correctly each time a page loads. Then for some unexplained reason it stops being called for a few pages.

using CMS.PortalEngine; using System; using System.Linq; using System.Web; using System.Data; using CMS.GlobalHelper; using CMS.SiteProvider; using CMS.SettingsProvider; using True.Kentico.Helpers; using System.Collections.Generic; using CMS.CMSHelper; using System.Text.RegularExpressions; using True.Kentico.Helpers.Content;

/// public class CustomPageTemplateInfoProvider : PageTemplateInfoProvider { private const string HOME_ALIASPATH = "/home";

/// <summary>
/// Override to add delivery instructions to order so they can be accessed through the CMS Desk
/// </summary>
/// <param name="cartObj"></param>
/// <returns></returns>
protected override void SetPageTemplateInfoInternal(PageTemplateInfo template)
{
    bool flag;
    if (template != null)
    {
        int categoryID = 0;
        bool pageTemplateId = template.PageTemplateId > 0;
        if (template.ItemChanged("PageTemplateLayout"))
        {
            flag = true;
        }
        else
        {
            flag = template.ItemChanged("PageTemplateCSS");
        }
        bool flag1 = flag;
        if (flag1 || string.IsNullOrEmpty(template.PageTemplateVersionGUID))
        {
            Guid guid = Guid.NewGuid();
            template.PageTemplateVersionGUID = guid.ToString();
        }
        if (template.IsReusable)
        {
            template.PageTemplateSiteID = 0;
        }
        if (!template.IsReusable)
        {
            PageTemplateCategoryInfo pageTemplateCategoryInfo = PageTemplateCategoryInfoProvider.GetPageTemplateCategoryInfo(template.CategoryID);
            if (pageTemplateCategoryInfo == null)
            {
                pageTemplateCategoryInfo = PageTemplateCategoryInfoProvider.GetAdHocCategory();
                if (pageTemplateCategoryInfo != null)
                {
                    template.CategoryID = pageTemplateCategoryInfo.CategoryId;
                }
                else
                {
                    throw new Exception("[PageTemplateInfoProvider.SetPageTemplateInfoInternal] Ad-hoc category is no available.");
                }
            }
        }

        template.WebParts = template.TemplateInstance.GetZonesXML();
        if (pageTemplateId)
        {
            PageTemplateInfo infoByCodeName = this.GetInfoByCodeName(template.CodeName, template.PageTemplateSiteID, false);
            if (infoByCodeName != null)
            {
                if (infoByCodeName.PageTemplateId == template.PageTemplateId)
                {
                    categoryID = infoByCodeName.CategoryID;
                }
                else
                {
                    throw new Exception(string.Concat("Page template '", template.CodeName, "' already exists."));
                }
            }
        }
        this.SetInfo(template);

        UpdateNodeTemplateWebParts(CMSContext.CurrentDocument.NodeID, template.PageTemplateId, template.WebParts);

        if (pageTemplateId && template.Generalized.TouchCacheDependencies)
        {
            int num = template.PageTemplateId;
            CacheHelper.TouchKey(string.Concat("template|", num.ToString()));
        }
        PageTemplateCategoryInfoProvider.UpdateCategoryTemplateChildCount(categoryID, template.CategoryID);
        if (HttpContext.Current != null)
        {
            string str = "CMSVirtualWebParts";
            CacheHelper.Remove(str);
        }
        if (PageTemplateInfoProvider.CreateSearchTasks && SearchIndexInfoProvider.SearchEnabled)
        {
            LogSearchTasks(template);
        }
    }
}

protected override PageTemplateInfo GetPageTemplateInfoInternal(int templateId)
{
    PageTemplateInfo templateInfo = base.GetPageTemplateInfoInternal(templateId);

    int nodeId = 0;

    Guid currentDocumentGuid;
    string pageUrl = HttpContext.Current.Request.RawUrl.Equals("/") ? HOME_ALIASPATH : HttpContext.Current.Request.RawUrl,
        documentGuidString = Regex.Match(pageUrl,
                                     @"\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b",
                                     RegexOptions.IgnoreCase).Value;

    if (Guid.TryParse(documentGuidString, out currentDocumentGuid))
    {
        // Design mode
        var currentNode = NodeHelper.GetSingleNodeFromGuid(currentDocumentGuid);
        nodeId = currentNode != null ? currentNode.NodeID : nodeId;
    }
    else
    {
        // Standard mode
        var currentNode = NodeHelper.GetSingleNode(pageUrl);
        nodeId = currentNode != null ? currentNode.NodeID : nodeId;
    }

    if (nodeId == 0)
    {
        return templateInfo;
    }

    string nodeXml = GetNodeTemplateWebParts(nodeId, templateId);

    if (nodeXml != string.Empty)
    {
        templateInfo.WebParts = nodeXml;
    }

    return templateInfo;
}

private static void LogSearchTasks(PageTemplateInfo template)
{
    string str = string.Format("DocumentPageTemplateID = {0} OR NodeTemplateID = {0} OR NodeWireframeTemplateID = {0}", template.PageTemplateId);
    ModuleCommands.TreeEngineLogSearchTasks(str);
}

private static void UpdateNodeTemplateWebParts(int nodeId, int pageTemplateId, string webPartXml)
{
    string where = String.Format("PageTemplateID = {0} and NodeID = {1}", pageTemplateId, nodeId);

    DataRow row = CustomTable.GetCustomTableItemData("True.NodeWebParts", where);

    var rowValues = new Dictionary<string, object>();

    rowValues.Add("NodeID", nodeId);
    rowValues.Add("PageTemplateID", pageTemplateId);
    rowValues.Add("NodeWebParts", webPartXml);

    if (row == null)
    {
        // Add data to custom table
        CustomTable.CreateCustomTableItem("True.NodeWebParts", rowValues);
    }
    else
    {
        // Update custom table
        CustomTable.UpdateCustomTableItem("True.NodeWebParts", (int)row["ItemID"], rowValues);
    }
}

private static string GetNodeTemplateWebParts(int nodeId, int pageTemplateId)
{
    string where = String.Format("PageTemplateID = {0} and NodeID = {1}", pageTemplateId, nodeId);

    DataRow row = CustomTable.GetCustomTableItemData("True.NodeWebParts", where);

    if (row == null)
    {
        return string.Empty;
    }

    return row["NodeWebParts"].ToString();
}

}

0 votesVote for this answer Mark as a Correct answer

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