Rendering an Item with a XSLT stored in sitecore or a database.


I work in an environment where getting something like an XSLT file onto a server requires a full QA test cycle of the whole system so I played around with rendering a sitecore item with XSLT stored inside of a sitecore item. I don’t know if I’ll ever do anything with it but here’s a class that allows you to set the path to to a sitecore item that contains a ‘Xsl Transform’ field.

    public class XslItem : XslFile
    {
        protected override void LoadXsl(System.Xml.XmlDocument xsl, string path)
        {
            Item item = Sitecore.Context.Database.GetItem(path);
            if (item != null)
            {
                if (!item.Fields.IsNullOrEmpty("Xsl Transform"))
                {
                    string xslField = item.Fields["Xsl Transform"].GetValue(true);
                    xsl.LoadXml(xslField);
                    return;
                }
            }
            base.LoadXsl(xsl, path);
        }
    }

Then to use it you’ll want something like this :


  XslItem file = new XslItem();
  file.Path = "/sitecore/content/SomeItemWithXslTransformField";
  file.DataSource = "/sitecore/content/theDataItem");
  HtmlTextWriter writer = new HtmlTextWriter(new StringWriter());
  file.RenderControl(writer);
  string htmlOutput = writer.InnerWriter.ToString();
  return new LiteralControl(htmlOutput);

And you can place your XSL inside of sitecore

public class XslItem : XslFile
{
protected override void LoadXsl(System.Xml.XmlDocument xsl, string path)
{
Item item = Sitecore.Context.Database.GetItem(path);
if (item != null)
{
if (!item.Fields.IsNullOrEmpty(“Xsl Transform”))
{
string xslField = item.Fields[“Xsl Transform”].GetValue(true);
xsl.LoadXml(xslField);
return;
}
}
base.LoadXsl(xsl, path);
}
}
Advertisements

MSDN Channel 9


I haven’t done much with ASP.Net MVC.  I sat through a week of C# 4.0  / Visual Studio 2010 training but we didn’t do much with MVC.

There was a training video on MVC 2 Basics on the main http://www.asp.net/mvc  site. It was 73 minutes and done by Scott Hanselman at the DevDays 2010 convention. Overall the material is good and there’s some light humor that doesn’t go over well at all with the crowd that keeps it amusing.

I learn a lot from web casts and screen casts and things like that so I found it informative. After I finished the video I  looked around the site and noticed it’s part of the MSDN and in there are things on LINQ and Windows Phone 7 development. I’ll probably go back and watch and some more MVC stuff.

You can see all the videos for the DevDays 2010 conference I was watching here : http://channel9.msdn.com/Tags/devdays-2010-nl

I pretty much learned vb.net, asp.net, and c#.net watching the old MSDN training videos and while it’s nice to get some actually training in lately I’m glad to see the MSDN videos collection is  improving.