Click here to Skip to main content
Licence CDDL
First Posted 23 Feb 2009
Views 24,432
Downloads 119
Bookmarked 13 times

How to build a WSS custom list item ID field

By David Meng | 23 Feb 2009
Introduces a way to create a key column in a SharePoint list.

1

2

3

4
4 votes, 100.0%
5
5.00/5 - 4 votes
μ 5.00, σa 1.10 [?]

Introduction

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

Background

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)
    {
        try
        {
            if (Controls.Count > 0)
            {
                ((TemplateBasedControl)Controls[0]).TemplateName = 
                    "~/_controltemplates/CustomTemplates.ascx";
                base.Render(output);
                output.Write("Read Only Field");
            }
            else
                base.Render(output);
        }
        catch (Exception ex)
        {
            PwCPMOExceptionLogging.Error(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 = 
          ConfigurationSettings.AppSettings["ProjectChangeTrackingNumberFieldName"];
        string CodeProjectCustomFieldSeed = 
          ConfigurationSettings.AppSettings["ProjectChangeTrackingNumberFieldSeed"];
        if (!properties.ListItem.Fields.ContainsField(CodeProjectCustomFieldName))
            return;

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

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

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.

History

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.

License

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

Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralYour source is missing some code PinmemberChristopher G. Lasater9:37 22 Feb '11  
GeneralQuestion PinmemberRalph Acevedo4:15 15 Jan '10  
GeneralLists Development PinmemberJovialwhispers2:42 13 Jul '09  
GeneralNice work PinmemberYuan Ren0:11 9 Mar '09  

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

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

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