tags:

views:

173

answers:

2

I have created a custom list as a feature in sharepoint.

i need to perform some validation on some of the fields. ive created a clss that inherits from SPItemEventReceiver

and need to implement the method:

public override void ItemAdding(SPItemEventProperties properties)

where do i take it from here? how do i access list items etc...

thanks

+1  A: 

There are a lot of samples on this out there. For example, this one.

It validates the Email column using this code snippet:

public override void ItemAdding(SPItemEventProperties properties)
{
    base.ItemAdding(properties);

    // only perform if we have an Email column
    if (properties.AfterProperties["Email"] != null)
    {
        // test to see if the email is valid
        if (!IsValidEmailAddress(properties.AfterProperties["Email"].ToString()))
        {
            // email validation failed, so display an error
            properties.Status = SPEventReceiverStatus.CancelWithError;
            properties.Cancel = true;
            properties.ErrorMessage = "Please enter a valid email address";

        }
    }


}
Magnus Johansson
A: 

Here is a good option:

Regular expressions field validation in SharePoint .. www.codeplex.com/SharePointRegEx

Its a custom field that allows adding a regular expression to validate the values entered by user. It also has an "error message" field.