Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

thanks.

share|improve this question

1 Answer 1

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)
   {
       base.ItemAdding(properties);
       if (properties.ListTitle.Equals("Book Massage"))
       {
           try
           {
               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. –  coder1 Feb 28 '13 at 10:16

Your Answer

 
discard

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.