SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a sharepoint custom list 'Contacts' where items get created.I want to write a event receiver for blocking of duplicate item entry here say name(column of list).

I know this can be achieved by enabling the unique constraint on list.but I need to make a tool/code block.

Can anyone please explain how can I achieve this?


share|improve this question
up vote 0 down vote accepted

There are 10's of examples online stating how to check for duplicates items in a list, which you can change for contact list,

here's an example,

public override void ItemAdding(SPItemEventProperties properties)
       if (properties.ListTitle.Equals("Book Massage"))
               using (SPSite thisSite = new SPSite(properties.WebUrl))
                   thisSite.AllowUnsafeUpdates = true;

                   SPWeb thisweb = thisSite.OpenWeb();
                   SPList spList = thisweb.Lists[properties.ListTitle];
                   SPQuery query = new SPQuery();
                   query.Query = @"<Where><Eq><FieldRef Name='Time' /><Value Type='Choise'>" +
                                 properties.AfterProperties["Time"] + "</Value></Eq></Where>";

                   SPQuery query2 = new SPQuery();
                   query2.Query = @"<Where><Eq><FieldRef Name='Date' /><Value Type='Choise'>" +
                                  properties.AfterProperties["Date"] + "</Value></Eq></Where>";

                   SPListItemCollection listItem = spList.GetItems(query);
                   SPListItemCollection listItem2 = spList.GetItems(query2);

                   if (listItem.Count > 0 && listItem2.Count > 0)
                       properties.Cancel = true;
                       properties.ErrorMessage = "The item exsists braa!";


For More Information click here

share|improve this answer
thanks a lot.It worked. – Navoneel Feb 28 '13 at 10:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.