본문 바로가기
SharePoint 2010

Custom Field Type 만들기

먼저 빈 SharePoint Project를 하나 생성합니다. 

 

[New Item]을 하나 추가하고,

 

[Class]를 선택합니다.

 

추가한 Class에 다음과 같이 원하는 형태로 Field Type을 개발합니다.

    public class VendorFieldType : SPFieldText
    {
        public VendorFieldType(SPFieldCollection fields, string fieldName) : base(fields, fieldName) { }
        public VendorFieldType(SPFieldCollection fields, string typeName, string displayName) : base(fields, typeName, displayName) { }

        public override string DefaultValue
        {
            get
            {
                string defaultValue = string.Empty;
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    try
                    {
                        SPWeb web = SPContext.Current.Web;
                        SPUser user = web.CurrentUser;
                        SPList list = web.Lists["Vendor List"];
                        SPQuery query = new SPQuery();
                        query.Query = "<Where><Eq><FieldRef Name='Contact_x0020_Name'/><Value Type='Text'>" + user.Name + "</Value></Eq></Where>";
                        SPListItemCollection itemC = list.GetItems(query);

                        foreach (SPListItem item in itemC)
                        {
                            defaultValue = string.Format("{0}", item["Vendor"]);
                        }
                    }
                    catch (Exception ex)
                    {
                        EventLog.WriteEntry("VendorFieldType", ex.Message.ToString(), EventLogEntryType.Error);
                    }
                });

                return defaultValue;
            }
            set
            {
                base.DefaultValue = value;
            }
        }

 

        //public override string GetValidatedString(object value)
        //{
            //if (!value.ToString().StartsWith("P"))
            //{
            //    throw new SPFieldValidationException(
            //                "Product code must start with 'P'");
            //}
            //if (value.ToString().Length != 4)
            //{
            //    throw new SPFieldValidationException(
            //                "Product code must be 4 chars");
            //}

            //return value.ToString().ToUpper();
        //}

 

이제, SharePoint에서 표시되고, 배포되어질 Field Type용 xml을 만들어 봅니다.

우선, xml을 업로드 할 [SharePoint Mapped Folder]를 추가합니다.

 

[TEMPLATE] - [XML]을 선택합니다.

 

추가한 위치에다 XML File을 추가합니다.

 

xml에 다음과 같이 입력합니다.

 <?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
  <FieldType>
    <Field Name="TypeName">Get Vendor</Field>
    <Field Name="ParentType">Text</Field>
    <Field Name="TypeDisplayName">Get Vendor</Field>
    <Field Name="TypeShortDescription">Get Vendor</Field>
    <Field Name="UserCreatable">TRUE</Field>
    <Field Name="Sortable">TRUE</Field>
    <Field Name="AllowBaseTypeRendering">TRUE</Field>
    <Field Name="Filterable">TRUE</Field>
    <Field Name="FieldTypeClass">Element.SharePoint.FieldType.VendorFieldType, $SharePoint.Project.AssemblyFullName$</Field>
    <PropertySchema>
      <Fields>
      </Fields>
    </PropertySchema>
  </FieldType>
</FieldTypes>

 

이제 프로젝트를 배포하고 나면, 아래와 같이 필드를 추가해서 사용할 수가 있습니다.