Sharepoint SP1创建自定义字段下面有一个Bug,我不知道SP2解决了这个问题没有:

通过调用自定义字段类型父类的SetCustomProperty(string propertyName,object propertyValue)来修改属性却不能保存,因此通常的做法是创建一个静态Field来缓存这些设置,利用WSPBuilder来创建的自定义字段在模版中重写SetCustomProperty和GetCustomProperty方法,而不用手工完成,只需调用this.SetCustomProperty或者this.GetCustomProperty就可以了,不能调用base.SetCustomProperty和base.GetCustomProperty方法。

字段定义文件如下:

代码

1 <?xml version="1.0" encoding="utf-8" ?>
2  <FieldTypes>
3 <FieldType>
4 <Field Name="TypeName">RegularField</Field>
5 <Field Name="ParentType">Text</Field>
6 <Field Name="TypeDisplayName">A Regular Field</Field>
7 <Field Name="TypeShortDescription">A field type for regular expression</Field>
8 <Field Name="UserCreatable">TRUE</Field>
9 <Field Name="Sortable">TRUE</Field>
10 <Field Name="AllowBaseTypeRendering">TRUE</Field>
11 <Field Name="Filterable">TRUE</Field>
12 <Field Name="FieldTypeClass">SingTel.SharePoint.CustomFields.RegularField,
        SingTel.SharePoint.CustomFields, Version=1.0.0.0, Culture=neutral,
        PublicKeyToken=fc0c17e3617723dd</Field>
13 <PropertySchema>
14 <Fields>
15 <Field Hidden="FALSE" Name="RegularExpression" DisplayName="Regular Expression" Type="Text" Required="TRUE">
            </Field>
16 <Field Hidden="FALSE" Name="ErrorMessage" DisplayName="Error Message" Type="Text" Required="TRUE">
            </Field>
17 </Fields>
18 <Fields></Fields>
19 </PropertySchema>
20 <RenderPattern Name="DisplayPattern">
21 <HTML><![CDATA[<font style="color:red;">]]></HTML>
22 <Column/>
23 <HTML><![CDATA[</font>]]></HTML>
24 </RenderPattern>
25 </FieldType>
26  </FieldTypes>

字段类型代码:

代码

1 using System;
2  using System.Collections.Generic;
3  using System.Text.RegularExpressions;
4  using Microsoft.SharePoint;
5  using Microsoft.SharePoint.WebControls;
6
7  namespace SingTel.SharePoint.CustomFields
8 {
9 public class RegularField : SPFieldText
10 {
11 private static string[] CustomPropertyNames = new string[] { "RegularExpression", "ErrorMessage" };
12 public RegularField(SPFieldCollection fields, string fieldName)
13 : base(fields, fieldName)
14 {
15 InitProperties();
16 }
17 public RegularField(SPFieldCollection fields, string typeName, string displayName)
18 : base(fields, typeName, displayName)
19 {
20 InitProperties();
21 }
22 #region Property storage and bug workarounds - do not edit
23 /// <summary>
24 /// Indicates that the field is being created rather than edited. This is necessary to
25 /// work around some bugs in field creation.
26 /// </summary>
27   public bool IsNew
28 {
29 get { return _IsNew; }
30 set { _IsNew = value; }
31 }
32 private bool _IsNew = false;
33 /// <summary>
34 /// Backing fields for custom properties. Using a dictionary to make it easier to abstract
35 /// details of working around SharePoint bugs.
36 /// </summary>
37   private Dictionary<string, string> CustomProperties = new Dictionary<string, string>();
38 /// <summary>
39 /// Static store to transfer custom properties between instances. This is needed to allow
40 /// correct saving of custom properties when a field is created - the custom property
41 /// implementation is not used by any out of box SharePoint features so is really buggy.
42 /// </summary>
43 private static Dictionary<string, string> CustomPropertiesForNewFields = new Dictionary<string, string>();
44 /// <summary>
45 /// Initialise backing fields from base property store
46 /// </summary>
47 private void InitProperties()
48 {
49 foreach (string propertyName in CustomPropertyNames)
50 {
51 CustomProperties[propertyName] = base.GetCustomProperty(propertyName) + "";
52 }
53 }
54 /// <summary>
55 /// Take properties from either the backing fields or the static store and
56 /// put them in the base property store
57 /// </summary>
58 private void SaveProperties()
59 {
60 foreach (string propertyName in CustomPropertyNames)
61 {
62 base.SetCustomProperty(propertyName, GetCustomProperty(propertyName));
63 }
64 }
65 /// <summary>
66 /// Get an identifier for the field being added/edited that will be unique even if
67 /// another user is editing a property of the same name.
68 /// </summary>
69 /// <param name="propertyName"></param>
70 /// <returns></returns>
71 private string GetCacheKey(string propertyName)
72 {
73 return SPContext.Current.GetHashCode() + "_" + (ParentList == null ? "SITE" : ParentList.ID.ToString()) + "_" + propertyName;
74 }
75 /// <summary>
76 /// Replace the buggy base implementation of SetCustomProperty
77 /// </summary>
78 /// <param name="propertyName"></param>
79 /// <param name="propertyValue"></param>
80 new public void SetCustomProperty(string propertyName, object propertyValue)
81 {
82 if (IsNew)
83 {
84 // field is being added - need to put property in cache
85 CustomPropertiesForNewFields[GetCacheKey(propertyName)] = propertyValue + "";
86 }
87 CustomProperties[propertyName] = propertyValue + "";
88 }
89 /// <summary>
90 /// Replace the buggy base implementation of GetCustomProperty
91 /// </summary>
92 /// <param name="propertyName"></param>
93 /// <param name="propertyValue"></param>
94 new public object GetCustomProperty(string propertyName)
95 {
96 if (!IsNew && CustomPropertiesForNewFields.ContainsKey(GetCacheKey(propertyName)))
97 {
98 string s = CustomPropertiesForNewFields[GetCacheKey(propertyName)];
99 CustomPropertiesForNewFields.Remove(GetCacheKey(propertyName));
100 CustomProperties[propertyName] = s;
101 return s;
102 }
103 else
104 {
105 return CustomProperties[propertyName];
106 }
107 }
108 /// <summary>
109 /// Called when a field is created. Without this, update is not called and custom properties
110 /// are not saved.
111 /// </summary>
112 /// <param name="op"></param>
113 public override void OnAdded(SPAddFieldOptions op)
114 {
115 base.OnAdded(op);
116 Update();
117 }
118 #endregion
119 public override BaseFieldControl FieldRenderingControl
120 {
121 get
122 {
123 BaseFieldControl fieldControl = new RegularFieldControl();
124
125 fieldControl.FieldName = InternalName;
126
127 return fieldControl;
128 }
129 }
130 public override void Update()
131 {
132 SaveProperties();
133 base.Update();
134 }
135 public string RegularExpression
136 {
137 get { return this.GetCustomProperty("RegularExpression") + ""; }
138 set { this.SetCustomProperty("RegularExpression", value); }
139 }
140 public String ErrorMessage
141 {
142 get
143 {
144 return this.GetCustomProperty("ErrorMessage") + "";
145 }
146 set
147 {
148 this.SetCustomProperty("ErrorMessage", value);
149 }
150 }
151 public override string GetValidatedString(object value)
152 {
153 if(value==null || String.IsNullOrEmpty(value.ToString())) throw new SPFieldValidationException("Value is null or empty");
154 if(!Regex.IsMatch(value.ToString(),RegularExpression)) throw new SPFieldValidationException(ErrorMessage);
155 return base.GetValidatedString(value);
156 }
157 }
158 }

UI控件如下:

代码

1 using System.Web.UI;
2 using System.Web.UI.WebControls;
3 using Microsoft.SharePoint.WebControls;
4
5 namespace SingTel.SharePoint.CustomFields
6 {
7 public class RegularFieldControl : BaseFieldControl
8 {
9 private TextBox textBox;
10 public RegularFieldControl() { }
11
12 protected override void CreateChildControls()
13 {
14 base.CreateChildControls();
15 if (this.ControlMode == SPControlMode.Display)
16 {
17 this.Controls.Add(new LiteralControl("" + this.Value));
18 }
19 else
20 {
21 textBox = new TextBox();
22 this.Controls.Add(textBox);
23 }
24 }
25 public override object Value
26 {
27 get
28 {
29 EnsureChildControls();
30 if (this.textBox != null)
31 return this.textBox.Text;
32 else
33 return null;
34 }
35 set
36 {
37 EnsureChildControls();
38 if (this.textBox != null)
39 {
40 this.textBox.Text = "" + value;
41 }
42 }
43 }
44 }
45 }

补充:上面字段类型代码中有点错误,应该在该字段必须填写的情况下才执行验证,改正如下:

代码

1 public override string GetValidatedString(object value)
2 {
3 if (base.Required)
4 {
5 if (value == null || String.IsNullOrEmpty(value.ToString())) throw new SPFieldValidationException("Value is null or empty");
6 if (!Regex.IsMatch(value.ToString(), RegularExpression)) throw new SPFieldValidationException(ErrorMessage);
7 }
8 return base.GetValidatedString(value);
9 }

转载于:https://www.cnblogs.com/cdutedu/archive/2010/05/18/1738394.html

Sharepoint SP1下创建自定义字段应注意的问题相关推荐

  1. php sku添加,php – 在单个产品页面中显示SKU下的自定义字段值

    我正在定制WooCommerce,我想在产品页面中添加和显示自定义文本(条件和品牌). 该头寸位于"库存"或"SKU"元下.我已设法创建并保存自定义字段,但如何 ...

  2. SAP不同的产品是如何支持用户创建自定义字段的

    我们从SAP CRM,Cloud for Customer(简称C4C)和S/4HANA这三个产品分别来看看. SAP CRM 我们使用所谓的Application Enhancement Tool( ...

  3. ArcGIS Engine环境下创建自定义的ArcToolbox Geoprocessing工具

    在上一篇日志中介绍了自己通过几何的方法合并断开的线要素的ArcGIS插件式的应用程序.但是后来考虑到插件式的程序的配置和使用比较繁琐,也没有比较好的错误处理机制,于是我就把之前的程序封装成一个类似于A ...

  4. 在linux下创建自定义service服务

    三个部分 这个脚本分为3个部分:[Unit] [Service] [Install]. Unit Unit表明该服务的描述,类型描述.我们称之为一个单元.比较典型的情况是单元A要求在单元B启动之后再启 ...

  5. Windows下创建自定义服务的正确姿势(InstrsrvSrvany)

    总览 Windows NT工具包(Windows NT Resource Kit)提供了两个小工具,可以让我们创建自定义服务(适合于NT应用和一些16进制应用,批处理除外).两个工具包的下载地址:CS ...

  6. Struts2下创建自定义类型转换器(表单中日期的处理)

    在表单提交中需要有日期的输入,默认的Struts2处理机制可能不能满足需求,需要自定义一下类型转换器.如: String----->java.util.Date:输入 java.util.Dat ...

  7. php sku 代码编写,php – 在单个产品页面中显示SKU下的自定义字段值

    您的问题中提供的代码不完整,应该是这样的: // Enabling and Displaying Fields in backend add_action( 'woocommerce_product_ ...

  8. Excel数据透视表经典教程八《创建单页/自定义字段透视表》

    前言: 一.单页字段透视表:当对多个表格进行数据透视分析时,不能直接点击插入数据透视表操作.因此,需要创建单页或者自定义字段透视表. 二.自定义字段透视表:由于上述创建的单页字段透视表,对于不同的表格 ...

  9. sharepoint2010 创建自定义列表

    sharepoint2010 创建自定义列表 分类: sharepoint20102014-04-04 14:06 106人阅读 评论(0) 收藏 举报 转:http://boke.25k5.com/ ...

最新文章

  1. GPT-2大战GPT-3:OpenAI内部的一场终极对决
  2. 业务代码解构利器--SWAK
  3. Python基础知识(第五天)
  4. ORACLE EXPDP命令使用详细【转】
  5. 从位图数据取得位图句柄
  6. IT管理人才必备的十大能力(转)
  7. 看 B 站,可以更快!
  8. 7-1 堆栈操作合法性 (15 分)
  9. 【测试工具】Selenium 自动化浏览器(Python 篇)
  10. 【北通游戏手柄安装驱动(WIN10)】
  11. 创意爆破效果PS动作
  12. idea插件开发--组件--编程久坐提醒
  13. Error opening dll library错误的解决
  14. 华为鸿蒙系统卡片,18个月不卡?这四款华为2年还流畅,支持鸿蒙OS
  15. RCV 接收指令例程
  16. C++实验3-税收计算
  17. 东师19年春计算机在线作业,东师算法分析与设计20春在线作业1【标准答案】
  18. .[转] 全国主体功能区规划图
  19. 电脑无法连接wifi得解决方法
  20. CMD/DOS学习笔记

热门文章

  1. 大数据对企业竞争的作用
  2. 大数据平台的搭建和数据分析
  3. 如何破解物联网卡带来的连接痛点
  4. Python实战技术 - Python虚拟隔离环境 和 Docker技术
  5. mysql表的一列拆分成两列_将float值拆分成MySQL表的两列?
  6. L2-007 家庭房产(并查集)
  7. 依存可视化︱Dependency Viewer——南京大学自然语言处理研究组
  8. [tensorflow]tensorflow 2.1 函数API(The Functional API)
  9. 加速网站速度的最佳做法_(2)把样式表放在顶部
  10. 运维自动化-ansible