客户今天提出了一个需求,定义一个list,在这个list中有一列EventID,follow下面的规则:
Auto created; Format equals YYYYMMDDXX where XX equals sequential number for the day.
实现方向一:通过Out-of-box的方式,仅仅使用配置或者Design工具实现,经过一天的尝试以失败告终。
实现方向二:通过Code方式处理,最终成功。
步骤如下:
1、使用Vs Create一个Empty Sharepoint Project.
2、在项目中添加自定义控件customfieldcontrol.ascx 并且定义此Field显示界面。
<%@ Control Language="C#" %> <%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.WebControls" %> <SharePoint:RenderingTemplate ID="CustomFieldRendering" runat="server"> <Template> <asp:Label runat="server" ID="lblEventID"></asp:Label> </Template> </SharePoint:RenderingTemplate>
3、 在项目中添加一个customfieldcontrol .cs 文件,这个类文件可以理解为刚刚创建的ascx控件的后置代码文件.
using System; using System.Collections.Generic; using System.Web.UI.WebControls; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint.WebPartPages; namespace AutoIncrease { public class customfieldcontrol : BaseFieldControl { protected Label lblEventID; const string strControlName = "lblEventID"; const string strStartKey = "01"; const string strFieldTypeName = "AutoIncrease"; protected override string DefaultTemplateName { get { return "CustomFieldRendering"; } } public override object Value { get { EnsureChildControls(); return lblEventID.Text; } set { lblEventID.Text = value.ToString(); } } public override void Focus() { EnsureChildControls(); lblEventID.Focus(); } protected override void CreateChildControls() { try { string strmax = string.Empty; string itemValue = string.Empty; if (Field == null) { return; } base.CreateChildControls(); lblEventID = (Label)TemplateContainer.FindControl(strControlName); if (ControlMode == Microsoft.SharePoint.WebControls.SPControlMode.New) { ListFormWebPart listWP = (ListFormWebPart)this.Parent.Parent.Parent.Parent.Parent.Parent.Parent; SPList sList = listWP.ItemContext.Web.Lists[listWP.ListId]; lblEventID.Text = System.DateTime.Now.ToString("yyyyMMdd") + strStartKey; if (sList != null) { foreach (SPListItem item in sList.Items) { for (int i = 0; i < item.Fields.Count - 1; i++) { if (item.Fields[i].FieldTypeDefinition.TypeName == strFieldTypeName) { itemValue = item[item.Fields[i].Id].ToString(); if (itemValue.Substring(0, 8) == System.DateTime.Now.ToString("yyyyMMdd") && int.Parse(itemValue.Remove(0, 8)) > (strmax.Length > 0 ? int.Parse(strmax.Remove(0, 8)) : -1)) { strmax = itemValue; } break; } } } if (strmax.Length == 10) //yyyyMMddXX { lblEventID.Text = strmax.Substring(0, 8) + (int.Parse(strmax.Remove(0, 8)) + 1).ToString().PadLeft(2, '0'); } if (strmax.Length > 10) //yyyyMMddXXX... { lblEventID.Text = strmax.Substring(0, 8) + (int.Parse(strmax.Remove(0, 8)) + 1).ToString(); } } } } catch (Exception ex) { lblEventID.Text = ex.ToString(); } } } }
4、新建另一个类CustomField .cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; namespace AutoIncrease { class CustomField : SPFieldText { public CustomField(SPFieldCollection fields, string fieldName) : base(fields, fieldName) { } public CustomField(SPFieldCollection fields, string fieldName, string displayName) : base(fields, fieldName, displayName) { } public override BaseFieldControl FieldRenderingControl { get { BaseFieldControl fieldControl = new customfieldcontrol(); fieldControl.FieldName = this.InternalName; return fieldControl; } } public override string GetValidatedString(object value) { return value.ToString(); } } }
5、最后,创建一个自定义Field的xml规范文件(文件命名要以fldtypes_打头),其主要功能是将我们上面定义的自定义Field添加到Sharepoint系统中去.
<?xml version="1.0" encoding="utf-8" ?> <FieldTypes> <FieldType> <Field Name="TypeName">AutoIncrease</Field> <Field Name="ParentType">Text</Field> <Field Name="TypeDisplayName">AutoIncrease</Field> <Field Name="TypeShortDescription">AutoIncrease</Field> <Field Name="UserCreatable">TRUE</Field> <Field Name="ShowOnListCreate">TRUE</Field> <Field Name="ShowOnSurveyCreate">TRUE</Field> <Field Name="ShowOnDocumentLibrary">TRUE</Field> <Field Name="ShowOnColumnTemplateCreate">TRUE</Field> <Field Name="Sortable">TRUE</Field> <Field Name="Filterable">TRUE</Field> <Field Name="FieldTypeClass">AutoIncrease.CustomField, AutoIncrease, Version=1.0.0.0, Culture=neutral, PublicKeyToken=35c29fe3d73c8105</Field> <Field Name="SQLType">nvarchar</Field> </FieldType> </FieldTypes>
6、 上面所说的Code步骤已经完成,接下来就是将Field部署到 Sharepoint中去
将编译好的Class Library项目dll,注册到系统GAC中:C:/WINDOWS/assembly(可直接拖动到该文件下) 将.ascx控件复制到: C:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/CONTROLTEMPLATES目录中 将.xml文件复制到: C:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/XML 目录中 最后,run--> iisreset ,重启后就可以登录到sharepoint中,查看实验成果了!
最后感谢http://www.cnblogs.com/chenchaospr/archive/2009/02/20/1394709.html 文章的帮助,谢谢~
