zoukankan      html  css  js  c++  java
  • Sharepoint 自定义字段

    SharePoint已经为一般用户和开发者提供了非常好的可重用的能力。更进一步,你可以定义一个可重用的栏定义,这将为我们带来更大的灵活性。

    下面是创建一个自定义字段类型的总体步骤:

    1.新建SharePoint工程,并添加SharePoint映射文件夹Xml,在Xml的文件名必须以fldtypes_开头,否则不能识别。

    2. 添加自定义字段类(RoyCustomField),代码主要重载 protected override void CreateChildControls(),

    如果要设置默认值请Override DefaultValue,如果要验证格式是否正确 请Override GetValidatedString

    如下:

    复制代码
    namespace RoyCustomField
    {
        public class RoyCustomField : SPFieldText
        {
            protected TextBox txtField;
            public RoyCustomField(SPFieldCollection field, string strFieldName)
                : base(field, strFieldName)
            {
            }
            public RoyCustomField(SPFieldCollection field, string strFieldName, string strDispName)
                : base(field, strFieldName, strDispName)
            {
            }
    
            public override string DefaultValue
            {
                get
                {
    
                    return "Roy";
                }
            }
         
            public override BaseFieldControl FieldRenderingControl
            {
                get
                {
                    BaseFieldControl fc = new RoyText();
                    fc.FieldName = this.InternalName;
                    return fc;
                }
            }
    
            public override string GetValidatedString(object value)
            {
                if (!value.ToString().ToUpper().StartsWith("Roy"))
                {
                    throw new SPFieldValidationException("内容必须以Roy开头!");
                }
    
                return value.ToString();
            }
        }
    
    
        public class RoyText : BaseFieldControl
        {
    
            protected TextBox txtRoy;
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
              this.TemplateContainer.FindControl("txtRoy");
                txtRoy = new TextBox();
                this.Controls.Add(txtRoy);
            }
           
            public override object Value
            {
                get
                {
                    this.EnsureChildControls();
                    return txtRoy.Text;
                }
                set
                {
                    this.EnsureChildControls();
                    txtRoy.Text=(string)this.ItemFieldValue;
                }
            }
    
            protected override string DefaultTemplateName
            {
                get
                {
                    return "RoyTextRenderingTemplate";
                }
            }
        }
    }
    复制代码

    3.定义XML

    复制代码
    <?xml version="1.0" encoding="utf-8" ?>
    <FieldTypes>
      <FieldType>
        <Field Name="TypeName">RoyCustomField</Field>
        <Field Name="ParentType">Text</Field>
        <Field Name="TypeDisplayName">RoyCustomField</Field>
        <Field Name="TypeShortDescription">RoyCustomField</Field>
        <Field Name="UserCreatable">TRUE</Field>
        <Field Name="FieldTypeClass">RoyCustomField.RoyCustomField,$SharePoint.Project.AssemblyFullName$</Field>
      </FieldType>
    </FieldTypes>
    复制代码

    4.打包,部署,效果图如下:

  • 相关阅读:
    ASP.NET MVC5写.php路由匹配时的问题 ASP.NET MVC 4 在 .NET 4.0 与.NET 4.5 的專案範本差異
    asp.net mvc上传头像加剪裁功能介绍
    图片延迟加载实现
    c#中多线程访问winform控件的若干问题
    C# WinForm实现控件拖动实例介绍
    C# 实现对窗体(Form)换肤
    C#读写txt文件的两种方法介绍
    C#实现JSON序列化与反序列化介绍
    高效的VS调试技巧
    SQL 添加字段和默认值脚本
  • 原文地址:https://www.cnblogs.com/xw2cc1314/p/2966395.html
Copyright © 2011-2022 走看看