33
SHAREPOINT CUSTOM FIELD TYPES Eugene Rosenfeld CTO, Black Blade Associates 2-time SharePoint MVP [email protected] www.blackbladeinc.com thingsthatshouldbeeasy.blogspot.com

Eugene Rosenfeld: SharePoint Custom Field Types

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Eugene Rosenfeld: SharePoint Custom Field Types

SHAREPOINT CUSTOM FIELD TYPES

Eugene RosenfeldCTO, Black Blade Associates2-time SharePoint MVP

erosenfeld@blackbladeinc.comwww.blackbladeinc.comthingsthatshouldbeeasy.blogspot.com

Page 2: Eugene Rosenfeld: SharePoint Custom Field Types

CON

TENTS

Contents

Custom Field Types Defined Business Cases Custom Field Types and Alternate WSS Extensibility Custom Field Types Implementation Demo

Page 3: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES DEFIN

EDOverview

• Custom field types are Windows SharePoint Service’s mechanism for allowing developers to:• Define atomic data structures with custom data structures• Custom data validation• Custom rendering• Custom property storage and processing

• These items are “what” custom field types can do

• Custom field types can be thought of as a merging between fields, custom web parts, validators, and event receivers

Page 4: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES DEFIN

EDCustom Field Type Scenarios

• Custom field types allow developers to interact with and extend the way in which Windows SharePoint Services organizes, collects, modifies, stores, and displays list item data:• Add a field that is based on a custom field type to a list • Edit a list field that is based on a custom field type• Show data stored in a custom field type instance in the list item display form• Show data stored in a custom field type instance in the list item edit form• Show data stored in a custom field type instance in the list item new form• Show data stored in a custom field type instance in the list view

• These items describe “when” we would resort to custom field types.

Page 5: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES DEFIN

EDCUSTOM DATA STRUCTURES

• Custom field types may define one or more field data components• SharePoint treats all field components as one, indivisible entity• SharePoint saves and loads all components of a custom field type as a single unit• Any custom data structure is fair game, as long as it is serializable as text

• Why text, even for booleans, numbers, dates, etc?• All data is stored in SQL. Data is retrieved and modified using SQL

statements. SQL statements are text, even for logically binary data.

Page 6: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES DEFIN

EDCustom Rendering

• Developers can associate web controls to display field data to users• Think of field render controls as custom web parts that are always associated with

all instances of the custom field type• Custom rendering controls may include:

• Web custom controls• Client controls or HTML• JavaScript• AJAX• Images• Anything else that can render a MIME type that the client browser can

interpret

Page 7: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES DEFIN

EDCustom Field Type Properties

• Custom field types can define custom properties that instances of those field types can set

• Custom field type properties are meant to allow users to modify the behavior of a custom field type instance

• These properties can be used to:• Set default values for data stored in instance fields• Parameterize custom validation logic• Control rendering of field data

• As with the field data, developers can define custom rendering and validation for custom field type properties

• Custom field properties are very similar to custom web part properties

Page 8: Eugene Rosenfeld: SharePoint Custom Field Types

BUSIN

ESS CASESOverview

• Custom Data Storage• Custom Data Validation• Custom Field Rendering• Custom Behavioral Logic

Page 9: Eugene Rosenfeld: SharePoint Custom Field Types

BUSIN

ESS CASESCustom Data Storage

• Atomic multicolumn field• Address (street, city, state, zip)• Full name (first, middle, last)• Geo-data (latitude, longitude, elevation)

• Pointer to external data• Primary key to an external database table• Parameter values to a Web Service call• URI to external records management system

Page 10: Eugene Rosenfeld: SharePoint Custom Field Types

BUSIN

ESS CASESCustom Data Validation

• Data is in a particular format• Email• SSN• Phone number• Product code• Guid

• Related data from multiple controls is consistent• Address (street, city, state, zip)• Full name (first, middle, last)

• Data is validated against an external entity• Database• XML file• Web Service – address is an actual valid street address

Page 11: Eugene Rosenfeld: SharePoint Custom Field Types

BUSIN

ESS CASESCustom Field Rendering

• Graphic• Chart• Map• KPI Indicator• Anti-bot / anti-OCR image

• Embedded object• Flash• Silverlight• ActiveX

• AJAX• Iframe• VML / SVG• User presence information (name with presence field)

Page 12: Eugene Rosenfeld: SharePoint Custom Field Types

BUSIN

ESS CASESCustom Behavioral Logic

• Typically used to control the behavior of the field when it is being populated with data• Accessing metadata stored on a system external to SharePoint• Custom dialog boxes• Wizard interfaces• Trees and other navigation elements• Asset picker control

Page 13: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES AND

ALTERNATE W

SS EXTENSIBILITY

Overview

• CUSTOM FIELD TYPES AND CONTENT TYPES• CUSTOM FIELD TYPES AND CUSTOM WEB PARTS• CUSTOM FIELD TYPES AND EVENT RECEIVERS• CUSTOM FIELD TYPES AND WORKFLOWS

Page 14: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES AND

CON

TENT TYPES

Similarities between Custom Field Types and Content Types

• Both can be used to define a grouping or set of discrete data• Both can trigger code execution when data changes

Page 15: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES AND

CON

TENT TYPES

Deciding which One to Use

• Use custom field types when:• Must be created by a developer• Must be defined using managed code (C#, VB.Net, etc)• Can complicate matters when writing code that consumes list field values

that are custom field types• May prevent the use of third party tools or applications if these can not deal

with custom field types • Use content types when:

• Definable by content manager through the web UI• Deployable to SharePoint farm as XML (no code needed)

Page 16: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES AND

CUSTO

M W

EB PARTSSimilarities between Custom Field Types and Custom Web Parts

• Both can define custom rendering for SharePoint data• Both can define custom metadata that alters the rendering of the data• Both can perform processing logic on their respective data• Both can access data outside of SharePoint

Page 17: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES AND

CUSTO

M W

EB PARTSDeciding which One to Use

• Use a custom field type when:• You want the custom defined rendering to always be associated with the

field data• You want to define custom rendering that will be used in the listview web

part when the field data is shown in the listview• You want to define custom rendering for field data that will be used in all

instances of the display item, new item, and edit item forms for list items that contain the a field instance

• Use a web part when:• You want to define custom rendering for a group of fields • You want to create views that aggregate information from multiple list items• You want to give users the ability to drag the custom rendering onto a web

part page through a web browser• You want to participate in web part connections

Page 18: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES AND

EVENT RECEIVERS

Similarities between CUSTOM FIELD TYPES AND EVENT RECEIVERS

• Both can be used to run custom code when data in a list is about to change• Both can prevent changing list data from being saved to the content database• Both can be used to catch field schema changes on lists

Page 19: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES AND

EVENT RECEIVERS

Deciding which One to Use

• Use a custom field type when:• You want the field code to run for every item an item is changed or added to

every list with an instance of the field type exists• Note: Two field instances of a custom field type means the code runs twice!

• Use an event receiver when:• You want to be able to activate or deactivate the code for all lists within a

particular site (i.e. feature)• You need to work with data from several fields in the current list item

Page 20: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES AND

WO

RKFLOW

SSimilarities between Custom Field Types and Workflows

• Both can execute custom code when list item data is changed

Page 21: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES AND

WO

RKFLOW

SDeciding which One to Use

• Use a custom field type when:• You want the field code to run for every item an item is changed or added to

every list with an instance of the field type exists• Note: Two field instances of a custom field type means the code runs twice!• You want to execute code that finishes running quickly and stores no long-

term state information (other than the actual list item field data)• Use a workflow when:

• You want to allow users to be able to associate the code with specific list instances through a web browser

• You want to give users the option to execute the code automatically or manually

• Your code needs to run for a long time, over days, weeks, or months• You will need to collect information from users involved in the code

execution through the browser• You need to work with data from several fields in the current list item

Page 22: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES IMPLEM

ENTATIO

NOVERVIEW

• Custom Field Type Definition• Custom Field Type Properties• Custom Field Data Validation• Custom Field Value Classes• Custom Field Type Deployment

Page 23: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES IMPLEM

ENTATIO

NCustom Field Type Definition• A new fldtypes*.xml file to hold your field type definition

• This is the entry point that the WSS runtime uses to start addressing your custom field type• Similar in concept to the webtemp*.xml files for site definitions

• A class the inherits from the SPField class, more commonly, from another class that inherits from SPField, such as SPFieldMultiColumn

• The type defined by the FieldTypeClass attribute must implement two constructors: public MyFieldComputed (SPFieldCollection fields, string fieldName)

: base(fields, fieldName){} public MyFieldComputed (SPFieldCollection fields, string typeName, string displayName)

: base(fields, typeName, displayName){}

<FieldType> <Field Name="TypeName">MyComputed</Field> <Field Name="TypeDisplayName">$Resources:core,fldtype_computed;</Field> <Field Name="ParentType"> Computed </Field> <Field Name="FieldTypeClass">MySharePoint.MyFieldComputed</Field> <RenderPattern Name="PreviewDisplayPattern"> <HTML><![CDATA[["]]></HTML><Property Select="DisplayName" HTMLEncode="TRUE"/><HTML>"$Resources:core,fldtypes011;</HTML> </RenderPattern> <RenderPattern Name="PreviewEditPattern"><Property Select="DisplayName" HTMLEncode="TRUE"/></RenderPattern> <RenderPattern Name="PreviewNewPattern"><Property Select="DisplayName" HTMLEncode="TRUE"/></RenderPattern> </FieldType>

Page 24: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES IMPLEM

ENTATIO

NCustom Field Type Properties• Custom field properties are defined in a PropertySchema/Fields element• Each custom property is defined as a Field element• Custom field properties are available in code via the SPField. GetCustomProperty method

Note: The SPField.Update method is not called when a new instance of a custom field type is added to a list. Only the SPField.OnAdded method gets called.

• Two ways to render the custom properties:• Using the PropertySchema element

Note: There seems to be a bug in the PropertySchema element rendering system. The edit form for the list field does not bind the custom field property values to the form control correctly. Instead of binding the to the property values, the form binds to the property display names. The only known workaround is to use the next option.

• Using a field editor user control

<FieldType> <Field Name="TypeName">USAddress</Field> … <PropertySchema> <Fields> <Field Name="DefaultState" DisplayName="Default State" MaxLength="2" DisplaySize="2" Type="Text"> <Default>WA</Default> </Field> <Field Name="DefaultZip" DisplayName="Default Zip" MaxLength="5" DisplaySize="5" Type="Text"> <Default>98052</Default> </Field> </Fields> </PropertySchema></FieldType>

Page 25: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES IMPLEM

ENTATIO

NCustom Field Data Validation

• Custom field types are validated by overriding the SPField. GetValidatedString method in the field class

• Throw the SPField.SPFieldValidationException exception when the supplied field value is not for the field

Page 26: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES IMPLEM

ENTATIO

NCustom Field Value Classes

• Custom field value classes do two primary tasks:• Convert field value as represented by managed types into a string

• Done by overriding the SPField. GetValidatedString method in the custom field class

• The default implementation calls the ValueClass.ToString method to get the string

• Convert a string field value back to a managed type• Done by overriding the SPField. GetFieldValue method in the custom

field class• Value classes must be serializable, implementing the ToString method

Page 27: Eugene Rosenfeld: SharePoint Custom Field Types

CUSTO

M FIELD

TYPES IMPLEM

ENTATIO

NCustom Field Type Deployment

• Fldtypes*.xml file must get copied to the "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\TEMPLATE\XML\“ directory

• Assemblies that implement the custom field, custom field values, rendering controls must be deployed to the GAC

• Any assemblies referenced by the custom field assemblies must also reside in the GAC

• Requires an IIS application pool recycle or an IISRESET in order for changes to take affect.• The fldtype*.xml files are reparsed by the IIS worker process• The assemblies loaded from the GAC are un-cached and re-cached by the IIS

worker process

Page 28: Eugene Rosenfeld: SharePoint Custom Field Types

DEM

ODemo

Code walkthrough and demo of Media Field Type

http://wsswmpcft.codeplex.com

Page 29: Eugene Rosenfeld: SharePoint Custom Field Types

CLOSEO

UT

Summary

• Defining custom field types• Reviewing the business cases that justify creation of custom field types• Comparison between custom field types and other WSS extensibility• Code walkthrough and demo of several custom field type implementations

Page 30: Eugene Rosenfeld: SharePoint Custom Field Types

CLOSEO

UT

Additional Resources

Black Blade Associateshttp://www.blackbladeinc.com

“Things that should be Easy” by Eugene Rosenfeldhttp://thingsthatshouldbeeasy.blogspot.com/

Phone number custom field type by Morgan Everett & Mark Collinshttp://www.sharethispoint.com/archive/2006/08/07/23.aspx#codetwo

SharePoint SDK sample: How to create a custom field controlhttp://msdn2.microsoft.com/en-us/library/aa981226.aspx

SharePoint Server 2007 Visual How Toshttp://msdn2.microsoft.com/en-us/library/bb530308.aspx

Page 31: Eugene Rosenfeld: SharePoint Custom Field Types

CLOSEO

UT

Questions?

Page 32: Eugene Rosenfeld: SharePoint Custom Field Types

CLOSEO

UT

Thank you sponsors!CLO

SEOU

TThank you sponsors!

Page 33: Eugene Rosenfeld: SharePoint Custom Field Types

2 ASUS Netbooks

Also Some Books1 Typemock Isolator LicenseA 2-5 Day Course from SetFocus on SharePointTelerik RAD Controls Set1 DeliverPoint WFE 2010 license (Worth $1500)1 BCS Meta Man license (Worth $1200)1 Lightning Conductor 2010 WFE license (Worth $800)1 Lightning Storm Forums license. (Worth $600)

CLOSEO

UT

Remember to fill out evaluations for your chance to win cool prizes!CLO

SEOU

TRemember to fill out evaluations for your chance to win cool prizes!

3 Apple IPAD 32 GB Wifi