开发自定义字段类型 sharepoint

网上有好多类似的文章,但是他们写的注释太少了,我研究了一个星期才弄好。

我也写写吧,希望对新接触的朋友有帮助。

首先:写好一个xml文件

<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">PeopleSelector</Field>--类名
<Field Name="ParentType">Text</Field> --字段类型
<Field Name="TypeDisplayName">People Selector</Field>
<Field Name="TypeShortDescription">PictureTag</Field> --显示名称
<Field Name="UserCreatable">TRUE</Field>
<Field Name="FieldTypeClass">
RownSearch(命名空间).PictureSelector(要调用的类名), RownSearch(命名空间), Version=1.0.0.0, Culture=neutral, PublicKeyToken=1a47724c4e1447d0
</Field>
</FieldType>
</FieldTypes>

namespace RownSearch
{
//继承
class PeopleSelector : SPFieldText(必须继承)
{
public PeopleSelector(
SPFieldCollection fields, string fieldName) :
base(fields, fieldName) { }

public PeopleSelector(
SPFieldCollection fields,
string typeName, string displayName) :
base(fields, typeName, displayName) { }

//上面是必写的
//两种方式一种自定义用户控件
public override BaseFieldControl FieldRenderingControl
{
get
{
//转到定义页
BaseFieldControl fieldControl = new PictureSelector_Control();
fieldControl.FieldName = InternalName;
return fieldControl;
}
}
}
}

namespace RownSearch
{
//必须继承BaseFieldControl
class PictureSelector_Control : BaseFieldControl
{
//自定义控件
protected TextBox Picture_TextBoxField;
protected HiddenField Picture_TextBoxFields;
protected Button Picture_Btn;

//创建控件
protected override void CreateChildControls()
{
//在编辑和新增下显示
if (this.Field == null || this.ControlMode == SPControlMode.Display)
{
return;
}
base.CreateChildControls();
//从模版中获取控件

Picture_TextBoxField = TemplateContainer.FindControl("Picture_TextBox") as TextBox;
//获取button
Picture_Btn = TemplateContainer.FindControl("btn_Picture") as Button;
}

//在显示模式下显示模版
//public override string DisplayTemplateName {
// get {
// return "";
// }
//}
//在默认模式下显示模版 //模版SharePoint:RenderingTemplate的id
protected override string DefaultTemplateName
{
get
{
return "Picture_FieldTemplate";//用户控件里SharePoint:RenderingTemplate的ID
}
}
//重写value
public override object Value
{
get
{
this.EnsureChildControls();
if (Picture_TextBoxField != null)
{
return Picture_TextBoxField.Text.ToString(); ;
}
else
{
return null;
}
}
set
{
this.EnsureChildControls();
if (Picture_TextBoxField != null)
{
Picture_TextBoxField.Text = (string)this.ItemFieldValue;
}
}
}
}

用户控件

<SharePoint:RenderingTemplate Id="TagTree_FieldTemplate" runat="server" >
<Template>
<script type="text/javascript">
function OpenToSearchPeople() {
var options = {
url: "/_layouts/showTagTree.aspx",
width: 600,
height: 480,
title: "标签",
dialogReturnValueCallback: TagTreeCloseCallback
};
SP.UI.ModalDialog.showModalDialog(options);
}
function TagTreeCloseCallback(result, value) {
debugger;
if (result == SP.UI.DialogResult.OK) {
var txtID = "ctl00_m_g_5a16be22_8c73_4a3f_a3ee_03fe2a571152_ff121_ctl00_ctl00_People_TextBox";
document.getElementById(txtID).value += value;
}
}
</script>
<asp:TextBox ID="People_TextBox" runat="server"/>

<asp:Button ID="Button1" runat="server" OnClientClick="javascript:OpenToSearchPeople();return false;" Text="标签" />
</Template>
</SharePoint:RenderingTemplate>

用户控件调一个模式化窗体,这个不是所有人都用的到的

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script type="text/javascript">

function btonTree_ClientClick() {
var treeTagBack = document.getElementById("<% =this.TreeTagHidden.ClientID %>").value;

SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, treeTagBack);
}
</script>
<asp:Button ID="BackTreeTag" runat="server" Text="确定" OnClientClick="javascript:btonTree_ClientClick();" />
<asp:TreeView ID="TreeView1" runat="server" ImageSet="Arrows" OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged" ShowCheckBoxes="All">
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
<SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px" VerticalPadding="0px" />
<NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black" HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>

<asp:HiddenField ID="TreeTagHidden" runat="server"/>
</asp:Content>

里面有不懂得请给我留言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值