Search This Blog

Saturday, April 23, 2011

How to create a custom field type in SharePoint 2010 using visual studio 2010?

There are default field types like Text, Note, Boolean, Integer, Number, Decimal, DateTime, Choice, Lookup etc. We can also make our custom field types by using visual studio 2010


The structure should look like the below figure:




Steps to create custom field types:
  
First create a new SharePoint project using the Empty SharePoint project template.
Inside the project you need to add a public class for each custom field type.
You need to add a special xml file namely fldtypes_{project name}.xml, to deploy the field               type.


public class EmployeeStartDate : SPFieldDateTime
    {

        public EmployeeStartDate(SPFieldCollection fields, stringfieldName)
            : base(fields, fieldName) { }

        public EmployeeStartDate(SPFieldCollection fields, stringtypeName, string displayName)
            : base(fields, typeName, displayName) { }


        // configure field to display date but not time
        public override void OnAdded(SPAddFieldOptions op)
        {
            this.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;
            this.Update();
        }
}
The next step is to override whatever base class methods and properties make sense for your particular scenario. Here is the code sample that I have done here:
// add logic to create default date as first Monday
        public override string DefaultValue
        {
            get
            {
                DateTime startDate = DateTime.Today;
                while (startDate.DayOfWeek != DayOfWeek.Monday)
                {
                    startDate = startDate.AddDays(1);
                }
                returnSPUtility.CreateISO8601DateTimeFromSystemDateTime(startDate);
            }
        }

        // add validation to ensure start date is a Monday
        public override string GetValidatedString(object value)
        {
            DateTime input = System.Convert.ToDateTime(value);
            if (input.DayOfWeek != DayOfWeek.Monday)
            {
                throw new SPFieldValidationException("Employee start date must be a monday");
            }
            return base.GetValidatedString(value);
        }

Here we have completed the creation of the custom field type and now we need to deploy.

Deployment:

For this we need to create a XML file. The naming convention is very much important, it must start with fldtypes then _  and then the project name.xml. For example fldtypes_FieldTypeDemo.xml.
Within that XML file each field type is created using a FieldType element, which must reside inside top-level FieldTypes element. The Field elements are required to provide information about the custom field type, its parent type, its display name, the field class name, the name of the assembly etc. Below is the code sample

<o:p></o:p>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span class="Apple-style-span" style="font-family: Consolas;"></span>
<div class="MsoNormal" style="color: #a31515; line-height: normal; margin-bottom: 0.0001pt;"><span class="Apple-style-span" style="font-family: Consolas;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></span></span></div><pre class="brush:csharp"><div class="MsoNormal" style="color: #a31515; line-height: normal; margin-bottom: 0.0001pt;"><span class="Apple-style-span" style="font-family: Consolas;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><!--?</span--><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">xml</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">version</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">=</span><span style="font-family: Consolas; font-size: 9.5pt;">"<span style="color: blue;">1.0</span>"<span style="color: blue;"> </span><span style="color: red;">encoding</span><span style="color: blue;">=</span>"<span style="color: blue;">utf-8</span>"<span style="color: blue;"> ?></span></span></span></span></div><div class="MsoNormal" style="color: #a31515; line-height: normal; margin-bottom: 0.0001pt;"><span class="Apple-style-span" style="font-family: Consolas;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">FieldTypes</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span></span></div><div class="MsoNormal" style="color: #a31515; line-height: normal; margin-bottom: 0.0001pt;"><span class="Apple-style-span" style="font-family: Consolas;"><span style="font-family: Consolas; font-size: 9.5pt;"><o:p> </o:p></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">FieldType</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span class="Apple-style-span" style="color: blue; font-size: 13px;">  </span></span></div><div class="MsoNormal" style="color: #a31515; line-height: normal; margin-bottom: 0.0001pt;"><span class="Apple-style-span" style="font-family: Consolas;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Field</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">=</span><span style="font-family: Consolas; font-size: 9.5pt;">"<span style="color: blue;">TypeName</span>"<span style="color: blue;">></span>EmployeeStartDate<span style="color: blue;"><!--</span--><span style="color: #a31515;">Field</span><span style="color: blue;">></span></span></span></span></div><div class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span class="Apple-style-span" style="font-family: Consolas;"><span style="font-family: Consolas; font-size: 9.5pt;"><span class="Apple-style-span" style="color: blue;"><</span></span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Field</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">=</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"<span style="color: blue;">ParentType</span>"<span style="color: blue;">></span>DateTime<span style="color: blue;"><!--</span--><span style="color: #a31515;">Field</span><span style="color: blue;">></span><o:p></o:p></span></span></span></div><div class="MsoNormal" style="color: #a31515; line-height: normal; margin-bottom: 0.0001pt;"><span class="Apple-style-span" style="font-family: Consolas;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Field</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">=</span><span style="font-family: Consolas; font-size: 9.5pt;">"<span style="color: blue;">TypeDisplayName</span>"<span style="color: blue;">></span>Employee Start Date<span style="color: blue;"><!--</span--><span style="color: #a31515;">Field</span><span style="color: blue;">></span><o:p></o:p></span></span></span></div><div class="MsoNormal" style="color: #a31515; line-height: normal; margin-bottom: 0.0001pt;"><span class="Apple-style-span" style="font-family: Consolas;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Field</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">=</span><span style="font-family: Consolas; font-size: 9.5pt;">"<span style="color: blue;">TypeShortDescription</span>"<span style="color: blue;">></span>Employee Start Date (demo)<span style="color: blue;"><!--</span--><span style="color: #a31515;">Field</span><span style="color: blue;">></span><o:p></o:p></span></span></span></div><div class="MsoNormal" style="color: #a31515; line-height: normal; margin-bottom: 0.0001pt;"><span class="Apple-style-span" style="font-family: Consolas;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Field</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">=</span><span style="font-family: Consolas; font-size: 9.5pt;">"<span style="color: blue;">UserCreatable</span>"<span style="color: blue;">></span>TRUE<span style="color: blue;"><!--</span--><span style="color: #a31515;">Field</span><span style="color: blue;">></span><o:p></o:p></span></span></span></div><div class="MsoNormal" style="color: #a31515; line-height: normal; margin-bottom: 0.0001pt;"><span class="Apple-style-span" style="font-family: Consolas;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Field</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">=</span><span style="font-family: Consolas; font-size: 9.5pt;">"<span style="color: blue;">FieldTypeClass</span>"<span style="color: blue;">></span><o:p></o:p></span></span></div><div class="MsoNormal" style="color: #a31515; line-height: normal; margin-bottom: 0.0001pt;"><span class="Apple-style-span" style="font-family: Consolas;"><span style="font-family: Consolas; font-size: 9.5pt;">FieldTypeDemo.EmployeeStartDate,FieldTypeDemo, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=2a494da19a21cf85<o:p></o:p></span></span></div><div class="MsoNormal" style="color: #a31515; line-height: normal; margin-bottom: 0.0001pt;"><span class="Apple-style-span" style="font-family: Consolas;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><!--</span--><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Field</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></span></span></div><div class="MsoNormal" style="color: #a31515; line-height: normal; margin-bottom: 0.0001pt;"><span class="Apple-style-span" style="font-family: Consolas;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><!--</span--><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">FieldType</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></span></span></div><div class="MsoNormal" style="color: #a31515; line-height: normal; margin-bottom: 0.0001pt;"><span class="Apple-style-span" style="font-family: Consolas;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><!--</span--><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">FieldTypes</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">><o:p></o:p></span></span></span></div><div class="MsoNormal" style="color: #a31515; line-height: normal; margin-bottom: 0.0001pt;"><span class="Apple-style-span" style="font-family: Consolas;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></span></span></div></pre><span class="Apple-style-span" style="font-family: Consolas;"><o:p></o:p></span>
</div><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></span></div>


TypeName and FieldTypeClass name are very much important.
Now deploy the solution and enjoy.



2.  

6 comments:

  1. Greetings! Very useful advice within this article!
    It is the little changes which will make the most important changes.

    Thanks for sharing!

    Here is my web blog: permissive

    ReplyDelete
  2. Hi there, I noticed your blog on http://pravahaminfo.
    blogspot.com/ while searching for a similar topic, your website came up, it seems
    good. I've bookmarked it in my google book marks.
    . It's made me remember precisely why I loved this blog so
    much.
    Thanks for keeping it going, I'm gonna start checking back more frequently. How often do you update your blog.?

    Feel free to visit my site highest yielding money market

    ReplyDelete
  3. Its like you read my mind! You appear to grasp a lot about this, such as you wrote the ebook in it or something.
    I feel that you just could do with a few % to pressure the message house a bit, but instead of that, that is magnificent blog. A great read. I will definitely be back.

    Feel free to visit my web page Pure Green Coffee Bean Extract

    ReplyDelete
  4. I read this post completely about the resemblance of
    most recent and preceding technologies, it's awesome article.

    Also visit my website; barbituriques

    ReplyDelete
  5. Asking questions are genuinely nice thing if you are not
    understanding something totally, except this article provides good understanding yet.



    Feel free to surf to my site ... no deposit casino blog

    ReplyDelete
  6. Hi, I'm a writer based out of Latchford, Great Britain and I found your site via http://pravahaminfo.blogspot.com/. Do you have any helpful hints for up-and-coming writers? I'm working on
    beginning my own blog soon but I don't really know where to begin. Do you believe I should begin with a free site like Chyrp or shell out some cash into a pay site? There are so many options out there that I'm
    absolutely overwhelmed... Any tips?

    Also visit my web site: Défibrillateurs

    ReplyDelete