Wednesday, March 14, 2012

Cleaning XSLT Data View Web Part

SharePoint, as default, gives a lot of parameters to be rander in the XSL code.
After creating a new DVWP, we can clean the DVWP from unwanted parameters and functions, and convert it from <table> to <div> tags to be designed.
As for a start the XSL code can be at least 178 rows that we can remove and to be 31 rows tops. Sounds better and look better. The least rows we have, the faster the page will load.
There are many options and functions we can remove from the code that SharePoint Designer creates by default in a DVWP:
  • Creating a new Data View from “Empty Data View” (by the “Data View” button) – when creating a new Data View, SPD will generate the web part with parameters and the values already in the code. By creating an empty one, you will control most of the values and parameters to be added.
  • “Display all items” option (in the “Paging button), will remove some of the code.
  • <xsl:when test="($ManualRefresh = 'True')"> - This function builds a table with “Refreshing” option. In many DVWP we don’t use it – so we can erase this.
  • <xsl:call-template name="dvt_1.empty"/> and <xsl:template name="dvt_1.empty"> - Empty template
  • “ms-alternating” class function – there is a function to design all of the rows which divided by 2. When designing a web part with styles and classes there is no need for it because we are overwriting it with our styles.
  • <xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1"> - the automode is a function is used to switch modes, usually between display and edit.
1.  <XSL>
2.  <xsl:stylesheet xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ddw1="http://schemas.microsoft.com/sharepoint/soap/" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" 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">
3.   <xsl:output method="html" indent="no"/>
4.   <xsl:decimal-format NaN=""/>
5.   <xsl:param name="dvt_apos">'</xsl:param>
6.   <xsl:param name="ManualRefresh"></xsl:param>
7.   <xsl:param name="dvt_firstrow">1</xsl:param>
8.   <xsl:param name="dvt_nextpagedata" />
9.   <xsl:variable name="dvt_1_automode">0</xsl:variable>
10.  <xsl:template match="/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"   xmlns:ddw1="http://schemas.microsoft.com/sharepoint/soap/" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:SharePoint="Microsoft.SharePoint.WebControls">
11.
This function builds a table with “Refreshing” option. In many DVWP we don’t use it – so we can erase this.
  <xsl:choose>

12.    <xsl:when test="($ManualRefresh = 'True')">
13.     <table width="100%" border="0" cellpadding="0" cellspacing="0">
14.      <tr>
15.       <td valign="top">
16.        <xsl:call-template name="dvt_1"/>
17.       </td>
18.       <td width="1%" class="ms-vb" valign="top">
19.        <img src="/_layouts/images/staticrefresh.gif" id="ManualRefresh"
20. border="0" onclick="javascript: {ddwrt:GenFireServerEvent('__cancel')}"
21. alt="Click here to refresh the dataview."/>
22.       </td>
23.     </tr>
24.    </table>
25.   </xsl:when>
26.   <xsl:otherwise>
27.    <xsl:call-template name="dvt_1"/>
28.   </xsl:otherwise>
29.  </xsl:choose>
30. </xsl:template>
31.  
32. <xsl:template name="dvt_1">
33. <xsl:variable name="dvt_StyleName">Table</xsl:variable>
34. <xsl:variable name="Rows" select=”Data Source”/>
35. <xsl:variable name="dvt_RowCount" select="count($Rows)"/>
36. <xsl:variable name="RowLimit" select="10" />
37. <xsl:variable name="FirstRow" select="$dvt_firstrow" />
38.
This function is testing if there are items to display – if there are, it will run the dvt_1.empty. If not, it will run the dvt_1.body. Because we don’t use the function Empty and the table header, so we can erase the Yellow part and leave the “Call” to dvt_1.body.
<xsl:variable name="LastRow">

39. <xsl:choose>
40. <xsl:when test="($FirstRow + $RowLimit - 1) > $dvt_RowCount">
41. <xsl:value-of select="$dvt_RowCount" />
42. </xsl:when>
43. <xsl:otherwise>
44. <xsl:value-of select="$FirstRow + $RowLimit - 1" />
45. </xsl:otherwise>
46. </xsl:choose>
47. </xsl:variable>
48. <xsl:variable name="IsEmpty" select="$dvt_RowCount = 0" />
49. <xsl:variable name="dvt_IsEmpty" select="$dvt_RowCount = 0"/>
50.  
51. <xsl:choose>
52. <xsl:when test="$dvt_IsEmpty">
53. <xsl:call-template name="dvt_1.empty"/>
54. </xsl:when>
55. <xsl:otherwise>
56. <table border="0" width="100%" cellpadding="2" cellspacing="0">
57. <tr valign="top">
58. <xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
59. <th class="ms-vh" width="1%" nowrap="nowrap"></th>
60. </xsl:if>
61. <th class="ms-vh" nowrap="nowrap">ows_Title</th>
62. </tr>
63. <xsl:call-template name="dvt_1.body">
64. <xsl:with-param name="Rows" select="$Rows[position() >= $FirstRow and
65. position() <= $LastRow]"/>
66. <xsl:with-param name="FirstRow" select="1" />
67. <xsl:with-param name="LastRow" select="$dvt_RowCount" />
68. </xsl:call-template>
69. </table>
70. </xsl:otherwise>
71. </xsl:choose>
72. <xsl:call-template name="dvt_1.commandfooter">
73. <xsl:with-param name="FirstRow" select="$FirstRow" />
74. <xsl:with-param name="LastRow" select="$LastRow" />
75. <xsl:with-param name="RowLimit" select="$RowLimit" />
76. <xsl:with-param name="dvt_RowCount" select="$dvt_RowCount" />
77. <xsl:with-param name="RealLastRow" select="number(ddwrt:NameChanged('',-100))" />
78. </xsl:call-template>
79. </xsl:template>
80. <xsl:template name="dvt_1.body">
81. <xsl:param name="Rows"/>
82. <xsl:param name="FirstRow" />
83. <xsl:param name="LastRow" />
84. <xsl:for-each select="$Rows">
85. <xsl:variable name="dvt_KeepItemsTogether" select="false()" />
86. <xsl:variable name="dvt_HideGroupDetail" select="false()" />
87. <xsl:if test="(position() >= $FirstRow and position() <= $LastRow) or
88. $dvt_KeepItemsTogether">
89. <xsl:if test="not($dvt_HideGroupDetail)" ddwrt:cf_ignore="1">
90. <xsl:call-template name="dvt_1.rowview" />
91. </xsl:if>
92.
This test is testing if there is more than one item. If true it will give the second item another class (ms-alternating) to make it look good.
</xsl:if>
93. </xsl:for-each>
94. </xsl:template>
95. <xsl:template name="dvt_1.rowview">
96. <tr>
97. <xsl:if test="position() mod 2 = 1">
98. <xsl:attribute name="class">ms-alternating</xsl:attribute>
99.
This is checking once again for the value of $dvt_1_automode. If it is enabled, the contained table detail cell is rendered. Even Marc D Anderson is not sure, but we erase it.
</xsl:if>
100.          <xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
101.          <td class="ms-vb" width="1%" nowrap="nowrap">
102.          <span ddwrt:amkeyfield="" ddwrt:amkeyvalue="string($XPath)"
103.          ddwrt:ammode="view"></span>
104.          </td>
105.          </xsl:if>
106.         
This is where we enter the values to display
<td class="ms-vb">
107.          <xsl:value-of select="@ows_Title"/>
108.          </td>
109.          </tr>
110.          </xsl:template>
111.          <xsl:template name="dvt_1.empty">
112.          <xsl:variable name="dvt_ViewEmptyText">There are no items to show in this
113.         
This is where we define the parameters for the Empty Template.
view.</xsl:variable>
114.          <table border="0" width="100%">
115.          <tr>
116.          <td class="ms-vb">
117.          <xsl:value-of select="$dvt_ViewEmptyText"/>
118.          </td>
119.          </tr>
120.          </table>
121.          </xsl:template>
122.           
123.         
This is the Command footer and the navigation templates. If we don’t want paging with “Next” and “Previous” options, you can erase it.
<xsl:template name="dvt_1.commandfooter">
124.          <xsl:param name="FirstRow" />
125.          <xsl:param name="LastRow" />
126.          <xsl:param name="RowLimit" />
127.          <xsl:param name="dvt_RowCount" />
128.          <xsl:param name="RealLastRow" />
129.          <table cellspacing="0" cellpadding="4" border="0" width="100%">
130.          <tr>
131.          <xsl:if test="$FirstRow > 1 or $LastRow < $dvt_RowCount">
132.          <xsl:call-template name="dvt_1.navigation">
133.          <xsl:with-param name="FirstRow" select="$FirstRow" />
134.          <xsl:with-param name="LastRow" select="$LastRow" />
135.          <xsl:with-param name="RowLimit" select="$RowLimit" />
136.          <xsl:with-param name="dvt_RowCount" select="$dvt_RowCount" />
137.          <xsl:with-param name="RealLastRow" select="$RealLastRow" />
138.          </xsl:call-template>
139.          </xsl:if>
140.          </tr>
141.          </table>
142.          </xsl:template>
143.          <xsl:template name="dvt_1.navigation">
144.          <xsl:param name="FirstRow" />
145.          <xsl:param name="LastRow" />
146.          <xsl:param name="RowLimit" />
147.          <xsl:param name="dvt_RowCount" />
148.          <xsl:param name="RealLastRow" />
149.          <xsl:variable name="PrevRow">
150.          <xsl:choose>
151.          <xsl:when test="$FirstRow - $RowLimit < 1">1</xsl:when>
152.          <xsl:otherwise>
153.          <xsl:value-of select="$FirstRow - $RowLimit" />
154.          </xsl:otherwise>
155.          </xsl:choose>
156.          </xsl:variable>
157.          <xsl:variable name="LastRowValue">
158.          <xsl:choose>
159.          <xsl:when test="$LastRow > $RealLastRow">
160.          <xsl:value-of select="$LastRow"></xsl:value-of>
161.          </xsl:when>
162.          <xsl:otherwise>
163.          <xsl:value-of select="$RealLastRow"></xsl:value-of>
164.          </xsl:otherwise>
165.          </xsl:choose>
166.          </xsl:variable>
167.          <xsl:variable name="NextRow">
168.          <xsl:value-of select="$LastRowValue + 1"></xsl:value-of>
169.          </xsl:variable>
170.          <td nowrap="nowrap" class="ms-paging" align="right">
171.          <xsl:if test="$dvt_firstrow > 1" ddwrt:cf_ignore="1">
172.          <a>
173.          <xsl:attribute name="href">javascript:
174.          <xsl:value-of select="ddwrt:GenFireServerEvent('dvt_firstrow={1}')" />
175.          ;
176.          </xsl:attribute>
177.          Start</a>
178.          <xsl:text disable-output-escaping="yes" ddwrt:nbsp
179.          preserve="yes">&nbsp;</xsl:text>
180.          <a>
181.          <xsl:attribute name="href">javascript:
182.          <xsl:value-of
183.          select="ddwrt:GenFireServerEvent(concat('dvt_firstrow={',$PrevRow,'}'))”/>
184.          ;</xsl:attribute>
185.          <img src="/_layouts/images/prev.gif" border="0" alt="Previous" />
186.          </a>
187.          <xsl:text disable-output-escaping="yes" ddwrt:nbsp-
188.          preserve="yes">&nbsp;</xsl:text>
189.          </xsl:if>
190.          <xsl:value-of select="$FirstRow" />
191.          <xsl:value-of select="$LastRowValue" />
192.          <xsl:text disable-output-escaping="yes" ddwrt:nbsp-preserve="yes" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">&nbsp;</xsl:text>
193.           
194.          <xsl:if test="$LastRowValue < $dvt_RowCount or string length($dvt_nextpagedata)!=0" ddwrt:cf_ignore="1">
195.          <a>
196.          <xsl:attribute name="href">javascript: <xsl:value-of select="ddwrt:GenFireServerEvent(concat('dvt_firstrow={',$NextRow,'}'))" />;</xsl:attribute>
197.          <img src="/_layouts/images/next.gif" border="0" alt="Next" />
198.          </a>
199.          </xsl:if>
200.          </td>

201.          </xsl:template>

202.          </xsl:stylesheet></XSL>
Pretty big code, and with a lot unused decelerated functions. But after we clean it, it looks like this:
1.  <XSL><xsl:stylesheet xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ddw1="http://schemas.microsoft.com/sharepoint/soap/" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" 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">
2.  <xsl:output method="html" indent="no"/>
3.  <xsl:decimal-format NaN=""/>
4.  <xsl:param name="dvt_apos">'</xsl:param>
5.  <xsl:param name="dvt_firstrow">1</xsl:param>
6.  <xsl:param name="dvt_nextpagedata" />
7.  <xsl:template match="/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ddw1="http://schemas.microsoft.com/sharepoint/soap/" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:SharePoint="Microsoft.SharePoint.WebControls">
8.  <xsl:call-template name="dvt_1"/>
9.  </xsl:template>
10.  
11. <xsl:template name="dvt_1">
12. <xsl:variable name="dvt_StyleName">Table</xsl:variable>
13. <xsl:variable name="Rows" select=”Data Source”/>
14. <xsl:variable name="dvt_RowCount" select="count($Rows)"/>
15. <xsl:variable name="RowLimit" select="10" />
16. <xsl:variable name="FirstRow" select="$dvt_firstrow" />
17. <xsl:variable name="LastRow">
18. <xsl:choose>
19. <xsl:when test="($FirstRow + $RowLimit - 1) > $dvt_RowCount">
20. <xsl:value-of select="$dvt_RowCount" />
21. </xsl:when>
22. <xsl:otherwise>
23. <xsl:value-of select="$FirstRow + $RowLimit - 1" />
24. </xsl:otherwise>
25. </xsl:choose>
26. </xsl:variable>
27. <div>
28. <xsl:call-template name="dvt_1.body">
29. <xsl:with-param name="Rows" select="$Rows[position() >= $FirstRow and
30. position() <= $LastRow]"/>
31. <xsl:with-param name="FirstRow" select="1" />
32. <xsl:with-param name="LastRow" select="$dvt_RowCount" />
33. </xsl:call-template>
34. </div>
35. </xsl:template>
36. <xsl:template name="dvt_1.body">
37. <xsl:param name="Rows"/>
38. <xsl:param name="FirstRow" />
39. <xsl:param name="LastRow" />
40. <xsl:for-each select="$Rows">
41. <xsl:variable name="dvt_KeepItemsTogether" select="false()" />
42. <xsl:variable name="dvt_HideGroupDetail" select="false()" />
43. <xsl:if test="(position() >= $FirstRow and position() <= $LastRow) or
44. $dvt_KeepItemsTogether">
45. <xsl:if test="not($dvt_HideGroupDetail)" ddwrt:cf_ignore="1">
46. <xsl:call-template name="dvt_1.rowview" />
47. </xsl:if>
48. </xsl:if>
49. </xsl:for-each>
50. </xsl:template>
51. <xsl:template name="dvt_1.rowview">
52. <div>
53. <xsl:value-of select="@Title"/>
54. </div>
55. </xsl:template>
</xsl:stylesheet></XSL>
Now, the only thing have left is to insert out classes and styles and the DVWP is ready.
Good Luck!

4 comments:

  1. Hi dolev
    I met you at the last sharepoint user group in Microsoft Raanana.
    I asked you about showing list data (dvwp) from one website in another website , and you said something about using a list guid.
    can you please send me an example or post a blog
    about it
    10x
    Shlomy

    ReplyDelete
  2. Dear Shlomy,
    In the lecture with talked about exporting DVWP from onw site to another and not about displaying items from another site collection / web application.
    Unfortunately, This cannot be done using DVWP.

    ReplyDelete
    Replies
    1. Hi dolev
      many thanks for your response .
      I ment to display items from one site (/sites/jer/subsite1) in another site like (/site/jer/subsite2) or (/site/jer/subsite1/subsite11) without creating a dataview webpart for each site. i want to display ythe data in it . because dataview wp is fixed in its connection to the data source (list in my example)

      10x
      Shlomy

      Delete
    2. Please check this new post:
      http://dolevraz.blogspot.co.il/2012/12/spdatasource.html

      Delete