1,640 articles and 11,930 comments as of Wednesday, July 21st, 2010

Tuesday, January 26, 2010

Unlocking the Mysteries of Data View Web Part XSL Tags – Part 3: xsl:call-template

Author: Marc D. Anderson
http://mdasblog.wordpress.com

<xsl:call-template>

This is how you call a template which you’ve defined with <xsl-template>.

Unlocking Mysteries Part 3

In the last installment, I showed you what templates SharePoint Designer usually creates when you set up a Data View Web Part (DVWP). Having the templates in place is great, but you need a way to chain them together to make them do anything. That’s where <xsl:call-template> comes in.

Hopefully, even if you don’t have a programming background, this can make sense. Look at the diagram to the right and think of this like a flow chart. Each of the blue squares represents an <xsl:template>, and the arrows represent how <xsl:call-template> allows you to pass control from one template to the next. (The arrows are on the right and left just to make it look better; there’s no distinction intended.)

The order in which you call the templates makes a difference, of course. You need to put on your socks before your shoes; cook dinner before you can eat it, watch TV, and go to bed, etc.

If you look at the sample XSL again, you’ll see where <xsl:call-template> is used:

<XSL><xsl:stylesheet xmlns:x="<a href="http://www.w3.org/2001/XMLSchema">http://www.w3.org/2001/XMLSchema</a>" xmlns:d="<a href="http://schemas.microsoft.com/sharepoint/dsp">http://schemas.microsoft.com/sharepoint/dsp</a>" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="<a href="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">http://schemas.microsoft.com/WebParts/v2/DataView/runtime</a>" xmlns:asp="<a href="http://schemas.microsoft.com/ASPNET/20">http://schemas.microsoft.com/ASPNET/20</a>" xmlns:__designer="<a href="http://schemas.microsoft.com/WebParts/v2/DataView/designer">http://schemas.microsoft.com/WebParts/v2/DataView/designer</a>" xmlns:xsl="<a href="http://www.w3.org/1999/XSL/Transform">http://www.w3.org/1999/XSL/Transform</a>" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">

  <xsl:output method="html" indent="no"/>
  <xsl:decimal-format NaN=""/>
  <xsl:param name="dvt_apos">'</xsl:param>
  <xsl:variable name="dvt_1_automode">0</xsl:variable>

  <xsl:template match="/">
    <xsl:call-template name="dvt_1"/>
  </xsl:template>

  <xsl:template name="dvt_1">
    <xsl:variable name="dvt_StyleName">Table</xsl:variable>
    <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/>
    <table border="0" width="100%" cellpadding="2" cellspacing="0">
      <tr valign="top">
        <xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
          <th width="1%" nowrap="nowrap"></th>
        </xsl:if>
        <th nowrap="nowrap">Title</th>
      </tr>
      <xsl:call-template name="dvt_1.body">
        <xsl:with-param name="Rows" select="$Rows"/>
      </xsl:call-template>
    </table>
  </xsl:template>

  <xsl:template name="dvt_1.body">
    <xsl:param name="Rows"/>
    <xsl:for-each select="$Rows">
      <xsl:call-template name="dvt_1.rowview"/>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="dvt_1.rowview">
    <tr>
      <xsl:if test="position() mod 2 = 1">
        <xsl:attribute name="class">ms-alternating</xsl:attribute>
      </xsl:if>
      <xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
        <td width="1%" nowrap="nowrap">
          <span ddwrt:amkeyfield="ID" ddwrt:amkeyvalue="ddwrt:EscapeDelims(string(@ID))" ddwrt:ammode="view"></span>
        </td>
      </xsl:if>
      <td>
        <xsl:value-of select="@Title"/>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet></XSL>

You’ll see that each of the first three templates uses <xsl:call-template> to pass control along. The dvt_1.rowview template doesn’t use <xsl:call-template> because it’s the “end of the line”.

<xsl:template match="/">
  <xsl:call-template name="dvt_1"/>
</xsl:template>

<xsl:template name="dvt_1">
  <xsl:call-template name="dvt_1.body">
    …
  </xsl:call-template>
</xsl:template>

<xsl:template name="dvt_1.body">
  <xsl:call-template name="dvt_1.rowview"/>
</xsl:template>

Note that the code section above is just the skeleton to show you how things fit together; it isn’t valid XSL.

Unlocking Mysteries Part 3Here’s a little bonus trick you can use to see the raw XML which is being returned from your DataSource. If you drop this XSL section in, replacing everything between the <XSL> and </XSL> tags, you’ll be able to see the raw XML as it’s coming back from your DataSource. This is handy when you’re working on displaying the results from a Search more nicely, but also anytime you just want to know exactly what’s in the result set.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ddwrt2="urn:frontpage:internal">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Depending on where you use this trick, you’ll probably need to View Source for the page to see the results. Search for <dsQueryResponse> and you’ll see the rows which are returned from the DataSource.

Next up: <xsl:with-param>. You use this with <xsl:call-template> when you want to pass a value into a template, usually a value that varies.

Author: Marc D. Anderson
http://mdasblog.wordpress.com

Marc D. Anderson is a Co-Founder and the President of Sympraxis Consulting LLC, based in Newton, MA.  He has over 25 years of experience as a technology consultant and line manager across a wide spectrum of industries and organizational sizes.  Marc has done extensive consulting on knowledge management and collaboration and what makes them actually work in practice.  Marc is a very frequent “answerer” on the MSDN SharePoint – Design and Customization forum.

Entries in this series:
  1. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 1: Overview
  2. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 2: xsl:template
  3. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 3: xsl:call-template
  4. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 4: xsl:with-param
  5. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 5: xsl:param
  6. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 6: xsl:variable
  7. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 7: xsl:for-each
  8. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 8: xsl:sort
  9. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 9: xsl:if
  10. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 10: xsl:choose
  11. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 11: xsl:value-of
  12. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 12: Miscellaneous - Person or Group Columns
  13. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 13: Miscellaneous - String Functions
  14. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 14: Miscellaneous – ddwrt Namespace Functions
  15. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 15: Miscellaneous – Field / Node Functions
  16. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 16: Miscellaneous – xsl:attribute
  17. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 17: Miscellaneous – xsl:comment and xsl:text
  18. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 18: Miscellaneous – Some Math / Number Functions
  19. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 19: Miscellaneous – More Math / Number Functions
  20. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 20: xsl:import
  21. EUSP eBook Store: First SharePoint Title is Now Available
 

Please Join the Discussion

6 Responses to “Unlocking the Mysteries of Data View Web Part XSL Tags – Part 3: xsl:call-template”
  1. Matt B. says:

    Very nice series and well explained. Thanks for creating this!

  2. Again, well written series, thanks :)

  3. LOVE_MOSS_NOT says:

    Just make sure you dont bother to make it the HTML + XSTL look nice, SharePoint designer will mess it all up afterwards.. :(

  4. LOVE_MOSS_NOT: That depends. If you make the DataSource look nice, SharePoint Designer will definitely mess it up. Once I’ve changed the formatting and the code, I rarely go back to the Designer dialogs again. One of the reasons is that things get “messed up”, but the other is that usually, based on the changes I’ve made, Designer doesn’t “understand” my code anymore. This is to be expected, as it can’t prdict what it is I’ve done; remember that it’s basically just a code generator.

    M.

  5. Adam says:

    Loving the series! – looking forward to the next one.

  6. Adam:

    Thanks! Just published another on today. I’m trying to get them out every few days or so. Let me know if there’s anything specific you’d like me to cover.

    M.


Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!