XSL, Not Javascript, in SharePoint Forms

1 minute read

I’ve been working a lot with custom forms in SharePoint lately, and I’m finding all sorts of shortcuts.

Here’s a good example.  I’ve got a form on which I am allowing the user to select 1-n tickets for an event by ticking a checkbox in each row.  The form displays all available tickets for the event, as well as any tickets that the user has already selected (so that they can deselect the ticket to “give it back”).  So, when the page loads, I want to set the state of each checkbox to reflect whether it has already been selected.

This is an occasion where it may feel natural to turn to Javascript.  I certainly did, because I already had some Javascript in the page that I could clone and alter to my needs.  However, rather than needing this Javascript (and all its attendant functions):

function setCheckBoxes() {
  rows = getRowsWithFieldName('input', 'RequestID', 'ff1_'); //Get a count of the number of rows displayed
  for (var i = 1; i <= rows; i++) { //For each row
    RequestID = getValueWithFieldName('input', 'RequestID', 'ff1_' + i); //Get the RequestID
    if(RequestID != 'NOID') { //If the RequestID isn't equal to our default 'unrequested' value of NOID
      setCheckedWithFieldName('input', 'RequestTicket', 'ff4_' + i); //Set the checkbox

I can just do this in the XSL:

&#91;sourcecode language="xml"&#93;<input title="RequestTicket" name="RequestTicket" id="ff4{$Pos}" type="checkbox" onclick="setIncrementDecrement(this.checked, {$Pos}', '{$RequestID}')">
  <xsl:if test="@RequestID = $RequestID">
    <xsl:attribute name="checked" />

I’ve used the XSL method many times before, but that temptation to use Javascript got me. Clearly in this case, the XSL makes for a nicer, more maintainable solution.


Have a thought or opinion?