首页 > 代码库 > SharePoint 2010 用xsl文件定制列表样式

SharePoint 2010 用xsl文件定制列表样式

有时候我们不希望列表用默认的方式显示,要我们自定义的方式定制。其中有一种方式是使用xsl文件。

在AllItems.aspx页面中,列表是以webpart的形式显示在页面上的,webpart类型是XsltListViewWebPart,当我们编辑web部件的时候,有一个属性是“XSL 链接”,我们就定义一个xsl文件,然后将xsl的文件链接放到这里就完成任务了。

下面是我的xsl代码,将它保存到customstyle.xsl文件中,将这个文件复制到,/_layouts/xsl/下。

  1 <xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" 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" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" ddwrt:ghost="show_all">
  2 <xsl:include href="/_layouts/xsl/main.xsl"/>
  3 <xsl:include href="/_layouts/xsl/internal.xsl"/>
  4 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  5 <xsl:output method="html" indent="yes"/>
  6 <xsl:template xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:sharepoint="Microsoft.SharePoint.WebControls" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" match="/" ddwrt:ghost="">
  7 <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row[$EntityName = ‘‘ or (position() >= $FirstRow and position() <= $LastRow)]"/>
  8 <table cellpadding="10" cellspacing="0" border="0" width="100%">
  9 <xsl:for-each select="$Rows">
 10 <xsl:call-template name="RowView"></xsl:call-template>
 11 </xsl:for-each>
 12 <tr>
 13 <td colspan="2">
 14 <xsl:call-template name="pagingButtons"/>
 15 </td>
 16 </tr>
 17 </table>
 18 </xsl:template>
 19 <xsl:template name="RowView">
 20 <xsl:variable name="thisNode" select="."/>
 21 <tr>
 22 <xsl:if test="position() mod 2 = 1">
 23 <xsl:attribute name="class">ms-alternating</xsl:attribute>
 24 </xsl:if>
 25 <td class="ms-vb" style="border-bottom:dotted 1px #333;padding:5px 5px">
 26 <h3 style="padding:0;margin:0;font-size:22px">
 27 <xsl:call-template name="LinkTitleTem"/>
 28 </h3>
 29 <p style="padding:0;margin:0;color:#777">
 30 <span>
 31 <xsl:value-of select="$thisNode/@Author.title" disable-output-escaping="yes"/>
 32 <span style="padding:0 2px">/</span>
 33 <xsl:value-of select="@_x65b0__x95fb__x7c7b__x578b_"/>
 34 <span style="padding:0 2px">/</span>
 35 <xsl:value-of select="ddwrt:FormatDate(string($thisNode/@_x65b0__x95fb__x65f6__x95f4_),2052,3)"/>
 36 </span>
 37 </p>
 38 <div style=" clear:both;height:1px;overflow:hidden;margin-top:-1px"/>
 39 <p style="margin:0px;text-indent:2em;color:#333;font-size:14px;margin-bottom: 5px;line-height: 27px;font-family: Tahoma,‘Hiragino Sans Gb‘,‘Microsoft YaHei‘;font-weight: normal;vertical-align: middle;padding-right:20px">
 40 <xsl:value-of select="$thisNode/@_x65b0__x95fb__x7b80__x4ecb_"/>
 41 </p>
 42 <p style="text-align:right;padding-right:30px">
 43 <xsl:call-template name="DocDetails"/>
 44 </p>
 45 </td>
 46 </tr>
 47 </xsl:template>
 48 <xsl:template name="DocDetails">
 49 <xsl:variable name="thisNode" select="."/>
 50 <a>
 51 <xsl:attribute name="href">
 52 <xsl:value-of select="$thisNode/@FileDirRef"/>
 53 customdisp.aspx?id=
 54 <xsl:value-of select="@ID"/>
 55 </xsl:attribute>
 56 <xsl:attribute name="style">
 57 font-size: 12px;font-size: 14px;font-weight: bold;color:#333
 58 </xsl:attribute>
 59 阅读全文
 60 </a>
 61 </xsl:template>
 62 <xsl:template name="LinkTitleTem" ddwrt:ghost="">
 63 <xsl:variable name="thisNode" select="."/>
 64 <a>
 65 <xsl:attribute name="href">
 66 <xsl:value-of select="$thisNode/@FileDirRef"/>
 67 customdisp.aspx?id=
 68 <xsl:value-of select="@ID"/>
 69 </xsl:attribute>
 70 <xsl:attribute name="style">
 71 line-height: 30px;font-size: 16px;color: #333;font-weight: bold;
 72 </xsl:attribute>
 73 <xsl:value-of select="$thisNode/@Title"/>
 74 </a>
 75 </xsl:template>
 76 <xsl:template name="pagingButtons">
 77 <xsl:choose>
 78 <xsl:when test="$XmlDefinition/List/@TemplateType = 106 and $XmlDefinition/@RecurrenceRowset=‘TRUE‘">
 79 <xsl:if test="$dvt_nextpagedata or $dvt_prevpagedata">
 80 <xsl:call-template name="CalendarExpandedRecurrenceFooter"/>
 81 </xsl:if>
 82 </xsl:when>
 83 <xsl:otherwise>
 84 <xsl:if test="$XmlDefinition/RowLimit[@Paged=‘TRUE‘]">
 85 <xsl:call-template name="CommandFooter">
 86 <xsl:with-param name="FirstRow" select="$FirstRow"/>
 87 <xsl:with-param name="LastRow" select="$LastRow"/>
 88 <xsl:with-param name="dvt_RowCount" select="$dvt_RowCount"/>
 89 </xsl:call-template>
 90 </xsl:if>
 91 </xsl:otherwise>
 92 </xsl:choose>
 93 </xsl:template>
 94 <xsl:template name="CommandFooter">
 95 <xsl:param name="FirstRow" select="1"/>
 96 <xsl:param name="LastRow" select="1"/>
 97 <xsl:param name="dvt_RowCount" select="1"/>
 98 <xsl:if test="$FirstRow > 1 or $dvt_nextpagedata">
 99 <xsl:call-template name="Navigation">
100 <xsl:with-param name="FirstRow" select="$FirstRow"/>
101 <xsl:with-param name="LastRow" select="$LastRow"/>
102 <xsl:with-param name="dvt_RowCount" select="$dvt_RowCount"/>
103 </xsl:call-template>
104 </xsl:if>
105 </xsl:template>
106 <xsl:template xmlns:ddwrt2="urn:frontpage:internal" name="Navigation" ddwrt:ghost="">
107 <xsl:param name="FirstRow" select="1"/>
108 <xsl:param name="LastRow" select="1"/>
109 <xsl:param name="dvt_RowCount" select="1"/>
110 <xsl:variable name="LastRowValue">
111 <xsl:choose>
112 <xsl:when test="$EntityName = ‘‘ or $LastRow < $RowTotalCount">
113 <xsl:value-of select="$LastRow"/>
114 </xsl:when>
115 <xsl:otherwise>
116 <xsl:value-of select="$RowTotalCount"/>
117 </xsl:otherwise>
118 </xsl:choose>
119 </xsl:variable>
120 <xsl:variable name="NextRow">
121 <xsl:value-of select="$LastRowValue + 1"/>
122 </xsl:variable>
123 <table id="bottomPagingCell{$WPQ}" style="font-size:25px;width:100%;padding:5px;" border="0">
124 <tr>
125 <td style="width:50%">
126 <xsl:if test="$dvt_firstrow > 1">
127 <a>
128 <xsl:choose>
129 <xsl:when test="$dvt_RowCount = 0 and not($NoAJAX)">
130 <xsl:attribute name="onclick">
131 javascript:RefreshPageTo(event, "
132 <xsl:value-of select="$PagePath"/>
133 ?
134 <xsl:value-of select="$ShowWebPart"/>
135 \u0026
136 <xsl:value-of select="$FieldSortParam"/>
137 <xsl:value-of select="$SortQueryString"/>
138 \u0026View=
139 <xsl:value-of select="$View"/>
140 ");javascript:return false;
141 </xsl:attribute>
142 <xsl:attribute name="href">javascript:</xsl:attribute>
143 <img src="/_layouts/{$LCID}/images/prev.gif" border="0" alt="{$Rows/@idRewind}"/>
144 <img src="/_layouts/{$LCID}/images/prev.gif" border="0" alt="{$Rows/@idRewind}"/>
145 </xsl:when>
146 <xsl:otherwise>
147 <xsl:variable name="RealRowLimit">
148 <xsl:choose>
149 <xsl:when test="$XmlDefinition/Query/GroupBy[@Collapse=‘TRUE‘]/@GroupLimit">
150 <xsl:value-of select="$XmlDefinition/Query/GroupBy[@Collapse=‘TRUE‘]/@GroupLimit"/>
151 </xsl:when>
152 <xsl:otherwise>
153 <xsl:value-of select="$XmlDefinition/RowLimit"/>
154 </xsl:otherwise>
155 </xsl:choose>
156 </xsl:variable>
157 <xsl:choose>
158 <xsl:when test="not($NoAJAX)">
159 <xsl:attribute name="onclick">
160 javascript:RefreshPageTo(event, "
161 <xsl:value-of select="$PagePath"/>
162 ?
163 <xsl:value-of select="$dvt_prevpagedata"/>
164 <xsl:value-of select="$ShowWebPart"/>
165 \u0026PageFirstRow=
166 <xsl:value-of select="$FirstRow - $RealRowLimit"/>
167 \u0026
168 <xsl:value-of select="$FieldSortParam"/>
169 <xsl:value-of select="$SortQueryString"/>
170 \u0026View=
171 <xsl:value-of select="$View"/>
172 ");javascript:return false;
173 </xsl:attribute>
174 <xsl:attribute name="href">javascript:</xsl:attribute>
175 </xsl:when>
176 <xsl:otherwise>
177 <xsl:attribute name="href">
178 javascript:
179 <xsl:call-template name="GenFireServerEvent">
180 <xsl:with-param name="param" select="concat(‘dvt_firstrow={‘,$FirstRow - $XmlDefinition/RowLimit,‘};dvt_startposition={‘,$dvt_prevpagedata,‘}‘)"/>
181 </xsl:call-template>
182 </xsl:attribute>
183 </xsl:otherwise>
184 </xsl:choose>
185 <img src="/_layouts/{$LCID}/images/prev.gif" border="0" alt="{$Rows/@idPrevious}"/>
186 前一页
187 </xsl:otherwise>
188 </xsl:choose>
189 </a>
190 </xsl:if>
191 </td>
192 <td style="width:50%" align="right">
193 <xsl:if test="$LastRowValue < $dvt_RowCount or string-length($dvt_nextpagedata)!=0">
194 <a>
195 <xsl:choose>
196 <xsl:when test="not($NoAJAX)">
197 <xsl:attribute name="onclick">
198 javascript:RefreshPageTo(event, "
199 <xsl:value-of select="$PagePath"/>
200 ?
201 <xsl:value-of select="$dvt_nextpagedata"/>
202 <xsl:value-of select="$ShowWebPart"/>
203 \u0026PageFirstRow=
204 <xsl:value-of select="$NextRow"/>
205 \u0026
206 <xsl:value-of select="$FieldSortParam"/>
207 <xsl:value-of select="$SortQueryString"/>
208 \u0026View=
209 <xsl:value-of select="$View"/>
210 ");javascript:return false;
211 </xsl:attribute>
212 <xsl:attribute name="href">javascript:</xsl:attribute>
213 </xsl:when>
214 <xsl:otherwise>
215 <xsl:attribute name="href">
216 javascript:
217 <xsl:call-template name="GenFireServerEvent">
218 <xsl:with-param name="param" select="concat(‘dvt_firstrow={‘,$NextRow,‘};dvt_startposition={‘,$dvt_nextpagedata,‘}‘)"/>
219 </xsl:call-template>
220 </xsl:attribute>
221 </xsl:otherwise>
222 </xsl:choose>
223 后一页
224 <img src="/_layouts/{$LCID}/images/next.gif" border="0" alt="{$Rows/@tb_nextpage}"/>
225 </a>
226 </xsl:if>
227 </td>
228 </tr>
229 </table>
230 <xsl:if test="not($GroupingRender)">
231 <script>
232 var topPagingCell = document.getElementById("topPagingCell
233 <xsl:value-of select="$WPQ"/>
234 "); var bottomPagingCell = document.getElementById("bottomPagingCell
235 <xsl:value-of select="$WPQ"/>
236 "); if (topPagingCell != null && bottomPagingCell != null) { topPagingCell.innerHTML = bottomPagingCell.innerHTML; }
237 </script>
238 </xsl:if>
239 </xsl:template>
240 </xsl:stylesheet>
View Code

修改webpart属性为/_layouts/xsl/customstyle.xsl.

显示效果:

这种方式将覆盖SharePoint默认的显示样式。

有的时候我们的列表已经创建了好多,一个一个的修改又很麻烦,下面写了一小段代码,快速的修改webpart属性,如果只有几个列表还是手动修改的比较快,可以根据情况写到控制台程序中。

 1 SPList listdd = web.Lists.TryGetList(list);
 2                         string dfdf = listdd.DefaultViewUrl;
 3                         SPFile file = web.GetFile(dfdf);
 4                         
 5                         SPLimitedWebPartManager limitedWebPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
 6                         SPLimitedWebPartCollection col = limitedWebPartManager.WebParts;
 7                         foreach (System.Web.UI.WebControls.WebParts.WebPart item in col)
 8                         {
 9                             if (item.GetType().Name == "XsltListViewWebPart")
10                             {
11                                 (item as BaseXsltListWebPart).XslLink = "/_layouts/xsl/customstyle.xsl";
12                                 limitedWebPartManager.SaveChanges(item);
13                             }
14                         }
View Code