Exposing SharePoint Context to XSLT

Picking on my previous post, I am going to show how you can access SharePoint (and ASP.NET context) from XSLT.

Create a class like this one:

   1: public class ContextExtensions
   2: {
   3:     public Object HttpContext(String expression)
   4:     {
   5:         return (DataBinder.Eval(System.Web.HttpContext.Current, expression));
   6:     }
   8:     public Object SpContext(String expression)
   9:     {
  10:         return (DataBinder.Eval(SPContext.Current, expression));
  11:     }
  12: }

Follow the process described in my post to register it, apply tag mappings and register the controls as safe. Then, on the XSLT, you can access any properties (and properties of properties) of both HttpContext.Current and SPContext.Current instances! The only thing you can’t do is call methods. This is a limitation of DataBinder.Eval.

Some examples:

   1: User.Identity.Name: <xsl:value-of select="my:HttpContext('User.Identity.Name')"/>
   2: SPFormContext.FormMode: <xsl:value-of select="my:SpContext('FormContext.FormMode')"/>
   4: <xsl:variable name="TasksItemCount" select="my:SpContext('Web.Lists[&quot;Tasks&quot;].ItemCount')"/>
   5: Web.Lists["Tasks"].ItemCount: <xsl:value-of select="$TasksItemCount"/>
   7: <xsl:variable name="ListItemName" select="my:SpContext('ListItem[&quot;Name&quot;]')"/>
   8: ListItem[&quot;Name&quot;]: <xsl:value-of select="$TasksItemCount"/>

Notice that when you are to evaluate expressions with “, you must do it in two steps:

  1. Define a variable that takes the expression;
  2. In that variable, replace “ for &quot.

Hope you find it useful!


1 Comment

Add a Comment

As it will appear on the website

Not displayed

Your website