sharepoint 2007 中数据视图的使用
Sharepoint中的数据视图(DataFormWebPart)有着非常重要的作用,特别是在需要定制自己的页面显示格式的时候。
数据视图可以将列表,比如通知列表、博客列表、链接列表等转换成数据视图,然后可以任意控制显示格式
数据视图可以从多个数据源取数据来显示,这里的数据源可以是子网站中的列表、可以是数据库中的记录、可以是其它网站的RSS源
数据视图可以实现分页、排序、查询、过滤等功能
数据视图可以利用ddwrt:IfHasRights实现权限的判断.....
基本的功能我就不说,下面列出我在开发中觉得比较有用的、不太注意到的一些功能。
Sharepoint中的数据视图(DataFormWebPart)有着非常重要的作用,特别是在需要定制自己的页面显示格式的时候。
数据视图可以将列表,比如通知列表、博客列表、链接列表等转换成数据视图,然后可以任意控制显示格式
数据视图可以从多个数据源取数据来显示,这里的数据源可以是子网站中的列表、可以是数据库中的记录、可以是其它网站的RSS源
数据视图可以实现分页、排序、查询、过滤等功能
数据视图可以利用ddwrt:IfHasRights实现权限的判断.....
基本的功能我就不说了,下面列出我在开发中觉得比较有用的、不太注意到的一些功能。
首先给出一个最简单的数据视图的代码:

数据视图代码
1
<webpartpages:dataformwebpart runat="server" __markuptype="vsattributemarkup" viewflag="9"
2
viewcontenttypeid="0x" detaillink="/blog/Lists/OtherBlogs/AllItems.aspx" width=""
3
height="" framestate="Normal" isincluded="true" isvisible="true" dir="Default"
4
allowremove="true" allowzonechange="true" allowminimize="true" description="将“其他博客”列表用于指向其他博客的链接。"
5
missingassembly="无法导入此 Web 部件。" partimagesmall="" partimagelarge="" partorder="3"
6
displayname="" listname="{FB1AC17A-BC4E-495F-8D4B-251A88830D6F}" title="其他博客"
7
frametype="None" nodefaultstyle="TRUE" __webpartid="{3D57C8F6-1465-4337-B9C9-6D2EE8C12F3C}"
8
id="g_3d57c8f6_1465_4337_b9c9_6d2ee8c12f3c" pagesize="30" __allowxsltediting="true"
9
webpart="true">
10
<DataSources>
11
<SharePoint:SPDataSource runat="server" IncludeHidden="true" SelectCommand="<View><Query><OrderBy><FieldRef Name="Order" Ascending="TRUE"/></OrderBy></Query></View>"
12
ID="datasource2" DataSourceMode="List" UseInternalName="true">
13
<InsertParameters>
14
<asp:Parameter Name="ListID" DefaultValue="{FB1AC17A-BC4E-495F-8D4B-251A88830D6F}" />
15
</InsertParameters>
16
<UpdateParameters>
17
<asp:Parameter Name="ListID" DefaultValue="{FB1AC17A-BC4E-495F-8D4B-251A88830D6F}" />
18
</UpdateParameters>
19
<DeleteParameters>
20
<asp:Parameter Name="ListID" DefaultValue="{FB1AC17A-BC4E-495F-8D4B-251A88830D6F}" />
21
</DeleteParameters>
22
<SelectParameters>
23
<asp:Parameter Name="ListID" DefaultValue="{FB1AC17A-BC4E-495F-8D4B-251A88830D6F}" />
24
<asp:Parameter Name="MaximumRows" DefaultValue="30" />
25
</SelectParameters>
26
</SharePoint:SPDataSource>
27
</DataSources>
28
<ParameterBindings>
29
<ParameterBinding Name="PageUrl" Location="Postback;Connection;CAMLVariable"/>
30
<ParameterBinding Name="PagePath" Location="Postback;Connection;CAMLVariable"/>
31
<ParameterBinding Name="HttpHost" Location="Postback;Connection;CAMLVariable"/>
32
<ParameterBinding Name="HttpPath" Location="Postback;Connection;CAMLVariable"/>
33
<ParameterBinding Name="List" Location="Postback;Connection;CAMLVariable"/>
34
<ParameterBinding Name="URL_Display" Location="Postback;Connection;CAMLVariable"/>
35
<ParameterBinding Name="HttpVDir" Location="Postback;Connection;CAMLVariable"/>
36
<ParameterBinding Name="View" Location="Postback;Connection;CAMLVariable"/>
37
<ParameterBinding Name="FilterLink" Location="Postback;Connection;CAMLVariable"/>
38
<ParameterBinding Name="Language" Location="Postback;Connection;CAMLVariable"/>
39
<ParameterBinding Name="dvt_sortfield" Location="Postback;Connection;CAMLVariable"/>
40
<ParameterBinding Name="dvt_sortdir" Location="Postback;Connection;CAMLVariable"/>
41
<ParameterBinding Name="dvt_p2plinkfields" Location="Postback;Connection;CAMLVariable"/>
42
<ParameterBinding Name="dvt_sorttype" Location="Postback;Connection;CAMLVariable"/>
43
<ParameterBinding Name="dvt_apos" Location="Postback;Connection;CAMLVariable"/>
44
<ParameterBinding Name="filterParam" Location="Postback;Connection"/>
45
<ParameterBinding Name="ImagesPath" Location="Postback;Connection;CAMLVariable"/>
46
<ParameterBinding Name="ListUrlDir" Location="Postback;Connection;CAMLVariable"/>
47
<ParameterBinding Name="EMail" Location="Postback;Connection;CAMLVariable"/>
48
<ParameterBinding Name="Userid" Location="Postback;Connection;CAMLVariable"/>
49
<ParameterBinding Name="URL_New" Location="Postback;Connection;CAMLVariable"/>
50
<ParameterBinding Name="URL_Edit" Location="Postback;Connection;CAMLVariable"/>
51
<ParameterBinding Name="URL_Lookup" Location="Postback;Connection;CAMLVariable"/>
52
<ParameterBinding Name="UserID" Location="CAMLVariable" DefaultValue="CurrentUserName"/>
53
<ParameterBinding Name="Today" Location="CAMLVariable" DefaultValue="CurrentDate"/>
54
<ParameterBinding Name="Param1" Location="QueryString(Name)" DefaultValue=""/>
55
</ParameterBindings>
56
<Xsl>
57
<xsl:stylesheet version="1.0" exclude-result-prefixes="rs z o s ddwrt dt msxsl" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:o="urn:schemas-microsoft-com:office" xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" xmlns:ddwrt2="urn:frontpage:internal"><xsl:output method="html" indent="no" /><xsl:decimal-format NaN="" />
58
<xsl:param name="PageUrl" />
59
<xsl:param name="PagePath" />
60
<xsl:param name="HttpHost" />
61
<xsl:param name="HttpPath" />
62
<xsl:param name="List" />
63
<xsl:param name="URL_Display" />
64
<xsl:param name="HttpVDir" />
65
<xsl:param name="View" />
66
<xsl:param name="FilterLink" select="ddwrt:FilterLink()" />
67
<xsl:param name="Language">1033</xsl:param>
68
<xsl:param name="dvt_sortfield"></xsl:param>
69
<xsl:param name="dvt_sortdir">ascending</xsl:param>
70
<xsl:param name="dvt_p2plinkfields"></xsl:param>
71
<xsl:param name="dvt_sorttype">text</xsl:param>
72
<xsl:param name="dvt_apos">'</xsl:param>
73
<xsl:param name="filterParam" ddwrt:NoCAMLVariable="1"></xsl:param>
74
<xsl:param name="ImagesPath"></xsl:param>
75
<xsl:param name="ListUrlDir"></xsl:param>
76
<xsl:param name="EMail">EMail</xsl:param>
77
<xsl:param name="Userid"></xsl:param>
78
<xsl:variable name="dvt_1_automode">0</xsl:variable>
79
<xsl:param name="URL_New" />
80
<xsl:param name="URL_Edit" />
81
<xsl:param name="URL_Lookup" />
82
<xsl:param name="Param1" />
83
<xsl:param name="Param2" />
84
<xsl:param name="Param3" />
85
<xsl:param name="Param4" />
86
<xsl:template match="/" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:asp="http://schemas.microsoft.com/ASPNET/20">
87
<xsl:call-template name="dvt_1" />
88
</xsl:template>
89
90
91
92
<xsl:template name="dvt_1">
93
<xsl:variable name="dvt_StyleName">Table</xsl:variable>
94
<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row" />
95
<xsl:variable name="RowLimit" select="30" />
96
<xsl:variable name="dvt_RowCount" select="count($Rows)" />
97
98
<xsl:variable name="dvt_IsEmpty" select="$dvt_RowCount = 0" />
99
<xsl:choose>
100
<xsl:when test="$dvt_IsEmpty">
101
此列表中不包含任何项目
102
<xsl:call-template name="dvt_1.toolbar">
103
<xsl:with-param name="Rows" select="$Rows" />
104
</xsl:call-template>
105
</xsl:when>
106
<xsl:otherwise> <div class="ms-WPBody">
107
<table style="width: 40%; " cellspacing="0" cellpadding="0">
108
<xsl:call-template name="dvt_1.body">
109
<xsl:with-param name="ParentPath" />
110
<xsl:with-param name="Rows" select="$Rows" />
111
<xsl:with-param name="FirstRow" select="1" />
112
<xsl:with-param name="LastRow" select="$RowLimit" />
113
114
</xsl:call-template>
115
</table></div>
116
<xsl:call-template name="dvt_1.toolbar">
117
<xsl:with-param name="Rows" select="$Rows" />
118
</xsl:call-template>
119
</xsl:otherwise>
120
</xsl:choose>
121
</xsl:template>
122
<xsl:template name="dvt_1.body">
123
<xsl:param name="ParentPath" />
124
<xsl:param name="Rows" />
125
<xsl:param name="FirstRow" />
126
<xsl:param name="LastRow" />
127
<xsl:for-each select="$Rows">
128
<xsl:sort select="*[name() = $dvt_sortfield] | @*[name() = $dvt_sortfield] | text()[name(ancestor::*[1]) = $dvt_sortfield]" order="{$dvt_sortdir}" data-type="{$dvt_sorttype}" />
129
<xsl:variable name="dvt_KeepItemsTogether" select="false()" />
130
131
<xsl:variable name="dvt_HideGroupDetail" select="false()" />
132
<xsl:if test=" (position() >=$FirstRow and position() <= $LastRow) or $dvt_KeepItemsTogether">
133
<xsl:if test="not($dvt_HideGroupDetail)" ddwrt:cf_ignore="1">
134
<xsl:variable name="SrcPos"><xsl:number /></xsl:variable>
135
<xsl:call-template name="dvt_1.rowview">
136
<xsl:with-param name="ParentPath" select="$ParentPath" />
137
<xsl:with-param name="SrcPos" select="$SrcPos" />
138
</xsl:call-template>
139
</xsl:if>
140
</xsl:if>
141
</xsl:for-each>
142
</xsl:template>
143
<xsl:template name="dvt_1.rowview">
144
<xsl:param name="ParentPath" />
145
<xsl:param name="SrcPos" />
146
<tr>
147
<td style="height:24px" class="style10" >•
148
<A HREF="{substring-before(@URL, ', ')}">
149
<xsl:choose>
150
<xsl:when test="substring-after(@URL, ', ')=''"><xsl:value-of disable-output-escaping="no" select="substring-before(@URL, ', ')" /></xsl:when>
151
<xsl:otherwise><xsl:value-of select="substring-after(@URL, ', ')" /></xsl:otherwise>
152
</xsl:choose>
153
</A>
154
</td>
155
156
</tr>
157
</xsl:template>
158
<xsl:template name="dvt_1.toolbar">
159
<xsl:param name="Rows" />
160
<xsl:choose>
161
<xsl:when test="ddwrt:IfHasRights(2)"><table width="40%" cellpadding="0" cellspacing="0" border="0" msopnlid="data"> <tr> <td align="left" class="ms-addnew" style="padding-bottom: 3px">
162
<xsl:text xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:nbsp-preserve="yes" disable-output-escaping="yes">&nbsp;</xsl:text>
163
<a class="ms-addnew" ID="idHomePageNewLink" href="{$URL_New}" ONCLICK="javascript:NewItem('{$URL_New}', true);javascript:return false;" target="_self">添加新链接</a>
164
165
</td>
166
</tr>
167
<tr><td><IMG SRC="/_layouts/images/blank.gif" width="1" height="5" alt="" /></td></tr>
168
</table></xsl:when>
169
<xsl:otherwise></xsl:otherwise>
170
</xsl:choose>
171
</xsl:template>
172
</xsl:stylesheet>
173
174
</Xsl>
175
<DataFields>@ID,ID;@ContentType,内容类型;@Modified,修改时间;@Created,创建时间;@Author,创建者;@Editor,修改者;@_UIVersionString,版本;@Attachments,附件;@Title,标题;@File_x0020_Type,文件类型;@FileLeafRef,名称 (用于表单);@FileDirRef,路径;@FSObjType,项目类型;@_HasCopyDestinations,具有复制目标;@_CopySource,复制源;@ContentTypeId,内容类型 ID;@_ModerationStatus,审批状态;@_UIVersion,UI 版本;@Created_x0020_Date,创建时间;@FileRef,URL 路径;@URL,URL;@Comments,注释;</DataFields>
176
</webpartpages:dataformwebpart>
177
可以看到数据视图主意分成以下几个部分:
DataSources 指定数据源,其中指定的ListID就是一个列表的ID值(AllLists表中的tp_ID字段值)
ParameterBindings 绑定的一些参数,从29-53行使用的是Connection方式,表示是从外部传入到这个webpart的值
54行QueryString(Name)表示接收的是Url参数中的Name值
另外还可以得到一些系统变量的值,比如:
<ParameterBinding Name="UserName" Location="ServerVariable(LOGON_USER)" />
可以取得当前登录用户的名称,可以在需要的地方显示,所有可以使用的系统变量参见msdn:
http://msdn2.microsoft.com/en-us/library/ms524602.aspx
DataFields 部分定义数据源中可以使用的字段
XSL 部分是实际控制页面显示的部分,也是最重要的部分。
显示字段最基本的两种方式:
<xsl:value-of disable-output-escaping="no" select="(@URL, ', ')" />
显示数据源中的字段值
<xsl:value-of select="$PageUrl" />
显示和使用定义的参数或变量的值
其中94行:<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row" /> 表示取得数据源中的所有行
其它的使用方式基本和标准的xsl的使用都一样。
另外数据视图中定义了自己的一个xsl的命名空间:xmlns:ddwrt=http://schemas.microsoft.com/WebParts/v2/DataView/runtime
具体提供了哪些方法可以参看msdn的一篇文章(是基于sharepoint 2003的,可以参考一下):
http://msdn2.microsoft.com/zh-cn/library/aa505323(en-us).aspx
例:使用ddwrt实现权限控制:

判断权限代码
1
<WebPartPages:DataFormWebPart runat="server" IsIncluded="True" FrameType="None" NoDefaultStyle="TRUE" ViewFlag="0" Title="DataView 1" __markuptype="vsattributemarkup" __WebPartId="{8C68F280-8632-4B32-AF48-23C9FE6D69E7}" id="g_8c68f280_8632_4b32_af48_23c9fe6d69e7">
2
<DataSources>
3
4
</DataSources>
5
6
<datafields/>
7
<XSL>
8
<xsl:stylesheet version="1.0" exclude-result-prefixes="rs z o s ddwrt dt msxsl" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:o="urn:schemas-microsoft-com:office" xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" xmlns:ddwrt2="urn:frontpage:internal"><xsl:output method="html" indent="no" /><xsl:decimal-format NaN="" />
9
<xsl:template match="/" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:asp="http://schemas.microsoft.com/ASPNET/20">
10
<xsl:if test="ddwrt:IfHasRights(2)">
11
已经登录
12
</xsl:if>
13
</xsl:template>
14
</xsl:stylesheet>
15
</XSL>
16
</WebPartPages:DataFormWebPart>
主要就是使用了IfHasRights(2)这个方法
方法的说明及其它方法的使用也可以在Sharepoint Designer的XPath表达式生成器中看到:

数据视图可以将列表,比如通知列表、博客列表、链接列表等转换成数据视图,然后可以任意控制显示格式
数据视图可以从多个数据源取数据来显示,这里的数据源可以是子网站中的列表、可以是数据库中的记录、可以是其它网站的RSS源
数据视图可以实现分页、排序、查询、过滤等功能
数据视图可以利用ddwrt:IfHasRights实现权限的判断.....
基本的功能我就不说了,下面列出我在开发中觉得比较有用的、不太注意到的一些功能。
首先给出一个最简单的数据视图的代码:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

可以看到数据视图主意分成以下几个部分:
DataSources 指定数据源,其中指定的ListID就是一个列表的ID值(AllLists表中的tp_ID字段值)
ParameterBindings 绑定的一些参数,从29-53行使用的是Connection方式,表示是从外部传入到这个webpart的值
54行QueryString(Name)表示接收的是Url参数中的Name值
另外还可以得到一些系统变量的值,比如:
<ParameterBinding Name="UserName" Location="ServerVariable(LOGON_USER)" />
可以取得当前登录用户的名称,可以在需要的地方显示,所有可以使用的系统变量参见msdn:
http://msdn2.microsoft.com/en-us/library/ms524602.aspx
DataFields 部分定义数据源中可以使用的字段
XSL 部分是实际控制页面显示的部分,也是最重要的部分。
显示字段最基本的两种方式:
<xsl:value-of disable-output-escaping="no" select="(@URL, ', ')" />
显示数据源中的字段值
<xsl:value-of select="$PageUrl" />
显示和使用定义的参数或变量的值
其中94行:<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row" /> 表示取得数据源中的所有行
其它的使用方式基本和标准的xsl的使用都一样。
另外数据视图中定义了自己的一个xsl的命名空间:xmlns:ddwrt=http://schemas.microsoft.com/WebParts/v2/DataView/runtime
具体提供了哪些方法可以参看msdn的一篇文章(是基于sharepoint 2003的,可以参考一下):
http://msdn2.microsoft.com/zh-cn/library/aa505323(en-us).aspx
例:使用ddwrt实现权限控制:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

主要就是使用了IfHasRights(2)这个方法
方法的说明及其它方法的使用也可以在Sharepoint Designer的XPath表达式生成器中看到: