前段时间,由于刚好项目定制的需要,笔者就开发了几个自定义字段类型。在这抽空做个详细笔记,方便初学者学习。这方面的资料也很多,如果自身觉得不大明白可以参考下SDK和网上的相关文章。本章的目的主要是给新手起个抛砖引玉的作用。至于字段类型要实现什么功能,还是以自己的实际开发需求来做引导。接下来就介绍该如何实现。。。。
其实实现一个字段类型的开发很简单,无非就是由3部分组成:1继承于SPField的字段类,2继承于BaseRenderControl的字段呈现空间类,3编写一个配置文件,配置文件必须以fldtypes_开头。(如果是采用模版,那就再编写一个.ascx模版页)。
1.BaseRenderControl的常用属性和方法有【
    Value:此属性自动被系统调用,可以利用此属性跟字段进行交互。
    Field:利用此属性获取跟控件相关联的字段类。
    FieldName:获取或设置字段名。ControlMode:获取空间当前所出的模式。
    List:获取关联的列表。ItemFieldValue:获取对应列表项的值】。
2.SPField的常用属性和方法有【
    ShowInDisplayForm:此属性控制字段是否显示在呈现页面
   ShowInEditForm:此属性控制字段是否显示在编辑页面
   ShowInNewForm:此属性控制字段是否显示在新建页面
   FieldValueType:字段类型值
   FieldRenderingControl:返回字段呈现空间实例
   GetValidatedString:此方法返回通过验证的值,若验证失败则抛出SPFieldValidationException
   GetFieldValue:获得字段的值
   GetFieldValueForEdit:用于显示在编辑的页面上
   OnAdded:字段被添加之后调用
   OnUpdated:字段被更新之后调用
   OnDeleting:字段被删除之后调用】
3.至于配置文件:大家可以直接参考SDK相关资料。。。。。。。。这里就不在详细简述了。
下面直接就把相关的代码文件附上:(该字段类型实现获取当前网站的所有调查列表的名称。至于中间用到的一些方法,大伙如果不明白,可以直接看代码中的注释。新建一个类库项目,两个类文件分别命GetallSurveyListTitleFielControl,GetallSurveyListTitleField,然后引用Microsoft.Sharepoint.dll,项目记得设置强名称。。。。。。。。。。。。。)
GetallSurveyListTitleFielControl.cs


  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using Microsoft.SharePoint;
  5using Microsoft.SharePoint.WebControls;
  6using System.Web;
  7using System.Web.UI.WebControls;
  8using System.IO;
  9namespace GetallSurveyListTitle
 10{
 11    class GetallSurveyListTitleFielControl:BaseFieldControl
 12    {
 13        private DropDownList drp_SurveyListTitle;
 14        private Label lb_SurveyListTitle;
 15        protected override void CreateChildControls()
 16        {
 17             base.CreateChildControls();
 18            //为显示模式下
 19            if (this.ControlMode == SPControlMode.Display)
 20             {
 21
 22                 this.TemplateName = this.DisplayTemplateName;
 23                 //此处的"txt_SurveyListTitle"和"Drp_SurveyListTitle"将在呆会创建的自定义字段模板中用到。
 24                 lb_SurveyListTitle = (Label)TemplateContainer.FindControl("lb_SurveyListTitle");
 25                 if (lb_SurveyListTitle == null)
 26                 {
 27                     throw new SPException("模板中必须存在ID为lb_SurveyListTitle的TextBox控件");
 28                 }
 29                 else
 30                 {  
 31                     lb_SurveyListTitle.Text=""+this.ItemFieldValue;
 32                     return;
 33                 }
 34             }
 35               //编辑或新增模式下
 36             else 
 37             {
 38                 drp_SurveyListTitle = (DropDownList)TemplateContainer.FindControl("Drp_SurveyListTitle");
 39                 if (drp_SurveyListTitle == null)
 40                 {
 41                     throw new SPException("模板中必须存在ID为Drp_SurveyListTitle的DropDownList控件");
 42
 43                 }
 44                
 45             }
 46
 47
 48             if (!Page.IsPostBack)
 49             {
 50                 SPWeb mysite = SPContext.Current.Web;
 51                 SPListCollection ListCollection = mysite.Lists;
 52                 foreach (SPList list in ListCollection)
 53                 {
 54                     if (list.BaseTemplate.ToString() == "Survey")
 55                     {
 56                         string surveytitle = list.Title;
 57                         //绑定到下拉框
 58                         this.drp_SurveyListTitle.Items.Add(new ListItem(surveytitle, surveytitle));
 59
 60                     }
 61
 62                 }
 63             }
 64            
 65            
 66          
 67        }
 68
 69        //在显示模式下使用的模板
 70        public override string DisplayTemplateName
 71        {
 72            get
 73            {   //TempalteID
 74                return "SurveyListTitleDisplay";
 75            }
 76
 77        }
 78
 79        //默认模式下使用的模板
 80
 81        protected override string DefaultTemplateName
 82        {
 83            get
 84            {
 85                return "SurveyListTitleFieldControl";
 86            }
 87        }
 88        //重写Value
 89        public override object Value
 90        {
 91            get
 92            {
 93                this.EnsureChildControls();
 94                if (this.drp_SurveyListTitle != null)
 95                {
 96
 97                    return drp_SurveyListTitle.SelectedValue.ToString();
 98                }
 99                else 
                {
                    return null;
                }
            }
            set
            {
                this.EnsureChildControls();
                if (this.drp_SurveyListTitle != null)
                {                     this.drp_SurveyListTitle.SelectedValue =(string)this.ItemFieldValue;
                }
                
            }
        }
        //重写控件焦点
        public override void Focus()
        {
            this.EnsureChildControls();
            this.drp_SurveyListTitle.Focus();         }
    
    
    
    }
}

GetallSurveyListTitleField.cs


 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using Microsoft.SharePoint;
 5using Microsoft.SharePoint.WebControls;
 6namespace GetallSurveyListTitle
 7{
 8    //直接继承SPFieldChoice,重写BaseFieldControl
 9    public class GetallSurveyListTitleField:SPFieldChoice
    {         public GetallSurveyListTitleField(SPFieldCollection fields, string fieldname)
            : base(fields, fieldname)
        { 
        
        }         public GetallSurveyListTitleField(SPFieldCollection fields, string typename,string displayname)
            : base(fields,typename,displayname)
        {         }
       //返回呈现控件(重写FielRenderingControl)
        public override BaseFieldControl FieldRenderingControl
        {
           
            get
            {
                BaseFieldControl ctl = new GetallSurveyListTitleFielControl();
                ctl.FieldName = this.InternalName;
                return ctl;
            }
        }
    
    }
}

fldtypes_GetallSurveyListTitle.xml


 1<?xml version="1.0" encoding="utf-8"?>
 2<FieldTypes>
 3  <FieldType>
 4    <Field Name="TypeName">GetallSurveyListTitle</Field>
 5    <Field Name="ParentType">Choice</Field>
 6    <Field Name="TypeDisplayName">获取调查列表标题</Field>
 7    <Field Name="TypeShortDescription">获取调查列表标题</Field>
 8    <Field Name="UserCreatable">TRUE</Field>
 9    <Field Name="ShowInListCreate">TRUE</Field>
    <Field Name="ShowInSurveyCreate">TRUE</Field>
    <Field Name="ShowInDocumentLibraryCreate">TRUE</Field>
    <Field Name="ShowInColumnTemplateCreate">TRUE</Field>
    <Field Name="FieldTypeClass">GetallSurveyListTitle.GetallSurveyListTitleField,GetallSurveyListTitle,Version=1.0.0.0, Culture=neutral, PublicKeyToken=d983c60297f46343</Field>
  </FieldType>
</FieldTypes>

GetallSurveyListTitleFielControl.ascx


 1<%@ Control Language="C#"   AutoEventWireup="false" %>
 2<%@Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
 3<%@Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.WebControls"%>
 4<SharePoint:RenderingTemplate ID="SurveyListTitleFieldControl" runat="server">
 5    <Template>
 6    <asp:DropDownList runat="server" ID="Drp_SurveyListTitle" />
 7    </Template>
 8</SharePoint:RenderingTemplate>
 9<SharePoint:RenderingTemplate ID="SurveyListTitleDisplay" runat="server">
    <Template>
    <asp:Label runat="server" ID="lb_SurveyListTitle" />
    </Template>
</SharePoint:RenderingTemplate>

到此一个字段类型开发完毕。
部署:1.把项目生成的.dll文件放入GAC。
         2.把配置文件放入12\Template\XML
         3.把字段模版文件放入12\Template\ControlTemplate
         4.IISRESET

开发MOSS自定义字段类型的更多相关文章

  1. sharepoint2010问卷调查(3)-实现问卷的开始和结束时间(采用自定义字段类型)

    接着上面的图片调查,sharepoint自带的问卷调查是没有开始和结束时间的.这个在项目过程不太实用.问卷一般有开始和结束时间的.因此需要自己 动手开发一个自定义字段类型字段.如下图: 开发添加栏目会 ...

  2. sharepoint2010问卷调查(2)-实现问卷的图片调查(采用自定义字段类型)

    1. 首先建立个图片库上传图片 并建立文件夹1和2,1下有1.1文件夹,2下2.1文件夹,2.1下有文件夹2.1.1. 在1文件夹下放如下图片: 2.建立自定义字段类型,如下图: 3.部署后建立栏目的 ...

  3. SharePoint 2010 自定义 字段 类型--------省市区联动

    转:http://www.cnblogs.com/sp007/p/3384310.html 最近有几个朋友问到了有关自定义字段类型的问题,为了让更多的人了解自定义字段类型的方法,特写一篇博客与大家分享 ...

  4. 转载:SharePoint 2010 自定义 字段 类型--------省市区联动

    最近有几个朋友问到了有关自定义字段类型的问题,为了让更多的人了解自定义字段类型的方法,特写一篇博客与大家分享,首先看一下解决方案目录 创建自定义类型分以下几个步骤: 第一步:添加SharePoint映 ...

  5. [SharePoint 2010] 自定义字段类型开发(二)

    在SharePoint 2010中实现View Action Button效果. http://www.sharepointblogs.be/blogs/vandest/archive/2008/06 ...

  6. MVC开发中自定义返回类型

    在做项目web的MVC中,会用到返回值的问题,我们一般使用AjaxResult的返回值,根据自己的需要进行自定义,如下参考: using System; using System.Collection ...

  7. sharepoint2010问卷调查(4)-实现问卷的重复答复次数(采用自定义字段类型和JS)

    sharepoint的问卷调查可以设置重复和一次答复.但是设置一次后,调查过的用户再进行答复.会提示如下图: 分析下:该提示用户体验很不好.给用户感觉是系统出问题了.因此网上有人提出用eventhan ...

  8. activiti实战系列之动态表单 formService 自定义变量类型

    目前Activiti默认支持的类型有String,long,enum,date,boolean,collection 要自定义字段类型,首先需要表单类型解析类 /** * @Author:LJ * @ ...

  9. [PHPCMS V9二次开发]自定义字段模型-添加字段类型

    步骤/方法 打开phpcms\modules\content\fields目录,复制文件夹downfiles,并改名为textgroups. 打开phpcms\modules\content\fiel ...

随机推荐

  1. tflearn save模型异常

    存储模型始终无法形成单个文件,最初以为是机器中间断电\休眠引起的,重复了3次之后,发现这个问题一直存在.(每一次都要跑8~9个小时啊,摔...) 解决办法:tensorflow的版本回退到0.11版 ...

  2. FreeMarker 语法

    copy自http://demojava.iteye.com/blog/800204 以下内容全部是网上收集: FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主 ...

  3. HTML下直接调用Less文件

    虽然有很多编译Less的插件可以使用 , 但是在开发的时候 , 每修改一次less代码就编译一次less文件 , 很明显效率就太低了 , 接下来为大家介绍一个直接在html的link标签中引入.les ...

  4. java泛型操作复习,以及讲解在android中使用的场景

    android使用泛型的地方很多,比如集成自BaseAdapter实现封装的Adapter,对常用操作进行封装,但是需要对传进来的数据进行处理,此时就使用到泛型,示例如下: public abstra ...

  5. ACdream 1064 完美数

    数位dp. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #incl ...

  6. smarty模板设计

      一.什么是smarty? smarty是一个使用PHP写出来的模板PHP模板引擎,它提供了逻辑与外在内容的分离,简单的讲,目的就是要使用PHP程序员同美工分离,使用的程序员改变程序的逻辑内容不会影 ...

  7. POJ 2977 Box walking

    题目链接:http://poj.org/problem?id=2977 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 222 ...

  8. Sass入门:第三章

    1.声明变量 Sass声明变量以美元符号"$"开头.例如: $width : 300px; 上面的例子中,Sass的变量包括三个部分: (1)声明变量的符号"$" ...

  9. F#中的自定义隐式转换

    我们知道隐式变换在可控情况下会使代码变得简洁.熟悉C#的都知道C#中可以自定义隐式变换,例如 public class A { private int data; public static impl ...

  10. BestCoder Round #86 A B C

    这次BC终于不像上次一样惨烈 终于A了三题…… 终测ing…… 发一波题解…… A.Price List A题十分无脑 只要把所有数加起来存到sum里 询问的时候大于sum输出1 否则输出0就行了…… ...