Andrey Markeev on SharePoint

SharePoint XSLT: how to deal with debug js versions

September 15, 2011

Client Object Model and SharePoint Sandbox could probably bring you to a JavaScript code overgrowth. And sooner or later, you will start to think about minifying your javascript code. But for debug purposes, it would be great to have a debug version of code with some comments and indentation.

So,  how to deal with that?

Turns out, SharePoint already have a framework to achieve this. All you need to do is to use OOTB ScriptLink control.

Wictor Wilén has a great article covering this topic, there you can find information about Microsoft JS minifier and more. Check it: Minifying custom JavaScript files in SharePoint 2010.

And here I just would like to add: occasionally, you might need to reference your script from XSLT. And in XsltListViewWebPart, server controls are not allowed. So, what to do?

The solution is very simple, you're looking for ddwrt:IfDebug function. You can use it following way:

<xsl:choose>
  <xsl:when test="ddwrt:IfDebug() = 'True'">
    <script type="text/javascript" src="/_layouts/script.debug.js" />
  </xsl:when>
  <xsl:otherwise>
    <script type="text/javascript" src="/_layouts/script.js" />
  </xsl:otherwise>
</xsl:choose>

Like it?

About me

I'm SharePoint MVP, published author, frequent speaker, opensource projects creator and online expert.

I'm one of the top 10 experts on SharePoint StackExchange:

profile for omlin at SharePoint, Q&A for SharePoint enthusiasts

You can learn more about me on LinkedIn and Facebook.

Want to follow?

Subscribe to RSS!

Twitter

Blog archive