+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / 3rd party app integration - attempting to render a template manually

3rd party app integration - attempting to render a template manually

2 posts, 0 answered
  1. theksmith
    theksmith avatar
    5 posts
    Registered:
    19 Nov 2010
    16 Dec 2010
    Link to this post
    I need to integrate a 3rd party ecommerce system with Sitefinity in terms of controlling it's appearance somewhat  by using Sitefinity templates. I've already abandoned the idea of getting this software and Sitefinity to completely integrate into the same application pool. However, I do currently have Sitefinity in the root of my domain and controlling the majority of the site. I then have the ecommerce app mapped to a sub-directory as it's own application within IIS and have this working.

    Currently, I need to render the full HTML that a Sitefinity template generates when previewed so that I can parse out it's sections and save them in XML for use by the ecommerce app's skinning engine. This will allow me to control most of our common header/footer/menu content with the Sitefinity template.

    I have 2 issues:

    - Even though I set the Page.AppRelativeVirtualPath before executing it, the controls on the page (navigation controls) do not seem to honor this setting. I want to force the execution of the page to "pretend" to be in the same folder as my ecommerce app, so that the relative URLs generated are correct.

    - and, is there a simpler way to achieve my goal of getting and parsing the rendered content of a template? am I going about it all wrong?!? I feel like I'm recreating the whole Sitefinity page renderer here and there should be a method already in the API that does this.

    here is my test code thus far:

    001.using System;
    002.using System.IO;
    003.using System.Web;
    004.using System.Web.UI;
    005.using System.Web.UI.WebControls;
    006.using System.Web.UI.HtmlControls;
    007.using System.Web.Services;
    008. 
    009.using Telerik.Cms;
    010.using Telerik.Cms.Web;
    011.using Telerik.Cms.Data;
    012.using Telerik;
    013. 
    014.public partial class RenderTemplate
    015.{
    016.    string TemplateName = string.Empty;
    017. 
    018.    public string[] GetContentSections(string templateName)
    019.    {
    020.        this.TemplateName = templateName;
    021. 
    022.        CmsManager cms = new CmsManager();
    023.        CmsTemplateContent tplate = (CmsTemplateContent)cms.GetTemplate(templateName);
    024.        if (tplate == null) throw new Exception("No template with that name could be found.");
    025. 
    026.        StringWriter strWriter = new StringWriter();
    027.        using (HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter))
    028.        {
    029.            using (Page pg = new Page())
    030.            {
    031.                pg.PreRender += new EventHandler(pg_PreRender);
    032. 
    033.                pg.AppRelativeVirtualPath = "~/PretendIAm/InHere/Default.aspx";
    034.                pg.MasterPageFile = "~/App_Master/" + tplate.MasterPage;
    035. 
    036.                foreach (CmsWebControl ctl in tplate.Controls)
    037.                {
    038.                    Control ctlContainer = pg.Master.FindControl(ctl.ContainerID);
    039. 
    040.                    if (ctlContainer != null && ctlContainer is ContentPlaceHolder)
    041.                    {
    042.                        ctlContainer.Controls.Add(new LiteralControl("{BEGIN:" + ctl.ContainerID + ":" + "}"));
    043. 
    044.                                                Control ctlLoaded = null;
    045.                        if (ctl.IsAscx)
    046.                        {
    047.                            ctlLoaded = (Control)Activator.CreateInstance(ctl.ControlType);
    048.                        }
    049.                        else
    050.                        {
    051.                            ctlLoaded = ctl.LoadControl();
    052.                        }
    053.                        ctlLoaded.ID = ctl.ID.ToString();
    054.                        ctlContainer.Controls.Add(ctlLoaded);
    055. 
    056.                        ctlContainer.Controls.Add(new LiteralControl("{END}"));
    057.                    }
    058.                }
    059. 
    060.                pg.Server.Execute(pg, htmlWriter, false);
    061.            }
    062.        }
    063. 
    064.        string content = strWriter.ToString();
    065.        content = content.Substring(content.IndexOf("{BEGIN:"), content.LastIndexOf("{END}") + 5 - content.IndexOf("{BEGIN:"));
    066. 
    067.        string[] contentSections = content.Split(new string[] { "{BEGIN:" }, StringSplitOptions.RemoveEmptyEntries);
    068.        for (int i = 0; i < contentSections.Length; i++)
    069.        {
    070.            contentSections[i] = "{" + contentSections[i].Substring(0, contentSections[i].LastIndexOf("{END}"));
    071.        }
    072. 
    073.        return contentSections;
    074.    }
    075. 
    076. 
    077.    private void pg_PreRender(object sender, EventArgs e)
    078.    {
    079.        Page pg = sender as Page;
    080. 
    081.        CmsManager cms = new CmsManager();
    082.        CmsTemplateContent tplate = (CmsTemplateContent)cms.GetTemplate(this.TemplateName);
    083. 
    084.        foreach (CmsWebControl ctl in tplate.Controls)
    085.        {
    086.            Control ctlContainer = pg.Master.FindControl(ctl.ContainerID);
    087. 
    088.            if (ctlContainer != null && ctlContainer is ContentPlaceHolder)
    089.            {
    090.                Control ctlLoaded = ctlContainer.FindControl(ctl.ID.ToString());
    091. 
    092.                Type ctlType = ctlLoaded.GetType();
    093.                foreach (CmsControlProperty ctlProp in ctl.Properties)
    094.                {
    095.                    System.Reflection.PropertyInfo ctlPropInfo = ctlType.GetProperty(ctlProp.Name);
    096.                    if (ctlPropInfo.CanWrite)
    097.                    {
    098.                        try
    099.                        {
    100.                            ctlPropInfo.SetValue(ctlLoaded, Convert.ChangeType(ctlProp.Value, ctlPropInfo.PropertyType), null);
    101.                        }
    102.                        catch (Exception ex)
    103.                        {
    104.                            ctlContainer.Controls.AddAt(1, new LiteralControl("{ERROR:" + ctlProp.Name + "=" + ctlProp.Value + "|" + ex.ToString() + "}"));
    105.                        }
    106.                    }
    107.                }
    108.            }
    109.        }
    110.    }
    111.}

    thanks for any assistance!
  2. theksmith
    theksmith avatar
    5 posts
    Registered:
    19 Nov 2010
    20 Dec 2010
    Link to this post
    ...or can anyone provide alternative solutions to the primary goal of rendering and capturing an entire template's HTML within server-side code?
2 posts, 0 answered