Skip to main content

Patrick Tisseghem's Blog [MVP SharePoint]

Go Search
U2U Blog Center
U2U website
  

The Happy SharePoint Traveller

U2U Blog Center > Patrick Tisseghem's Blog [MVP SharePoint] > Posts > Preventing users to add a beer that already exists in the beer list (or, how to work with the ItemAdding event)
Preventing users to add a beer that already exists in the beer list (or, how to work with the ItemAdding event)

I have a good crowd here in Sydney. They keep me busy with lots of interesting questions. I still have no news though regarding my bags so I went again to the shops and bought the things that a man needs abroad (socks, underwear and some shirts) J.

Anyway, question from a student was: 'How can we using an event handler to prevent a user from adding an item in a list that is already there?'

Take one of the beer lists (let's go for the German beers):

Can I prevent that a new beer titled Adler Pils Premium is added to the list? The ItemAdding is of course the event you want to work with to solve this. Unfortunately, there are some issues with the ItemAdding. You do not have access directly to the title of the beer when it is keyed in by the user and your code is executed. The ListItem property is null and both the AfterProperties and BeforeProperties seem to be empty. After a quick search, I found a couple of interesting postings about it. Best one (and excuse me if somebody else has done it before) is a posting of a Chinese guy (forgive me not being able to translate your name J). He got me started working out the following code:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Collections;

namespace BeerDuplicationPrevention
{
public class BeerDuplication: SPItemEventReceiver
{

        public override void ItemAdding(SPItemEventProperties properties)
        {
            string title = string.Empty;
            foreach (DictionaryEntry de in properties.AfterProperties)
            {
                if (de.Key.Equals("Title0")) title = de.Value.ToString();
                SPQuery qry = new SPQuery();
                qry.Query = "<Where><Eq><FieldRef Name='Title0' /><Value Type='Text'>" + title + "</Value></Eq></Where>";                
                SPList list = properties.OpenWeb().Lists[properties.ListId]; 
                SPListItemCollection results = list.GetItems(qry);
                if (results.Count > 0)
                {
                    properties.Cancel = true;
                    properties.ErrorMessage = "Dude.. this beer already exists!";
                }
            }
        }
    }
}

The AfterProperties can be consulted in your code by looping over it via a foreach. A small CAML query (created with the new CAML Builder J) finishes it off. Again, credit to all of you who researched this before, but I thought it was a good idea to do an additional posting on this.

Comments

what if afterproperties is null?

Patrick,
I have, in the past, tried to do just that - but afterproperties was always 0 values. I reported to MS (that was during beta2) and they told me "by design" and it wont be fixed.

any comments?
at 22/06/2007 9:17

what if afterproperties is null

yes, when you just look at the AfterProperties in the debugger of VS you'll notice they are empty, but follow the above code and you'll discover that they actually contain values :)

Patrick
Patrick Tisseghem at 22/06/2007 16:45

Shows a different error message

Patrick,
I try the same approach and it gives me a different error message "The data source control failed to execute the insert command."

it is not showing the properties.errormessage that i have set. any ideas why it might be misbehaving?

Thanks!
at 23/06/2007 1:38

Shows a different error message

Patrick,
I try the same approach and it gives me a different error message "The data source control failed to execute the insert command."

it is not showing the properties.errormessage that i have set. any ideas why it might be misbehaving?

Thanks!
at 23/06/2007 1:41

Web Services Anyone?

I encountered the above requirement while working with SharePoint 2003 and SharePoint Web Services.
In SharePoint 2003, when the user click on the "Save and Close" button, SharePoint calls a Javascript function named "ClickOnce()".
I place a hook onto this function and called the "Lists.asmx" Web Service, which contained a where clause.

<WHERE>
  ..Name of Field and Value to filter ...
</WHERE>

The Web Service would return a list of items that fit that criteria. If the Web Service returned a count of Zero then I would let SharePoint Submit the entry, otherwise, I would output an error message letting the user know that the item already exist.

I am sure that you can replicate this onto SharePoint 2007 by using AJAX.

Hope this helps ;)
Jorge Vasquez
jvasquez@metrostarsystems.com
at 25/06/2007 21:28

ItemAdding(), ItemAdded() do not fire for SiteCollection Lists...

Interestingly, ItemAdding and ItemAdded events do not fire for lists at SiteCollection level (or RootWeb).Eg., "User Information List".

Thought someone might be interested in this. Perhaps a bug in WSS 3.0 .. I dont know
at 3/07/2007 21:09

how can i handle events for "User Information" list?


Hi,
I am just associating a custom Event handler feature with SharePoint "User

Information" list, for creating a copy of newly added user in another sharepoint

custom list with the help of "ItemAdded" event handler, which will maintain user

profile but it doesn't seems to fire ItemAdding event with "User Information" list.
I have read this article: "http://msdn.microsoft.com/en-us/library/aa979520.aspx".
So, can you please suggest me that how can i handle events for "User Information" list?

Thank you,
Saurabh Kumar Singh
at 15/05/2009 23:02
Captcha

Enter the code shown above: *

(Note: If you cannot read the numbers in the above
image, reload the page to generate a new one.)