.Net & SharePoint '07

Technical blog for .Net and all SharePoint 2007 related Information

About the author

Me(Prince) and my wife are B.E in I.T & C.S.E respectively.  I a certified MCPD: Web from 2007 Dec. I am Intrestes in Web Application, MOSS, EPM, etc.
Now working with Deira International School, as IT Application & Help Manager. I have started my career as "Software Developer" @  REACH Sewn Technologies and Consulting Pvt. Ltd, Bangalore India from Oct 2004 to Feb 2006, then as "Web & Intranet Developer" @ Fosroc International Ltd, Dubai from April 2006 to Sep 2009.
You can catch me on mail@jpy-tech.com or mail@princepy.com. Or on 00971 - 50 - 4284530 

Google Translate

Tag cloud

Calendar

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

RecentComments

Comment RSS

Google Your Location


Sharepoint Unique Column (PK) : Using event handler

Many a times in a Sharepoint list we want to have one of the columns to act as a unique column. But there is no easy way to do it.
 
There are a couple of ways to make this happen,
            Use a custom field which behaves like a unique column.
            Use an event handler to check for uniqueness.
 
I normally prefer the use of event handler for this purpose. Event handler gives an easy way to make any or many column in a list to behave as if they can contain only unique data. Below is an example of the code required in the event handler.

const string _QUERY = @"<Query><Where><Eq><FieldRef Name""{0}"" /><Value Type=""Text"">{1}</Value></Eq></Where></Query>";
public override void ItemAdding(SPItemEventProperties properties)
{
    using (SPWeb web = properties.OpenWeb())
    {
        /*get the current list*/
        SPList list = web.Lists[properties.ListId];
        string columnName = "";
        foreach (SPField fld in list.Fields)
        {
            if (fld.Description.Contains("[$UNIQUE$]"))
            {
                columnName = fld.InternalName;
                break;
            }
        }

        if (properties.AfterProperties[columnName] != null)
        {
            string currentValue = properties.AfterProperties[columnName].ToString();
            SPQuery _query = new SPQuery();
            _query.Query = string.Format(_QUERY, columnName, currentValue);
            SPListItemCollection itemsWithSameValue = list.GetItems(_query);
            if (itemsWithSameValue.Count > 0)
            {
                properties.Cancel = true;
                properties.ErrorMessage = "There is already an item with the same value(" + currentValue + ") for the column / Field(" + columnName + ") in this list.";
            }
        }
    }
}


This will create an event handler for the adding event and stop any new addition of record if the value already exists. Remember to make this work properly, you will need another event for updating event to stop user from adding same value by editing a record.
 
thanks Vikram


Categories: MOSS | VS 2005 | WSS
Posted by admin on Thursday, February 05, 2009 11:47 AM
Permalink | Comments (8) | Post RSSRSS comment feed

Comments

ashish India

Thursday, March 12, 2009 11:30 AM

this is nice, but when some one tries to add duplicate name it goes to an error page.

is there a way with which we can show the error message in some nice way?

thanks in advance

Mario Nateras United States

Saturday, June 06, 2009 3:21 AM

Hello Guys,

I have tried your excellent example, but somehow the properties.Cancel is not working, the line does get executed I make sure of that thru debugging, but somehow the item is added a no error message is shown

What can it be?

My SharePoint Blog

Friday, June 19, 2009 1:43 AM

Trackback from My SharePoint Blog

Mange Event Handlers (Setup and Usage)

Adee Kaye United States

Wednesday, July 15, 2009 1:41 AM

I don't like your template but your posts are quite good so I will check back!

запознанства

Monday, July 20, 2009 2:53 AM

Thank you, for this code Smile Cheers!

Hoodia

Monday, July 20, 2009 10:30 PM

Thanks.. Funny, I actually had this on my mind a few days ago..

biodental

Tuesday, July 28, 2009 12:49 PM

Great job mate! I like your work. Thanks for sharing

Vladimir Russia

Tuesday, November 03, 2009 10:38 PM

Great job. I'm use you idea to create this EventHandler. Work perfect!!!