Skip to main content

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.  

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

Post a Comment

Popular posts from this blog

Tab Control in Asp.Net

Scenerio: I need your help in designing tab control in   asp.net .My requirement is I need a tab control in   asp.net (C#) like  for example goto my computer ,right click c drive and select properties.we get tabs like general,security etc....... like that i need to design one tab control(no need of any rightclick) in my web page and the database table columns should come as tabs and inseide the tab data of that particular column should come.   Example:Employee master tab1:Employee name.........his name in the tab tab2:Age.............his age tab3:Address........his address   Solution:   You can do this using a simple div <style type="text/css"> .tabs         {             position: relative;             height: 20px;             margin: 0;   ...

AI and Microsoft: Revolutionizing Efficiency in Nonprofit Organizations

  How AI and Microsoft Enhance Efficiency in Nonprofit Organizations In today’s fast-paced world, nonprofit organizations face unique challenges—limited resources, increasing demands, and the constant need to do more with less. But what if technology could be the game-changer they need? In my latest research paper,  "How AI and Microsoft Enhance Efficiency in Nonprofit Organizations" , I explore how cutting-edge technologies like Artificial Intelligence (AI) and Microsoft’s innovative tools are revolutionizing the way nonprofits operate. From streamlining administrative tasks to enhancing donor engagement and optimizing resource allocation, AI and Microsoft’s solutions are empowering nonprofits to focus on what truly matters—their mission. This paper dives deep into real-world examples, practical applications, and the transformative potential of these technologies. Whether you’re a nonprofit professional, a tech enthusiast, or simply curious about the intersection of technolo...

Social tagging overview in Sharepoint 2010

A tag is a word or phrase that identifies an individual piece of information according to a set of attributes or criteria. Tags make it easy to find and share information about a specific subject or task. Social tagging helps users categorize information in ways that are meaningful to them. Social tagging can improve the quality of search results by filtering against specific tags, and it can also connect individuals who want to share information with other users who have like interests. This article describes the social tagging features in Microsoft SharePoint Server 2010. This article does not describe how to configure social tagging features. It also does not discuss how to implement social tagging features as part of an overall social media strategy for an enterprise. About using social tagging features Social tagging features help users to share information and to retrieve relevant, high-quality content more efficiently. Such sharing encourages collaboration and b...