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.打包,部署,效果图如下:

  • 相关阅读:
    蓝桥杯算法训练 区间k大数查询
    【模板】快读
    [ACM] hdu 2544 最短路(dijkstra算法)
    [ACM] hdu 3791 二叉搜索树
    [ACM] hdu 2141 Can you find it? (二分查找)
    [ACM] hdu 2025查找最大元素(水题)
    [ACM] hdu 1232 畅通工程(并查集)
    [ACM] hdu 1022 Train Problem I(栈的使用)
    [ACM] hdu 2857 Mirror and Light (对称点+两条直线的交点)
    [ACM] hdu 爆头(点到直线距离)
  • 原文地址:https://www.cnblogs.com/xw2cc1314/p/2966395.html
Copyright © 2011-2022 走看看