上次修改时间: 2010年7月7日
适用范围: SharePoint Foundation 2010
本文内容
ListFieldIterator 控件
示例
示例
若要自定义列表项表单,您可以扩展默认的 Microsoft SharePoint Foundation 服务器控件以定义自己的自定义控件,然后用一个可引用该自定义控件的自定义模板来替代现有默认表单模板。您可以创建一个定义自定义类的类库,将项目的 DLL 复制到全局程序集缓存 (GAC),然后将包含引用该 DLL 的自定义控件模板定义的 .ascx 文件添加到 %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\CONTROLTEMPLATES 下的某个子文件夹中。
ListFieldIterator 控件
Microsoft.SharePoint.WebControls.ListFieldIterator 控件用于枚举要在表单内显示的项字段。可通过在位于 %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATES\CONTROLTEMPLATES 目录下的 DefaultTemplates.ascx 文件中定义的一系列嵌套控件模板,将此控件插入列表项表单中。DocumentLibraryForm 模板可为表单设置工具栏控件和链接的布局,但此模板还插入 DocumentLibraryFormCore 模板,而后者会接着插入 DocumentLibraryFields 控件。DocumentLibraryFields 控件的默认模板是 FileFormFields,它是一个在插入 ListFieldIterator 控件的 DefaultTemplates.ascx 文件中定义的控件。
示例
下面的示例定义一个自定义列表字段迭代器控件,该控件可扩展 ListFieldIterator 类以替代其 IsFieldExcluded 方法。该示例可防止在文档库表单中向具有特定组成员身份的用户显示特定字段,除非这些用户是网站管理员。
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebControls
Namespace CustomOverrideControls
Public Class CustomListFieldIterator
Inherits ListFieldIterator
Protected Overrides Function IsFieldExcluded(field As SPField) As Boolean
Dim site As SPWeb = SPContext.Current.Web
Dim groupId As Integer = site.Groups("ExcludeGroup").ID
If site.IsCurrentUserMemberOfGroup(groupId) AndAlso field.Title = "MySpecialColumn" AndAlso site.CurrentUser.IsSiteAdmin = False Then
Return True
End If
Return MyBase.IsFieldExcluded(field)
End Function 'IsFieldExcluded
End Class 'CustomListFieldIterator
End Namespace 'CustomOverrideControls
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace CustomOverrideControls
{
public class CustomListFieldIterator : ListFieldIterator
{
protected override bool IsFieldExcluded(SPField field)
{
SPWeb site = SPContext.Current.Web;
int groupId = site.Groups["ExcludeGroup"].ID;
if (site.IsCurrentUserMemberOfGroup(groupId) && field.Title == "MySpecialColumn" && site.CurrentUser.IsSiteAdmin == false)
{
return true;
}
return base.IsFieldExcluded(field);
}
}
}
创建自定义控件以扩展列表字段迭代器
在 Microsoft Visual Studio 中,单击"文件",指向"新建",然后单击"项目"。
在"新建项目"对话框中,在"已安装的模板"框中选择所需项目语言,选择"类库"作为项目模板,并键入用于生成项目的名称和位置,然后单击"确定"。
若要添加对 Microsoft.SharePoint 程序集的引用,请在"解决方案资源管理器"中右键单击该项目,并在"添加引用"对话框中的".NET"选项卡上选择"Microsoft SharePoint",然后单击"确定"。
若要在生成该项目时为您的自定义程序集指定一个强名称,请在"解决方案资源管理器"中,右键单击该项目,再单击"签名",选择"为程序集签名",然后为强名称密钥文件指定一个名称。
双击"解决方案资源管理器"中的项目 .cs 或 .vb 文件,并像在上述示例中一样添加代码,以便为用于扩展 SharePoint Foundation 控件的自定义控件定义一个类。
按 Ctrl+Shift+B 生成解决方案。
使用 gacutil.exe(随 Microsoft .NET Framework 2.0 软件开发工具包一起安装的命令行实用程序)将项目 DLL 复制到全局程序集缓存 (GAC)。在命令提示符下键入如下命令:gacutil.exe -if "<Full file system path to DLL>"。
若要实现自定义表单控件,您必须创建一个包含可替代默认模板的控件模板的 .ascx 文件,以便将该自定义控件插入表单页。
示例
下面的示例为表单定义了三个嵌套的自定义模板。第一个模板替代 DocumentLibraryForm 模板以创建对第二个模板 CustomDocumentLibraryFormCore 的引用。第二个模板接着将第三个模板 CustomFileFormFields 指定为 DocumentLibraryFields 控件的定义模板。第三个模板插入一个与上述示例中一样的可在类库中定义的自定义列表字段迭代器。
<SharePoint:RenderingTemplate ID="DocumentLibraryForm" runat="server">
<Template>
<SharePoint:InformationBar runat="server"/>
<wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbltop" RightButtonSeparator=" " runat="server">
<Template_RightButtons>
<SharePoint:SaveButton TabIndex=1 runat="server"/>
<SharePoint:GoBackButton runat="server"/>
</Template_RightButtons>
</wssuc:ToolBar>
<SharePoint:FormToolBar runat="server"/>
<SharePoint:FormComponent TemplateName="CustomDocumentLibraryFormCore" runat="server"/>
</Template>
</SharePoint:RenderingTemplate>
<SharePoint:RenderingTemplate ID="CustomDocumentLibraryFormCore" runat="server">
<Template>
<TABLE class="ms-formtable" style="margin-top: 8px;" border=0 cellpadding=0 id="formTbl" cellspacing=0 width=100%>
<SharePoint:ChangeContentType runat="server"/>
<SharePoint:DocumentLibraryFields TemplateName="CustomFileFormFields" runat="server"/>
<SharePoint:ApprovalStatus runat="server"/>
</TABLE>
<SharePoint:WebPartPageMaintenanceMessage runat="server"/>
<SharePoint:DocumentTransformersInfo runat="server"/>
<table cellpadding=0 cellspacing=0 width=100%><tr><td class="ms-formline">
<IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt="">
</td></tr></table>
<TABLE cellpadding=0 cellspacing=0 width=100% style="padding-top: 7px">
<tr><td width=100%>
<SharePoint:ItemHiddenVersion runat="server"/>
<SharePoint:InitContentType runat="server"/>
<wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbl" RightButtonSeparator=" " runat="server">
<Template_Buttons>
<SharePoint:CreatedModifiedInfo runat="server"/>
</Template_Buttons>
<Template_RightButtons>
<SharePoint:SaveButton runat="server"/>
<SharePoint:GoBackButton runat="server"/>
</Template_RightButtons>
</wssuc:ToolBar>
</td></tr></TABLE>
</Template>
</SharePoint:RenderingTemplate>
<SharePoint:RenderingTemplate ID="CustomFileFormFields" runat="server">
<Template>
<CustomOverrideControls:CustomListFieldIterator runat="server"/>
</Template>
</SharePoint:RenderingTemplate>
为文档库创建自定义控件模板文件
在 SharePoint 项目中创建用户控件,如如何:为 SharePoint 应用程序页或 Web 部件创建用户控件中所述。在部署项目时,Visual Studio 会在包含 .ascx 文件的 %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATES\CONTROLTEMPLATES 目录下创建一个项目子文件夹。
向替代特定默认控件模板的 .ascx 文件中添加模板定义,如上面的示例所示。
重置 Internet Information Services (IIS) 以使更改生效。
若要测试上一个示例,请通过其标题以"z"开头的用户界面创建一个字段,并导航到文档库中项的表单以查看更改。