3-2 SharePoint 开发自定义字段
1.新建CustomField类 主要进行关联

新建customfieldctrol 和用户控件 主要用与显示


新建xml进行关联

xml以特殊名称开头

查看解决方案assembly进行绑定


运行

using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ljzCustomField { class CustomField:SPFieldText { public CustomField(SPFieldCollection fields, string fieldName):base(fields, fieldName) { } public CustomField(SPFieldCollection fields, string typeName, string displayName):base(fields, typeName, displayName) { } public override string DefaultValue //设置字段的默认值 { get { return "http://"; } } public override BaseFieldControl FieldRenderingControl //关联字段展示控件 { get { BaseFieldControl fc = new CustomFieldControl(); fc.FieldName = this.InternalName; return fc; } } public override string GetValidatedString(object value)//验证字段是否符合要求 { string StartStr = this.GetCustomProperty("CustomFieldProperty").ToString().ToLower();//获得字段属性 string StartValue = string.Empty; if (value.ToString().Length > StartStr.Length) { StartValue = value.ToString().ToUpper().Substring(0, StartStr.Length).ToLower(); } // this.Required是否必填项的值 if (this.Required == true || value == null || StartStr != StartValue) { throw new SPFieldValidationException("该字段必须以" + StartStr + "开头");//将不符合要求的错误抛出来,以小红字显示在栏的下面 } return base.GetValidatedString(value); } } }
using Microsoft.SharePoint.Mobile.Controls; using Microsoft.SharePoint.WebControls; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ljzCustomField { class CustomFieldControl : BaseFieldControl { public TextBox tbStart; public Image myImage; //重写默认模板 protected override string DefaultTemplateName { get { if (this.ControlMode == SPControlMode.Display) { return this.DisplayTemplateName; } else { return "DefaultCustomFieldControl"; } } } //获取控件的值 public override object Value { get { EnsureChildControls(); if (tbStart != null) { return tbStart.Text; } else { return null; } } set { EnsureChildControls(); if (tbStart != null) { tbStart.Text = (String)value; } } } public override string DisplayTemplateName { get { return "DisplayCustomFieldControl"; } set { base.DisplayTemplateName = value; } } //重写控件生成方法 protected override void CreateChildControls() { base.CreateChildControls(); if (this.Field != null) { this.myImage = (Image)TemplateContainer.FindControl("myImage"); this.tbStart = (TextBox)TemplateContainer.FindControl("tbStart"); } if (this.ControlMode == SPControlMode.Display) { string strHeight = base.Field.GetCustomProperty("Height").ToString(); string strWidth = base.Field.GetCustomProperty("Width").ToString(); if (myImage != null) { myImage.ImageUrl = this.ItemFieldValue.ToString(); myImage.Width = Convert.ToInt32(strWidth); myImage.Height = Convert.ToInt32(strHeight); } } } } }
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %> <%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Control Language="C#" %> <%@ Assembly Name="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.WebControls" %> <SharePoint:RenderingTemplate ID="DefaultCustomFieldControl" runat="server"> <Template> <asp:TextBox ID="tbStart" runat="server" /> </Template> </SharePoint:RenderingTemplate> <SharePoint:RenderingTemplate ID="DisplayCustomFieldControl" runat="server"> <Template> <asp:Image ID="myImage" runat="server" /> </Template> </SharePoint:RenderingTemplate>
<?xml version="1.0" encoding="utf-8" ?> <FieldTypes> <FieldType> <Field Name="TypeName">自定义单行文本</Field> <Field Name="ParentType">Text</Field> <Field Name="TypeDisplayName">必须有特定标识开头的单行文本</Field> <Field Name="TypeShortDescription">自定义单行文本</Field> <Field Name="UserCreatable">TRUE</Field> <Field Name="ShowOnListCreate">TRUE</Field> <Field Name="ShowOnSurveyCreate">TRUE</Field> <Field Name="ShowOnDocumentLibraryCreate">TRUE</Field> <Field Name="ShowOnColumnTemplateCreate">TRUE</Field> <Field Name="FieldTypeClass">ljzCustomField, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c1eee1c344380342</Field> <PropertySchema> <Fields> <Field Name="CustomFieldProperty" DisplayName="设置起始标识" MaxLength="255" Type="Text"></Field> <Field Name="Height" DisplayName="图片高度" MaxLength="255" Type="Text"></Field> <Field Name="Width" DisplayName="图片宽度" MaxLength="255" Type="Text"></Field> </Fields> </PropertySchema> </FieldType> </FieldTypes>


浙公网安备 33010602011771号