承接MOSS各种工作流开发 联系人:王先生.电话:18618405729QQ:252385878 QQ群:41696750 MSN:wanghao-3@hotmail.com

导航

MOSS创建自定义字段类型

在MOSS里面,有很多地方都要用到自定义字段类行,比如说,有的用户要做一个工作流的表单,但是有的用户不想用InfoPath,那怎么办。那我们就用MOSS自定义定义一个表单出来
这个时候,我们就可以定制很强大的自定义类型出来,我下面的这个Demo是当然用户要申请一个任务的时候,制动的把当前的用户给读出来,不用用户在来自己输入一次。Demo其实很简单。但是,通过这个列子。可以让我们明白很多的道理,让我们去实现更强大的功能。。
首先要创建3个类。。
//这个是呈现类
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Security;

namespace CustomFieldType
{
   
    [Guid("daf7f358-83fa-41e9-915b-43de0e02ae57")]
    public class FieldControlField : SPFieldMultiColumn
    {
        public FieldControlField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {
        }
       
        public FieldControlField(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        {
        }

        public override BaseFieldControl FieldRenderingControl
        {
            //[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
            get
            {
                BaseFieldControl fieldControl = new FieldControlFieldControl();
                fieldControl.FieldName = this.InternalName;

                return fieldControl;
            }
        }

        public override object GetFieldValue(string value)
        {
            //return base.GetFieldValue(value);
            if (string.IsNullOrEmpty(value))
                return null;
            return new FieldControlFieldValue(value);
        }
    }
}

//大家一看就知道噢,这个就是控件类
using System;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;


namespace CustomFieldType
{
   
    [Guid("9714413c-bf5a-4372-bdf2-d5d2f2599124")]
    public class FieldControlFieldControl : BaseFieldControl
    {
        protected TextBox tbName;
        protected override string DefaultTemplateName
        {
            get
            {
                //return base.DefaultTemplateName;
                return "GetNameFieldControl";
            }
        }

        public override object Value
        {
            get
            {
                //return base.Value;
                EnsureChildControls();
                FieldControlFieldValue fieldValue = new FieldControlFieldValue();
                fieldValue.Name = tbName.Text.Trim().ToString();
                return fieldValue;
            }
            set
            {
               // base.Value = value;
                EnsureChildControls();
                FieldControlFieldValue fv = (FieldControlFieldValue)value;

                tbName.Text = fv.Name;
            }
        }

        public override void Focus()
        {
            //base.Focus();
            EnsureChildControls();
            tbName.Focus();
        }


        public string GetCurrName()
        {
            string UserName = default(System.String);
            SPSite site = new SPSite(@"http://lh-vmpc/personal/test");
            SPWeb web = site.OpenWeb();
            UserName = web.CurrentUser.Name;
            return UserName;

        }

        protected override void CreateChildControls()
        {
            //base.CreateChildControls();
            if (Field == null) return;
            base.CreateChildControls();

            if (ControlMode == SPControlMode.Display)
            {
                return;
            }

             tbName = (TextBox)TemplateContainer.FindControl("tbName");
             tbName.TabIndex = TabIndex;
             tbName.CssClass = CssClass;
             tbName.ToolTip = Field.Title + "Name";

            if (tbName == null)
            {
                throw new ArgumentException("tbName is null");
            }

 

            if (ControlMode == SPControlMode.New)
            {
                tbName.Text = this.GetCurrName();
            }


        }
    }
}


//这个是个初始化 保存值的//
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web;

namespace CustomFieldType
{
    class FieldControlFieldValue:SPFieldMultiColumnValue
    {
        private const int numberOfFields = 1;
        public FieldControlFieldValue() : base(numberOfFields) { }
        public FieldControlFieldValue(string value) : base(value) { }

        public string Name
        {
            get { return this[0]; }
            set { this[0] = value; }
        }
    }
}
这个是一个XML文件
名字一定要以fldtypes开头
<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
  <FieldType>
    <Field Name="TypeName">Name</Field>
    <Field Name="ParentType">FieldChoice</Field>
    <Field Name="TypeDisplayName">Name</Field>
    <Field Name="TypeShortDescription">Name</Field>
    <Field Name="UserCreatable">TRUE</Field>
    <Field Name="ShowOnListCreate">TRUE</Field>
    <Field Name="ShowOnDocumentLibraryCreate">TRUE</Field>
    <Field Name="ShowOnSurveyCreate">TRUE</Field>
    <Field Name="ShowOnColumnTemplateCreate">TRUE</Field>
    <Field Name="FieldTypeClass">CustomFieldType.FieldControlField,CustomFieldType, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5</Field>
    <PropertySchema>
      <Fields>
        <Field Name="DefaultName" DisplayName="Default Name:" MaxLength="50" DisplaySize="30" Type="Text">
          <Default>Name</Default>
        </Field>
      </Fields>
    </PropertySchema>
    <RenderPattern Name="DisplayPattern">
      <Switch>
        <Expr>
          <Column/>
        </Expr>
        <Case Value="">
        </Case>
        <Default>
          <Column SubColumnNumber="0" HTMLEncode="TRUE" />
          <HTML><![CDATA[<BR>]]></HTML>
          <Column SubColumnNumber="1" HTMLEncode="TRUE"/>
          <HTML><![CDATA[&nbsp;----&nbsp;]]></HTML>
          <Column SubColumnNumber="2" HTMLEncode="TRUE"/>
          <HTML><![CDATA[<BR>]]></HTML>
          <Column SubColumnNumber="3" HTMLEncode="TRUE"/>
        </Default>
      </Switch>
    </RenderPattern>
  </FieldType>
</FieldTypes>


最后是一个用户控件的代码,直接COPY就OK拉
<%@ Control Language="C#" Debug="true"%>
<%@ Assembly Name="Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.WebControls"%>

<SharePoint:RenderingTemplate ID="GetNameFieldControl" runat="server">
    <Template>
        <table class="ms-form">
            <tr>
                <td><asp:TextBox ID="tbName" MaxLength="255" size="50" runat="server" /></td>
            </tr>
        </table>
    </Template>
</SharePoint:RenderingTemplate>
最后是部署拉。。部署也很简单 。。
     部署.

1)      程序集加入GAC.

2)      把类型定义文件fldtypes_*.xml拷贝到目录c:\program...\12\TEMPLATE\XML下.

3)      把模板文件用户控件拷贝到目录c:\program...\12\TEMPLATE\CONTROLTEMPLATES下.

4)      重起IIS.

 

posted on 2007-06-22 14:16  A A  阅读(3608)  评论(3编辑  收藏  举报