Friday, July 30, 2010

Sitecore Fetch Squad

Automated crawler fetching websites and blogs from Sitecore content

Archive for the ‘xslt’ Category

Manually using Sitecore XSLT Renderings

Posted by admin On December - 29 - 2008

If you want a simple way to get the output from an XSLT Rendering using Sitecore in code use the following:

    Sitecore.Web.UI.WebControls.XslFile file = new Sitecore.Web.UI.WebControls.XslFile();
    file.Path = "/xsl/MyRendering.xslt";
    string output = file.RenderAsText();

This will give you the output of the rendering as a single string that you can then use for any purpose.One thing that would be really cool would be to create XSLT on the fly and feed it into this XslFile control. If I work this out I will let you know.

Mike’s Blog

XslExtensions be gone – part 2

Posted by admin On December - 29 - 2008

As described in part one of my post, we at Pentia have for some time had a bad feeling about the XSLT extensions concept introduced by Microsoft in the XsltTransform class and implemented further in Sitecore. We feel that XSLT’s belong in the UI tier (in a traditional n-tier architecture) and XSLT extensions has a tendency to introduce a mix of UI and business layer functionality and limited by the type restrictions in XSLT.

All developers (in their right minds) knows that asp.net codebehind files should contain UI layer functionality, not business logic, and that one c# codebehind file is bound to one asp.net file. What we want to achieve, is to have the same link between codebehind files in ASP.NET and XSLTs, therefore in our internal projects at Pentia, we have introduced the concept of XSLT codehind files.

So how does that work, you might ask?

Basically, we’ve developed a module which is included in all our projects (through our build environment), which automatically links XSLT extension classes to XSLT files through a common namespace. The module consists of an overridden Sitecore.Web.UI.WebControls.XslFile class, which overrides the AddExtensionObjects function:

  public class XslCodebehindFile : Sitecore.Web.UI.WebControls.XslFile
  {
    protected override void AddExtensionObjects(XsltArgumentList list, Item item)
    {
      //…
    }
  }

The AddExtensionObjects function dynamically determines which .NET class is the codebehind file for the current XSLT and adds it to the XsltArgumentList with a specific namespace (in our case http://www.pentia.net/codebehind). The class to add is determined via a custom .NET Attribute class which decorates the codebehind class (see the example below).

Furthermore the module implements an overridden Sitecore.Web.UI.XslControlRenderingType class:

  public class XslCodebehindControlRenderingType : XslControlRenderingType
  {
    public override Control GetControl(NameValueCollection parameters, bool assert)
    {
      // Returns the XslCodebehindFile class instead of the XslFile class
      // …
    }
  }

This class is used by the Sitecore renderings engine to return the .NET class which implements a Sitecore rendering type. The class is hooked into Sitecore through the web.config (done automatically though our build environment):

<configuration>
  <!– … –>
  <sitecore>
    <!– … –>
    <renderingControls>
      <!– … –>
      <control template=”xsl rendering”
        type=”PT.XslCodebehind.XslCodebehindControlRenderingType, PT.XslCodebehind”
        propertyMap=”Path=path” />
        <!– … –>

So how does the developer actually use it?

The developer starts by writing his XSLT file, e.g. /xsl/newslist.xslt. Most often it turns out that XSLT files does not even require codebehind or that the required functionality is implemented in the Sitecore XSLT extensions, but in the rare cases where advanced functionality is required (e.g. paging in the list of news) the developer creates a c# class file named newslist.xslt.cs (to keep the naming conventions defined by Microsoft). The class in the file is decorated with the XslCodebehind attribute which points to the fully qualified XSLT file:

  [XslCodebehind("/xsl/NewsArchive.xslt")]
  public class NewsArchive
  {
    //All functions in this class can be called in NewsArchive.xslt
    public Int32 GetCurrentPageIndex(XPathNodeIterator archive)
    {
      //…
    }
    public Int32 GetPreviousPageIndex(XPathNodeIterator archive)
    {
      //…
    }
    public Int32 GetNextPageIndex(XPathNodeIterator archive)
    {
      //…
    }
    }

This class is then automatically linked into the XSLT file at runtime and can be called though the http://www.pentia.net/codebehind namespace:

  <?xml version=”1.0″ encoding=”UTF-8″?>
  <xsl:stylesheet version=”1.0″
    xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
    xmlns:sc=”http://www.sitecore.net/sc”
    xmlns:codebehind=”http://www.pentia.net/codebehind”
    exclude-result-prefixes=”sc codebehind”>
    <!– … –>
    <xsl:template match=”*” mode=”main”>
      <!– … –>
      <a href=”?p={codebehind:GetNextPageIndex(.)}”>
        <!– … –>
      </a>
      <!– … –>
    </xsl:template>
  </xsl:stylesheet>

Easy-peasy – no meddling about in web.config and a much tighter coupling between XSLT files and their .NET functions.

The XSLT codebehind functionality took me about half a day to code and include into all our projects. It has made it practically easier for developers to link functionality to XSLT files and conceptually easier to understand the placement of the XSLT functionality in the UI layer.

      

Molten Core

XslExtensions be gone! - part 1

Posted by admin On December - 29 - 2008

One of the features of Sitecore which challenges correct development practise is XslExtensions (or XslHelpers as we call them). Good examples of XslExtensions are the Sitecore.Xml.Xsl.XslHelper and Sitecore.Xml.Xsl.SqlHelper classes (read more here).

The possibility of hooking in your own .NET functions into your XSLT code is a vital part of Sitecore, but the concept of XslExtensions brings a few problems to the party:

  • XslExtensions are actually a return to the world of functional programming and removes the concept of which context your code is running.
  • XslExtensions are loosly bound to where it is used. In practise it is difficult to know from where your code is being called – if it is called at all.
  • Hooking in XslExtensions requires changes to your web.config file. This is actually not a big problem, but it would be nice to be without.
  • XslExtension methods require specific types as input and output. Therefore if you introduce nice functionality in a XslExtension method, it is bound to the XSLT compliant types.
  • XslExtensions lies on the border between the UI and the business logic tier of your application. Therefore XslExtensions can often be cluttered with methods which replicate and port functionality in your business logic to XSLT compliancy, or actually introduces new business logic functionality.

A good example of this last two points are Sitecore.Xml.Xsl.XslHelper.HasBaseTemplate() method, which is actually the only place in the Sitecore API where you can query whether an Item derives from a given template. This means that if you need this code in ASP.NET, you need to replicate the functionality in your own classes or - even worse – call Sitecore.Xml.Xsl.XslHelper.HasBaseTemplate() from your ASP.NET codebehind.

So, what can we do to try to enforce good development practise while still maintaining the essential functionality of exposing .NET methods to XSLT’s? I’ll get back to that in part two of my post.

      

Molten Core

Power of Sitecore: Separation of UI and Data Layers

Posted by admin On December - 29 - 2008
There is a simple approach on how you can accomplish user driven site design by letting them select different CSS files or skins. What I did is put an XSLT rendering into the head section of the layout that will be reused across the pages: <head> <title>Some title</title> <sc:XslFile ID="styleSelector" Path="/xsl/styleselector.xslt" runat="server" /> </head> The source of the xslt rendering

Consulting and Supporting Sitecore Developer Community

WebEdit content markers

Posted by admin On December - 29 - 2008
This is pretty simple, but still worth mentioning. In order to add the green content markers to your page in XSLT, use this approach. As for .NET, you have a number of options: This will create the content marker that points to the context item: <sc:ContentDot id="contentDot" runat="server" /> This will create the content marker that points to the “news” item: <sc:ContentDot id="contentDot"

Consulting and Supporting Sitecore Developer Community

Showing Sitecore how to improve…

Posted by admin On December - 29 - 2008
A while back, Alex De Groot invited to an open debate on areas where Sitecore could improve.

This post is my response.

Readers of this blog will have noticed, I often take a somewhat critical stance to Sitecore. What may not be so obvious at a first glance however is, my beef is not so much with the product itself, but rather Sitecore as the company behind the product. The issues that come from working with the product ultimately comes down to the practices that are in place in the company that creates it. Sure, a particular bug might be annoying but it’s just the smoke - not the fire itself.

So I won’t be requesting features, in short. In fact, if anything, I will request “no new features” at all; but instead point out that good solid project management practices, quality assurance, usability studies and whatnot make up a very important (if not the most important) part of any ongoing software development endeveaour. Even if I couldn’t spell that right… ;-)

But then - it is also obviously very easy for me to just sit here and say “hey guys, manage your stuff - how hard can it be?!”. I’ve been doing software development for many years now, and if anything I’ve come to realise that perfection is more a direction than a goal in itself. I’ve seen improvements in the way Sitecore has been working, especially over the last year or so. Documentation for instance, has always been a weakness when working with Sitecore. So I was very pleased to find, that documentation is taking on a whole new level with The Sitecore 6 Documentation Package - while far from covering everything there is to say about working with Sitecore, it’s certainly a step in the right direction. Through Sitecore Support I’ve had the priviledge of looking at some of the other documents still being drafted, and I’m glad to see that this new trend is taking on.

Documentation would have been top of my list still, as the first thing to improve. It doesn’t matter that you have a good product, if noone knows about it.

With that said however, for crying out loud guys will you fix and consolidate your online information already? Per Bering from Codehouse pointed this out as well as a comment to Alex’ post. Your site search is absolute bollox and there is not a single other thing to say about it :P

“Did you mean..
xsl rendering
xsls rendering
xslext rendering
exslt rendering
xslt rederings
xslt renderingid
xslt renderingitem
xslt renderendtag

:P

Right now, the only way I am staying “on top” (if that’s what I am, I don’t know) of current Sitecore technical information is, by subscribing to roughly 20 Sitecore related blogs, chatting with people who work for Sitecore, chatting with other consultants who work for Sitecore partners and then as the last resort - the ultimate Sitecore tool; Reflector. I’ve even seen Sitecore support personnel themselves point developers in the direction of Reflector as a source for information. And while it’s a great tool, I can’t really believe this is how it is meant to be. Notice btw, how I deliberately didn’t include SDN5 as a common source of information for me. While I go there, it’s very rare that I am able to successfully find what I’m looking for there - so the fallback option is Sitecore Support. Fortunately I find Sitecore Support very good, at least :-)

Enough about documentation however.

The second thing I would ask for, is for Sitecore to start eating their own dogfood. If it’s not good enough for you to run your own site on, it’s certainly not good enough for the very large developer base out here who has very limited access to documentation on the product. Update notes and changelogs don’t really tell a useful story as I think most who work with the product will know. I’ve never seen a changelog saying that the “admin” user no longer comes with Danish locale as default in a blank Sitecore 6 - and that this breaks the way some fields work in Sitecore - nor have I seen a note that it was fixed. It might exist somewhere; see “searching” above ;-) Or what about Page Editor acting up and throwing exceptions if you happen to run Sitecore in a non “en” language context? Or breaks if you don’t set a layout for the Print device?

I would much rather wait an additional 3 months for any Sitecore release, if it means it has been fully implemented throughout the Sitecore organisation before being released to the general public. This will not cover all usage scenarios of Sitecore as a product - but it WILL get rid of “all those little annoyances” that are so very hard to explain to our customers. “Why does my image links break, if I do it this way instead of that way?” and so on. Fortunately, level 1 support is behind me - but then.. should we accept these kinds of issues at all?

Finally, and just for good measure, I will throw in a product feature comment. The Page Editor. Brilliant, but hugely underestimated right now. Find a good way to solve the problems of configurable controls (a.la SharePoint WebParts) getting easily added to layouts - and come up with some form of layout or rendering inheritance as pointed out by another commentator. Not necessarily saying the suggested approach is the best one (although it very well might be), but it certainly is something that I would like to see addressed and solved.

In summary:

  1. Document. Love your developer base, please ;-) Take a look at what the competition is doing, maybe expecting a new MSDN is asking too much after all ;-)
  2. Eat it yourselves. First.
  3. Keep evolving the Page Editor, which I honestly believe can be one of the absolute killer features and opens up a lot of opportunities for new development areas.

That’s not asking for much is it? ;-) We were invited however.

Into the core