Create your own field in Sharepoint 2010

Posted: April 11, 2012 in code, Field, Sharepoint 2010
Tags: , , , , ,

I am working in the Sharepoint development from more than 6 years, first as developer then as a software architect now as teacher and Technical mentor and Technical Leader, and one-off the things that always make the developers shy is when we say “and if we build our own Sharepoint field”.

Look’ s like we are talking about black magic, the true is that building a field and have the knowledge to do that is the base to build yourself as a good Sharepoint developer because if you understand the “magic” behind this task you understand the way Sharepoint works. The  true is that you don’t have a lot of explanation about this topic on books or training classes.

In this post i will try to explain you the art of building a custom Field, and explain each need and step to achieve the goal of creating a custom field.

In our example we will build a field that will allow a sharepoint user to define the elements of a list that should appear ( a kind of multiplelookupfield but made based on a variationroot site). Since nowadays almost developers are using visual studio we will use the visual studio 2010 as our development tool.

Let’s start by creating a new empty SharePoint project in Visual Studio 2010

After that we should start to create the struture of our project according to the needs of our field. start by creating a mapped folder to the ControlTemplates Sharepoint Folder that is the place where User Controls of the Sharepoint Fields are deployed. Then create a mapped folder to the XML folder, this folder is where Sharepoint stores the definitions of the different elements that are recognized as part of the framework. In some cases You may also need to Map the XSLT folder typically when you have the need or will to use a transform to Generate the forntEnd of your control.

After the creation off our project structure let’s start by specifying our field definition. In sharepoint whenever you need to define a new type (and that is one way to see a Field in the sharepoint world). To do that we need to create a xml file in the mapped folder XML (that will be deployed by the WSP Solution to SharePointRoot\Template\XML), the file should be named as fldTypes_*.xml. The files from the SharePointRoot\Template\XML are read by the runtime at the sharepoint starts or whenever the application pool is started. This file is also used This file is used during list creation to define how field types are rendered in one or more of the different modes for viewing list data (more about this topic in   MSDN ).

Is int this file that the magic begins to be created let’s take a look to understand the meaning of each entry.In the TypeName we define the Name of the new Field Type. The ParentType defines the base type of sharepoint form where the new fields inherits functionality. the TypeShortdescription is the description definition that will appear at the sharepoint backoffice when the Sharepoint administrator Choose the field. UserCreatable defines  if the user can create or not new instances of the field. The FieldTypeClass defindes the server side implementation of the field.

After defining or Type, what had ben done in the creation of the file fldTypes_SPListItemSelectionField.xml let’s start by implementing the class that defines the behaviour of the field. for that intent let’s add a new cs file to our project with the follow name ItemListSelectionField.cs. In that file let’s declare the call ItemListSelectionField that inherits from SPTextFied as suggested in the Field definition written above.

The class has SPFieldText as is base class since we want to extend from this Field Type. Typically when defining a new field we need to override two methods the FieldRenderingControl and the GetValidationString (we have defined the second one only for demonstration proposes).

The GetValidatedString is responsible for returning the expression that validates our field imagine in cases like an email or others.

The FieldRenderingControl returns the control that is responsible for the rendering of the Field. In our case we are creating a new instance of the of The SPItemListSelectionFieldControl that is our user control. After the implementation of the class that defines our field and to define or field against sharepoint using the field definition file, we should define the way our field render himself.

The easiest way to do that is to use a ASP.NET UserControl, so let’s create a new User Control in the mapped folder ControlTemplate.

As you can see is a normal ASP.NET User Control we have deleted the assembly reference from the ascx since we will delete the class generated by visual studio for code behind to create our own class. In the Code present you can see we have two repeater a first to be used in the edit mode to show the different options of the list we can select and a second one that shows the different user choices separed by the character “;”.

As base class we have consider the BaseFieldControl class. The BaseFieldControl is a Sharepoint Class that defines the behaviour of every Sharepoint Field Controls.  We have override the CrateChildControls method since we have the specific controls present in our ascx file and we have the need to populate/manipulate them. We have also override the DefaultTemplateName this method is responsible for the return of the ID of the SharePoint:RenderingTemplate control that contains the actual implementation of the user interface. and then we also have the need to define the way the value is stored in our field and presented.  Let´s take a look

The way this property works can be a little tricky, the set is called when ever the field is saved and the get whenever the control is rendered.

After this we can build our code and deploy our solution to the Sharepoint farm, and this are the explained needed steps to build your own Sharepoint Field. This is an important knowledge when you really want to customize the types of data you receive from user and to build complex fields. Hope this post is helpful feel free to ask. I will post all the code with is explanation later on codeplex.

Advertisements
Comments
  1. […] SharePoint Blog Post From SharePoint Development – Google Blog Search: I am working in the Sharepoint development from more than 6 years, first as developer then as a […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s