Custom Sharepoint Lookup Field

以下是新建的查阅项字段,有一些不足希望能一起讨论

一、新建一个sharepoint项目


二、接着新建一个cs文件作为字段的主要文件

namespace LookUpProject
{
    public class RCustomField : SPFieldLookup
    {
        public RCustomField(SPFieldCollection fields, string fName)
            : base(fields, fName) { }
        public RCustomField(SPFieldCollection fields,
                      string tName, string dName)
            : base(fields, tName, dName) { }


        public override BaseFieldControl FieldRenderingControl
        {
            get
            {
                BaseFieldControl fieldControl = new RCustomFieldControl();       //在下面的内容中定义
                fieldControl.FieldName = this.InternalName;
                return fieldControl;
            }
        } 
    }
}

cs文件中必须包含2个构造函数(2个参数和3个参数),在此文件中还可以添加一些其他方法,可以参考MSDN对SPFieldLookup的介绍。

FieldRenderingControl指定呈现控件类。

注:建立Web部件是要在Sharepoint映射文件夹中

 


三、查阅项与其他字段类型不同在于查阅项的值和查阅项的编辑器控件,首先讨论编辑器控件

namespace LookUpProject
{
    public class RCustomFieldEditor: UserControl, IFieldEditor {

        public bool DisplayAsNewSection { get { return true; } }

        public void InitializeWithField(SPField field) { }

        public void OnSaveChange(SPField field, bool isNewField) {
            RCustomField _f = field as RCustomField;
            SPSite _site = SPControl.GetContextSite(this.Context);
            SPWeb _web = _site.OpenWeb();
            _f.LookupWebId = _web.ID;
            _f.LookupList = _web.Lists["列表名"].ID.ToString();
        }
    }
}

查阅项的编辑器控件必须继承UserControl,同时要实现IFieldEditor接口(DisplayAsNewSection,InitializeWithField,OnSaveChange)

不同的接口实现不同的事件处理,在上述代码中主要实现了点击保存按钮后进行的操作,代码表示默认取得当前上下文中的Site、Web以及一个List。

由于查阅项是对其他列表或文档库的引用,所以需要在创建时设置目标网站ID和列表ID:

_f.LookupWebId = _web.ID;
_f.LookupList = _web.Lists["列表名"].ID.ToString()

编辑器控件的呈现控件代码如下(.ascx):

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register TagPrefix="wssuc" TagName="InputFormControl" src="~/_controltemplates/InputFormControl.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="InputFormSection" src="~/_controltemplates/InputFormSection.ascx" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="false" CompilationMode="Always" Inherits="LookUpProject.RCustomFieldEditor" %>
<wssuc:InputFormSection runat="server" id="RFilterLookupField" Title="Special Column Settings">
  <template_inputformcontrols>
  <wssuc:InputFormControl runat="server" LabelText="Specify detailed options for the filtered lookup column">
      <Template_Control>                       //这里可以添加自定义的控件
       </Template_Control>
    </wssuc:InputFormControl>
    </template_inputformcontrols>
</wssuc:InputFormSection>

 

四、接下来编写上面引用的显示控件:

namespace LookUpProject
{
    public class RCustomFieldControl : BaseFieldControl
    {

        protected override string DefaultTemplateName                         //绑定呈现控件的页面元素
        {
            get { return "UnitedStatesAddressRenderingTemplate"; }
        }

        protected DropDownList DropDownList1;
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            DropDownList1 = new DropDownList();
            RCustomField f = (RCustomField)base.Field;
            SPSite s = SPControl.GetContextSite(Context);
            SPWeb lookupWeb = s.OpenWeb(f.LookupWebId);                                     
            SPList lookupList = lookupWeb.Lists[new Guid(f.LookupList)];
            SPListItemCollection items = lookupList.Items;
            foreach (SPListItem i in items)
            {
                ListItem _s = new System.Web.UI.WebControls.ListItem(
                     i.Fields[FieldID].GetFieldValueAsText(i[FieldID]), i.ID.ToString());
                DropDownList1.Items.Add(_s);
            }
            this.Controls.Add(DropDownList1);
        }

        public override object Value
        {
            get
            {
                SPFieldLookupValue f = new SPFieldLookupValue(Convert.ToInt32(DropDownList1.SelectedValue),DropDownList1.Text);
                return f;
            }
            set
            {
                this.EnsureChildControls();
                base.Value = value as SPFieldLookupValue;
            }
        }
    }
}

 呈现控件是为了在新建、编辑是呈现的页面,在本文中为了简单明了,只用一个下拉列表显示查阅项内容,在后续的文章中会补充多值查阅项字段。

 CreateChildControls方法用来创建页面控件,根据编辑控件指定的WebID和ListID取得查阅项内容。

 查阅项的Value是 SPFieldLookupValue,由查阅项LookupID和LookupValue组成。

呈现页面代码:

 

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<SharePoint:RenderingTemplate runat="server" ID="UnitedStatesAddressRenderingTemplate" >
  <Template>                       //添加需要的控件
  </Template>
</SharePoint:RenderingTemplate>

五、最后是定义字段的XML

<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
  <FieldType>
    <Field Name="TypeName">RCustomField</Field>                         //类型名称
    <Field Name="ParentType">Lookup</Field>                             //父类型
    <Field Name="TypeDisplayName">RCustom Field</Field>                 //类型显示名称
    <Field Name="TypeShortDescription">RCustom Field</Field>            //类型描述
    <Field Name="UserCreatable">TRUE</Field>                            //可创建类型
    <Field Name="FieldTypeClass">                                       //绑定程序集
      LookUpProject.RCustomField,
      $SharePoint.Project.AssemblyFullName$
    </Field>
    <Field Name="FieldEditorUserControl">/_controltemplates/RCustomFieldEditor.ascx</Field>    //指定编辑器控件
  </FieldType>
</FieldTypes>

部署解决方案,测试结果


原文发布于:博客园Custom Sharepoint Lookup Field,转载请说明

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值