wtorek, 26 marca 2013

How to tune list view of style "Preview Pane". Part 2: no selection on page load

Another problem with the list or library view with style "Preview Pane" is that when the page loads, no item on the list is selected and as a result no field values are displayed in the table.
To correct this behaviour you need the help of javascript and jquery library.
Run SharePoint Designer and go to the view edition. You have to place these statements into the view code:

<scriptlink id="ScriptLink1" name="jquery.min.js" loadafterui="false" localizable="false" runat="server"></scriptlink>
<script type="text/javascript">
jQuery(document).ready(function () { $("div.ms-ppleft table tr td.ms-vb-title").first().trigger("onfocus"); })
</script>
The above code assumes that the jquery library file "jquery.min.js" is placed in folder C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS.

How to tune list view of style "Preview Pane". Part 1: duplicated "Name" field


When list's or library's view has style "Preview Pane", it shows all items' names in the list and the rest of the fields for the currently selected item is shown in the table. I don't like that the "Name" field gets duplicated in the list and in the table.

To correct this situation, run the SharePoint Designer and go to the view edition.
The inner name of the "Name" field is different for the list (LinkTitle) and for the library (LinkFilename). You can check the fields shown in the view looking for the <ViewFields> section in the code:

<ViewFields>
  <FieldRef Name="LinkFilename"/>
  <FieldRef Name="CustomField"/>
  <FieldRef Name="Modified"/>
</ViewFields>

First change the order of the fields so the "Name" field has the last position. You can do it in code or in Web GUI. 

Now find in code section that is responsible for drawing the table with fields:


<table class="ms-formtable" border="0" cellpadding="0" cellspacing="0" width="100%">
  <xsl:for-each select="ViewFields/FieldRef[not(@Explicit='TRUE')]">
    <tr>
      <td nowrap="nowrap" valign="top" width="190px"  class="ms-formlabel">
        <nobr>
          <xsl:value-of select="@DisplayName"/>
        </nobr>
      </td>
      <td valign="top" class="ms-formbody" width="400px" id="n{position()}{$WPQ}">
        <xsl:text disable-output-escaping="yes" ddwrt:nbsp-preserve="yes">&amp;nbsp;</xsl:text>
      </td>
    </tr>
  </xsl:for-each>
</table>

Add the xsl:if statement to eliminate the Name field from the table:

<table class="ms-formtable" border="0" cellpadding="0" cellspacing="0" width="100%">
  <xsl:for-each select="ViewFields/FieldRef[not(@Explicit='TRUE')]">
    <xsl:if test="@Name!='LinkFilename'">
      <tr>
        <td nowrap="nowrap" valign="top" width="190px"  class="ms-formlabel">
          <nobr>
            <xsl:value-of select="@DisplayName"/>
          </nobr>
        </td>
        <td valign="top" class="ms-formbody" width="400px" id="n{position()}{$WPQ}">
          <xsl:text disable-output-escaping="yes" ddwrt:nbsp-preserve="yes">&amp;nbsp;</xsl:text>
        </td>
      </tr>
    </xsl:if>
  </xsl:for-each>
</table>