In many scenarios, we all need to count the number of clicks on an article or document, but SharePoint itself does not design such a field, so we need to do this with simple field development.
First, create the project:
1. Create an empty SharePoint project, add a "SharePoint Map folder" and map it to Template/xml to hold the XML we described for the field.
2. Add an XML to the mapped XML folder, the name is Fldtypes_ field name, the previous name must be such a rule, will be recognized by SharePoint, XML template appended.
3. Add a VisitorRecord.cs file to inherit from the SPField field and add a VisitorRecordControl.cs file to inherit from Basefieldcontrol.
4. Project structure, as shown below:
Second, add code
1. Add XML description
<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">VisitorRecord</Field>
<Field Name="ParentType">Text</Field>
<Field Name="TypeDisplayName">浏览次数</Field>
<Field Name="TypeShortDescription">每点击一次DispForm页,浏览次数+1</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="Sortable">TRUE</Field>
<Field Name="AllowBaseTypeRendering">TRUE</Field>
<Field Name="Filterable">TRUE</Field>
<Field Name="FieldTypeClass">VisitorRecord.VisitorRecord,VisitorRecord, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a3d395e8da16bea6</Field>
<RenderPattern Name="DisplayPattern">
<Column />
</RenderPattern>
</FieldType>
</FieldTypes>
2. Add Visitorrecord method, the following table:
class VisitorRecord : SPField
{
public VisitorRecord(SPFieldCollection fields, string fieldName)
: base(fields, fieldName)
{
Init();
}
public VisitorRecord(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName)
{
Init();
}
void Init()
{
//设置在DisplayForm页显示
this.ShowInDisplayForm = true;
//设置在EditFrom页不显示
this.ShowInEditForm = false;
//在NewForm页不显示
this.ShowInNewForm = false;
}
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl vr1 = new VisitorRecordControl();
vr1.FieldName = this.InternalName;
return vr1;
}
}
}