C# – validation for custom list sharepoint

csharepoint

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

Best Answer

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";

        }
    }


}
Related Topic