Click here to Skip to main content
12,560,765 members (51,427 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked

How to build a WSS custom list item ID field

, 23 Feb 2009 CDDL
Rate this:
Please Sign up or sign in to vote.
Introduces a way to create a key column in a SharePoint list.


This article introduces a way to create a key column in a SharePoint list.


In WSS 3.0, every list item has an ID field which is read only. However, a SharePoint developer may find it is difficult to make advantage of the ID field in customized list items. They need a unique key field which is read only and whose value can synchronize with the ID field. Furthermore, it's better to make its value customizable like Project ID 1001, 1002 or Sale Order 70001, 70002...

Using the Code

The logic to implement this unique key field includes two things: a custom field type which is an integer and is read only, and an event handler feature which forces the new item value to be added by one automatically.

Let's see the code in the custom field type first:

public class CodeProjectNumberField : NumberField
    protected override void Render(System.Web.UI.HtmlTextWriter output)
            if (Controls.Count > 0)
                ((TemplateBasedControl)Controls[0]).TemplateName = 
                output.Write("Read Only Field");
        catch (Exception ex)

In the project custom field type, we customized the number field control's template to customTemplate.ascx. In this Custom Template file, we can easily set the number field control as read only. Please check the Custom Template file and compare it with HIV12\TEMPLATE\CONTROLTEMPLATES\DefaultTemplates.ascx.

Now, let me introduce the event handler feature. We will set the key field value and format it at the time of ItemAdded:

public class ReceiverHandler : SPItemEventReceiver
    public override void ItemAdded(SPItemEventProperties properties)
        string CodeProjectCustomFieldName = 
        string CodeProjectCustomFieldSeed = 
        if (!properties.ListItem.Fields.ContainsField(CodeProjectCustomFieldName))

        int intCodeProjectCustomFieldSeed = 1000;
        Int32.TryParse(CodeProjectCustomFieldSeed, out intCodeProjectCustomFieldSeed);

        properties.ListItem[CodeProjectCustomFieldName] = 
          (properties.ListItem.ID + intCodeProjectCustomFieldSeed).ToString();

We set the FieldName and ListName in the web.config file to force this event handler to only affect the field we defined. We also have to define the field seed to customize the value format. Then, we calculate the new field value based on the item ID values...

One important thing I have to say is that after all the event handler and custom field types are created and deployed, we have to customize FLDTYPES.XML under the folder HIV12\Template\XML to make the new field type appear when creating a new list column. (I suggest creating another FLDTYPES.XML like fldtypes_codeproject.xml instead of using the existing one. The reason is it may be overwritten when WSS is updated.)

Points of Interest

While figuring out this solution, I felt Microsoft SharePoint team is always trying to make their products better to meet end user requirements, and I believe we will be able to see Microsoft's implementation on this issue in the next version.


I will keep updating as I dig into the SharePoint world. In fact, there are lots of issues like this. I think Microsoft may make WSS 3.0 much better if they get more time before publishing the next version.


This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


About the Author

David Meng
Web Developer MapleCity Consulting Inc.
Canada Canada
I am so happy to have the opportunity to share experience each other with you. Currently I am a web developer on sharepoint solutions. In this field I have developed many web parts for Toronto based organizations. Recently I have done one portal customization and upgrade it from WSS 2.0 to WSS3.0. I like sharepoint development. It brings me to the world of the collision between cutting edge techniques and traditional ways. I am enjoying the experience to know Microsoft wisdoms.

You may also be interested in...


Comments and Discussions

GeneralYour source is missing some code Pin
Christopher G. Lasater22-Feb-11 8:37
memberChristopher G. Lasater22-Feb-11 8:37 
GeneralQuestion Pin
Ralph Acevedo15-Jan-10 3:15
memberRalph Acevedo15-Jan-10 3:15 
GeneralLists Development Pin
Jovialwhispers13-Jul-09 1:42
memberJovialwhispers13-Jul-09 1:42 
GeneralNice work Pin
Yuan Ren8-Mar-09 23:11
memberYuan Ren8-Mar-09 23:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161026.1 | Last Updated 23 Feb 2009
Article Copyright 2009 by David Meng
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid