设为首页 收藏本站
查看: 966|回复: 0

[经验分享] Custom Sharepoint Lookup Field

[复制链接]

尚未签到

发表于 2015-9-25 07:08:58 | 显示全部楼层 |阅读模式
  对sharepoint查阅项字段定制摸索了比较久,感觉微软对sharepoint内部的东西介绍的还是比较模糊。
  以下是新建的查阅项字段,有一些不足希望能一起讨论
  一、新建一个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>
  部署解决方案,测试结果
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-118324-1-1.html 上篇帖子: SharePoint Server 2007 :InfoPath Forms Services初体验 下篇帖子: Sharepoint学习笔记—Ribbon系列-- 7. 在Ribbon中替换指定控件(针对用户自定义Tab)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表