InfoPath的自定义表单,是轻松的事情,但是因为InfoPath表单的部署带来很多不便,所以在项目中,我们用到的是自定义表单的开发。
首先,我们了解一下sharepoint的表单工作原理
每个列表类型通常都有一组设置列表时设置的显示、编辑和新建表单 ASPX 页(DispForm.aspx、EditForm.aspx 和 NewForm.aspx)。这些 ASPX 页设置到“内容空间”中。换句话说,它们不位于 %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\LAYOUTS 中,且不通过 /_layouts 虚拟目录操作。对于文档库,这些页在列表中称为 forms
Microsoft SharePoint Foundation 2010 使用 Microsoft ASP.NET 显示表单。每个表单逻辑上由一组控件(例如,工具栏控件)组成。一个称为迭代器
通过自定义表单模板,可以修改列表项表单的呈现方式。在列表定义的 Schema.xml 文件中,您可以通过Template 属性为每个列表类型指定用于呈现表单的控件模板的 ID。您必须为每个要在其中实现自定义模板的所有表单指定 Template
常见的表单和字段控件

如何开发自定表单模板:
- 启动 Microsoft Visual Studio 2012
- 在“文件”菜单上,指向“新建”,然后单击“项目”
- 在“项目类型”的“Visual Basic”或“C#”下,选择“空白 SharePoint 项目”
- 键入 UserDefinitionFormTemplateSolution作为项目的名称。单击“确定”。
- 在“SharePoint 自定义向导”中选择“部署为场解决方案”。单击“完成”。
- 在“解决方案资源管理器”中,右键单击“UserDefinitionFormTemplateSolution”项目,依次选择“添加”和“新建项”。
- 在“添加新项”对话框中,选择“列表”模板,并键入 “OrderList” 作为名称。单击“添加”。
- 选中“默认(自定义列表)”选项,然后单击“完成”。
- 接下来,打开已创建的“Schema.xml”文件。
- 在 Fields元素中,添加Field的定义
<Field ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}"
Type="Text"
Name="Title"
DisplayName="名称"
Indexed="TRUE"
Required="FALSE"
AllowDuplicateValues="FALSE"
EnforceUniqueValues="TRUE" />
<Field ID="{F21741F5-0B59-48F1-A4CD-2BA84D26CF34}"
Name="draftPeople"
StaticName="draftPeople"
Required="FALSE"
Type="Text"
DisplayName="拟稿人">
</Field>- 在ContenTypes的元素中,删除已有的ContentType的应用,添加新的ContentType的定义
<ContentType ID="0x0100af3f58d8ef214b2ebcad2089e721da9a" Name="测试">
<FieldRefs>
<FieldRef ID="fa564e0f-0c70-4ab9-b863-0177e6ddd247" Name="Title" />
<FieldRef ID="F21741F5-0B59-48F1-A4CD-2BA84D26CF34" Name="draftPeople" />
<FieldRef ID="F70053CF-3FBF-4F1E-B5D6-C9742D1EF005" Name="Urgent" />
<FieldRef ID="F1817829-6C7E-41E9-ACAD-8BB8AD28EEE1" Name="Category" />
<FieldRef ID="4E974D9A-DEE4-44BE-B035-BF705E79F1C1" Name="PUnit" />
<FieldRef ID="13464616-322F-47DA-AC6D-30992AFA66DC" Name="Documents" />
<FieldRef ID="FB4F8F35-88F0-404D-AA61-D20F0CA51FE4" Name="SecurityClassification" />
<FieldRef ID="B4D5CDE9-D403-41E3-940A-EE6CA63A2188" Name="MainUnits" />
<FieldRef ID="B18E9ACC-DD65-49C5-ABD3-9FCC518237F1" Name="FilesNumber" />
<FieldRef ID="EBCD850E-293D-49FA-88A3-B363FF28520C" Name="FileGUID" />
</FieldRefs
</ContentType>- 在“解决方案资源管理器”中,右键单击“UserDefinitionFormTemplateSolution”项目,依次选择“添加”和“新建项”
- 在“添加新项”对话框中,选择”用户控件”模版,键入” UserTemplateDefinition”作为用户控件名称,点击确定。
- 将UserTemplateDefinition 移动至ControlTemplates的目录下,删除UserDefinitionFormTemplateSolution的文件夹。
- 删除UserTemplateDefinition.cs的文件,只保留ascx文件。
- 打开%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\TEMPLATE\CONTROLTEMPLATES下的DefaultTemplates.ascx的文件。
- 在DefaultTemplates.ascx的文件中,复制如下行
<%@ Control Language="C#" AutoEventWireup="false" %>
<%@Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.WebControls"%>
<%@Register TagPrefix="ApplicationPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.ApplicationPages.WebControls"%>
<%@Register TagPrefix="SPHttpUtility" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.Utilities"%>
<%@ Register TagPrefix="wssuc" TagName="ToolBar" src="~/_controltemplates/ToolBar.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="ToolBarButton" src="~/_controltemplates/ToolBarButton.ascx" %>- 打开UserTemplateDefinition.ascx文件,将复制内容替换UserTemplateDefinition.ascx中所有内容
- 在DefaultTemplates.ascx的文件中,找到ID为ListForm的SharePoint:RenderingTemplate元素,复制SharePoint:RenderingTemplate元素的内容,粘贴到UserTemplateDefinition.ascx文件中
<SharePoint:RenderingTemplate id="ListForm" runat="server">
<Template>
<span id='part1'>
<SharePoint:InformationBar runat="server"/>
<div id="listFormToolBarTop">
<wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbltop" RightButtonSeparator=" " runat="server">
<Template_RightButtons>
<SharePoint:NextPageButton runat="server"/>
<SharePoint:SaveButton runat="server"/>
<SharePoint:GoBackButton runat="server"/>
</Template_RightButtons>
</wssuc:ToolBar>
</div>
<SharePoint:FormToolBar runat="server"/>
<SharePoint:ItemValidationFailedMessage runat="server"/>
<table class="ms-formtable" style="margin-top: 8px;" border="0" cellpadding="0" cellspacing="0" width="100%">
<SharePoint:ChangeContentType runat="server"/>
<SharePoint:FolderFormFields runat="server"/>
<SharePoint:ListFieldIterator runat="server"/>
<SharePoint:ApprovalStatus runat="server"/>
<SharePoint:FormComponent TemplateName="AttachmentRows" runat="server"/>
</table>
<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:ParentInformationField 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>
</span>
<SharePoint:AttachmentUpload runat="server"/>
</Template>
</SharePoint:RenderingTemplate>- 在UserTemplateDefinition.ascx的文件中,把SharePoint:RenderingTemplate中ID修改为UserDifinitionDisplayForm
- 在UserTemplateDefinition.ascx的文件中,重新复制一个SharePoint:RenderingTemplate的元素,将ID修改为 UserDifinitionEditForm
- 打开列表定义中的schema.xml文件,找到Forms的元素,在子元素Type=Display的form元素中,添加Template="UserDifinitionDisplayForm"
- 在子元素Type= EditForm ,和Type= NewForm的form元素中,添加Template=" UserDifinitionEditForm"
<Forms>
<Form Type="DisplayForm" Url="DispForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" Template="UserDifinitionDisplayForm" UseLegacyForm="true" />
<Form Type="EditForm" Url="EditForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" Template="UserDifinitionEditForm" />
<Form Type="NewForm" Url="NewForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
</Forms>- 在“解决方案资源管理器”中,右键单击“UserDefinitionFormTemplateSolution”项目,依次选择“添加”和“新建项”。
- 在“添加新项”对话框中,选择”用户控件”模版,键入” ListDisplayFormControl”作为用户控件名称,点击确定。
- 重复上述步骤,添加名为” ListEditFormControl”的用户控件。
- 在ListDisplayFormControl和ListEditFormControl用户控件中,可以自行添加自己的样式和布局
- 用<SharePoint:FieldLabel> 控件来显示字段的显示名称,将字段的定义名称绑定到控件的FieldName属性
- 用<SharePoint:FormField> 控件来显示字段的值,将字段的定义名称帮顶到控件的FieldName的属性
<table id="main">
<caption>
拟印发文</caption>
<tr>
<th id="divline" colspan="4"></th>
</tr>
<tr>
<th class="red">
<SharePoint:FieldLabel ID="lblTitle" runat="server" FieldName="Title" />
</th>
<td>
<SharePoint:FormField ID="txtTitle" runat="server" FieldName="Title" />
</td><th> <SharePoint:FieldLabel ID="lbDraftPeople" runat="server" FieldName="draftPeople" /> </th> <td> <SharePoint:FormField ID="txtDraftPeople" runat="server" FieldName="draftPeople" /> </td>
</tr>
</table>在这里我们就克自助定义表单的显示。
- 打开UserTemplateDefinition.ascx文件,在该用户控件中,注册ListEditFormControl和ListEditFormControl
<%@ Register TagPrefix="uc" TagName="Edit" Src="~/_controltemplates/15/FormControl/ListEditFormControl.ascx" %>
<%@ Register TagPrefix="uc" TagName="Display" Src="~/_controltemplates/15/FormControl/ListDisplayFormControl.ascx" %>- 在ID为UserDifinitionDisplayForm的SharePoint:RenderingTemplate元素中找到<SharePoint:ListFieldIterator>控件,将该控件删除,添加自定义用户控件<uc:Display runat="server" />
<%--<SharePoint:ListFieldIterator ID="ListFieldIterator1" runat="server" />--%>
<uc:Display runat="server" />- 在ID为UserDifinitionEditForm的SharePoint:RenderingTemplate元素中找到<SharePoint:ListFieldIterator>控件,将该控件删除,添加自定义用户控件<uc:Edit runat="server" />
<%--<SharePoint:ListFieldIterator ID="ListFieldIterator2" runat="server" />--%>
<uc:Edit runat="server" />
















