SharePoint 2013 图文开发系列之自定义字段
转载自:http://www.cnblogs.com/jianyus/p/3462626.html 谢谢
SharePoint使用的优势,就在于开箱即用、快速搭建,SharePoint自身为我们提供了很多字段类型,已经很丰富了。但是,在实际应用中,我们还需要一些功能特殊的字段,下面,我们简单介绍下字段的开发,大家了解以后,可以按照需求扩展自己的字段类型。
1、新建项目,选择SharePoint 2013 空项目,如下图:
2、选择调试网站和解决方案类型,如下图:
3、添加新项,类,这个是用来定义字段的,如下图:
4、添加新项,类,这个是用来编写字段展示的,如下图:
5、添加映射文件夹,如下图:
6、选择映射文件夹,这个文件夹,添加的是CustomFieldControl.cs的前台文件,如下图:
7、添加映射文件夹,选择Xml,这个是字段的描述文件,如下图:
8、为xml目录下添加一个xml文件,用来写字段的描述文件,如下图:
9、在CONTROLTEMPLATES文件夹下,添加用户控件,用来写CustomFieldControl.cs的前台文件,因为这样,比较好进行字段展示,如下图:
10、删除没用的cs文件,最后的如下图
11、为字段类CustomField.cs添加方法,如下图:
12、字段类CustomField.cs完整代码,有点长,关键代码有注释,如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Web; 7 using System.Web.UI; 8 using System.Web.UI.WebControls; 9 using Microsoft.SharePoint; 10 using Microsoft.SharePoint.WebControls; 11 12 namespace SP2013CustomField 13 { 14 class CustomField : SPFieldText 15 { 16 public CustomField(SPFieldCollection fields, string fieldName) 17 : base(fields, fieldName) 18 { 19 } 20 21 public CustomField(SPFieldCollection fields, string typeName, string displayName) 22 : base(fields, typeName, displayName) 23 { 24 } 25 26 public override string DefaultValue //设置字段的默认值 27 { 28 get 29 { 30 return "http://"; 31 } 32 } 33 34 public override BaseFieldControl FieldRenderingControl //关联字段展示控件 35 { 36 get 37 { 38 BaseFieldControl fc = new CustomFieldControl(); 39 fc.FieldName = this.InternalName; 40 return fc; 41 } 42 } 43 44 public override string GetValidatedString(object value)//验证字段是否符合要求 45 { 46 string StartStr = this.GetCustomProperty("CustomFieldProperty").ToString().ToLower();//获得字段属性 47 string StartValue = string.Empty; 48 if (value.ToString().Length > StartStr.Length) 49 { 50 StartValue = value.ToString().ToUpper().Substring(0, StartStr.Length).ToLower(); 51 } 52 // this.Required是否必填项的值 53 if (this.Required == true || value == null || StartStr != StartValue) 54 { 55 throw new SPFieldValidationException("该字段必须以" + StartStr + "开头");//将不符合要求的错误抛出来,以小红字显示在栏的下面 56 } 57 return base.GetValidatedString(value); 58 } 59 } 60 }
13、为字段展示控件类CustomFieldControl.cs添加方法,如下图:
14、附CustomFieldControl.cs完整代码,如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Web; 7 using System.Web.UI; 8 using System.Web.UI.WebControls; 9 using Microsoft.SharePoint; 10 using Microsoft.SharePoint.WebControls; 11 12 namespace SP2013CustomField 13 { 14 class CustomFieldControl : BaseFieldControl 15 { 16 public TextBox tbStart; 17 public Image myImage; 18 19 //获取控件的值 20 public override object Value 21 { 22 get 23 { 24 EnsureChildControls(); 25 if (tbStart != null) 26 { 27 return tbStart.Text; 28 } 29 else 30 { 31 return null; 32 } 33 } 34 set 35 { 36 EnsureChildControls(); 37 if (tbStart != null) 38 { 39 tbStart.Text = (String)value; 40 } 41 } 42 } 43 44 //重写默认模板 45 protected override string DefaultTemplateName 46 { 47 get 48 { 49 if (this.ControlMode == SPControlMode.Display) 50 { 51 return this.DisplayTemplateName; 52 } 53 else 54 { 55 return "DefaultCustomFieldControl"; 56 } 57 } 58 } 59 60 public override string DisplayTemplateName 61 { 62 get 63 { 64 return "DisplayCustomFieldControl"; 65 } 66 set 67 { 68 base.DisplayTemplateName = value; 69 } 70 } 71 72 //重写控件生成方法 73 protected override void CreateChildControls() 74 { 75 base.CreateChildControls(); 76 if (this.Field != null) 77 { 78 this.myImage = (Image)TemplateContainer.FindControl("myImage"); 79 this.tbStart = (TextBox)TemplateContainer.FindControl("tbStart"); 80 } 81 if (this.ControlMode == SPControlMode.Display) 82 { 83 string strHeight = base.Field.GetCustomProperty("Height").ToString(); 84 string strWidth = base.Field.GetCustomProperty("Width").ToString(); 85 if (myImage != null) 86 { 87 myImage.ImageUrl = this.ItemFieldValue.ToString(); 88 myImage.Width = Convert.ToInt32(strWidth); 89 myImage.Height = Convert.ToInt32(strHeight); 90 } 91 } 92 } 93 } 94 }