Monday 31 October 2011

Filtering the InfoPath 2010 Multiple-Selection List Box

So you want to have a Multiple Selection List Box on a form that displays values from a secondary data-source but want to store the value ids within the main data source. No problem.

Now just the selected values need to be displayed. Just filter the main data source values with a lookup right – wrong! This is not compatible with InfoPath Form Services. In order to achieve this you must add the secondary data source as a repeating section. Add a calculated field within this for the display value with conditional formatting to only display the values that occur within the main data source. This works !?

image

To check that the ID is within the main data source: Count how many nodes exist within the main data source exist with the same ID as the one being displayed, if it is less than one then don’t display i.e.

count(xdXDocument:get-DOM()/my:myForm/my:MainList/my:mainItems[. = current()/d:ID]) < 1

Spell Check within InfoPath 2010 Browser Forms

One thing I have never understood is the lack of a spell check facility within InfoPath Form Services forms. SharePoint has a spell checking web service which can be viewed at “_vti_bin/SpellCheck.asmx” so why can this not be used ?

Well it can and here’s how:

Override a list form into an InfoPath form via the Customise form options:

image

Once saved select the Form Web Parts and edit the New Form as follows: Add a Content Editor Web Part to the top; edit the HTML and add the following:

   1:  <script language="javascript" src="/_layouts/1033/SpellCheckEntirePage.js?rev=zYQ05cOj5Dk74UkTZzEIRw%3D%3D" type="text/javascript"></script>
   2:  <script language="javascript" type="text/javascript">
   3:     function CheckSpelling()
   4:     {
   5:        SpellCheckEntirePage('/_vti_bin/SpellCheck.asmx', '/_layouts/SpellChecker.aspx');      
   6:     }
   7:  </script>
   8:   
   9:  <a onclick="CheckSpelling();" href="javascript:"><img alt="Spell Check" src="_layouts/1033/images/RTE2SPCHK.GIF" style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px"/></a>
  10:   

The form spell check relies on the SpellCheckEntirePage JavaScript file so this must first be included. All that is left to do is call it.

Save and close the page and now should end up with something that looks as follows:

Untitled

Press the spell check button and the form is spell checked. Should you not want to spell check a specific field then you will need to exclude the controls first by setting the attribute excludeFromSpellCheck to true.

It would be nice to package this into a feature for the Ribbon but I will leave this as an exercise for the reader.

Wednesday 5 October 2011

XSLT List View Web Part (XLV)

XLV is the default List view technology in SharePoint 2010 replacing the XSLT LVWP used within 2007. Within XLV the data is generated directly from the ViewXML providing us with a consistent way of formatting the results. More information can be found on the SharePoint Team Blog and on MSDN.

To format a web part, edit the web part and expand the Miscellaneous property section, you will notice an XSL Link property. This allows us to override the default formatting by entering a path to an alternate XSL transform file.

To illustrate, upload the following XML to a folder in the Style Library of your site collection with the name “xlvAll.xsl”. Now add a task web part to your home page and set the XSL Link property to point to the file. The web part should render the raw ViewXml, this is a great starting point for formatting the results.

   1:  <?xml version="1.0" encoding="UTF-8"?>
   2:  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   3:  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
   4:      <xsl:template match="/">
   5:          <xmp>
   6:              <xsl:copy-of select="*"/>
   7:          </xmp>
   8:      </xsl:template>
   9:  </xsl:stylesheet>

So lets format the results of the web part, upload the following XML to the same Style Library folder with the name “xlvTask.xsl”. Update the XSL Link property to point to the file.


   1:  <xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal" xmlns:o="urn:schemas-microsoft-com:office:office">    
   2:      <xsl:template match="/">
   3:          <table class="ms-listviewtable" cellspacing="0" cellpadding="1" width="100%" border="0">     
   4:              <thead>
   5:                  <th class="ms-vh2">Title</th>
   6:                  <th class="ms-vh2">Status</th>
   7:                  <th class="ms-vh2">Percent Complete</th>
   8:              </thead>      
   9:              <xsl:apply-templates />
  10:          </table>
  11:      </xsl:template>
  12:   
  13:      <xsl:template match="Row">
  14:          <tr>
  15:              <td> 
  16:                  <xsl:value-of select="@Title"/> 
  17:              </td> 
  18:              <td> 
  19:                  <xsl:value-of select="@Status"/> 
  20:              </td> 
  21:              <td> 
  22:                  <xsl:value-of select="@PercentComplete"/> 
  23:              </td> 
  24:          </tr>
  25:      </xsl:template>
  26:      
  27:  </xsl:stylesheet>

This should give you something resembling the image below:


image


While being able to override the formatting of the web part is useful it is not always appropriate, sometimes we just need to format a couple of columns, this is where the new format comes into its own. The XSL below will override the status and the percent complete fields to display them graphically within the existing web part and the best bit is that we can re-use this across multiple web parts.


   1:  <xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal" xmlns:o="urn:schemas-microsoft-com:office:office">
   2:      <xsl:include href="/_layouts/xsl/main.xsl"/>
   3:      <xsl:include href="/_layouts/xsl/internal.xsl"/> 
   4:   
   5:      <xsl:template name="FieldRef_body.Status" match="FieldRef[@Name='Status']" mode="body">
   6:          <xsl:param name="thisNode" select="."/>
   7:              <xsl:choose>
   8:                  <xsl:when test="$thisNode/@*[name()=current()/@Name] = 'Completed'">
   9:                      <img src="/_layouts/images/IMNON.png" alt="Status: {$thisNode/@Status}"/>
  10:                  </xsl:when>
  11:                  <xsl:when test="$thisNode/@*[name()=current()/@Name] = 'In Progress'">
  12:                      <img src="/_layouts/images/IMNIDLE.png" alt="Status: {$thisNode/@Status}"/>
  13:                  </xsl:when>
  14:                  <xsl:otherwise>
  15:                      <img src="/_layouts/images/IMNBUSY.png" alt="Status: {$thisNode/@Status}"/>
  16:                  </xsl:otherwise>
  17:              </xsl:choose>
  18:      </xsl:template>
  19:      
  20:      <xsl:template name="FieldRef_Number_body.PercentComplete" ddwrt:dvt_mode="body" match="FieldRef[@Name='PercentComplete']" mode="Number_body" ddwrt:ghost="" xmlns:ddwrt2="urn:frontpage:internal">
  21:          <xsl:param name="thisNode" select="."/>
  22:          <div style="position:relative;width:100%;height:15px;background-color:red;color:white;">                         
  23:              <div>
  24:                  <xsl:attribute name='style'>height:15px;background-color:green;width:<xsl:value-of select="translate($thisNode/@PercentComplete,' %','')"/>%</xsl:attribute>
  25:              </div>                        
  26:              <div style="position:absolute;top:0;left:0;text-align:center;width:100%"><xsl:value-of select="$thisNode/@PercentComplete"/></div>
  27:          </div>
  28:      </xsl:template>
  29:  </xsl:stylesheet>

This results in the following:image


I should mention that you of course can open SharePoint Designer and format the web parts on the page however this is not always appropriate.