0

Here is my ItemAdded function inside my event receiver that detects if the item is a duplicate entry:

public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);
        if (properties.ListTitle.Equals("Runner"))
        {
            try
            {
                using (SPSite thisSite = new SPSite(properties.WebUrl))
                {
                    SPWeb thisWeb = thisSite.OpenWeb();
                    SPList list = thisWeb.Lists[properties.ListId];
                    var name = properties.AfterProperties["Name1"].ToString();
                    var nameFieldValue = new SPFieldLookupValue(name);
                    var nameId = nameFieldValue.LookupId;

                    var round = properties.AfterProperties["Round"].ToString();
                    var roundFieldValue = new SPFieldLookupValue(round);
                    var roundId = roundFieldValue.LookupId;

                    SPQuery query = new SPQuery();
                    query.Query = "<Where><And><Eq><FieldRef Name='Name1' LookupId='TRUE' /><Value Type='User'>" + Convert.ToString(nameId) + "</Value></Eq><Eq><FieldRef Name='Round' LookupId='TRUE' /><Value Type='Lookup'>" + Convert.ToString(roundId) + "</Value></Eq></And></Where>";
                    SPListItemCollection listItem = list.GetItems(query);
                    if (listItem.Count > 1)
                    {
                        properties.ErrorMessage = "Custom Error Message";
                        properties.Status = SPEventReceiverStatus.CancelWithError;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new SPException(ex.Message.ToString());
            }
        }
    }

I know for a fact it hits properties.ErrorMessage... when there is a duplicate entry. However, the addition of the item is not cancelled and the item is added per normal. I have tried creating this function using ItemAdding but getting the ID of the user returns a negative number. Any help would be appreciated. Cheers.

EDIT: I have not included my ItemUpdated code here.

1 Answer 1

3

The ItemAdded is ran after the item has been added (hence its name). Therefore, if you cancel the item has already been added to the list and will not be removed.

You should try to use the Adding for this, figuring out your original problem

1
  • You are right. Cheers for the advice.
    – Mike
    Sep 9, 2014 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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