Do you use field and list item validation (in 2010)

Updated by Brady Stroud [SSW] 1 year ago. See history

123

<introEmbed
  body={<>
```csharp
class CreateShoppingListHandler : SPItemEventReceiver
{
    public override void ItemAdding(SPItemEventProperties properties)
    {
        float price = 0;
        float cost = 0;

        if(float.TryParse(properties.ListItem.Fields["Price"].ToString(), out price) && float.TryParse(properties.ListItem.Fields["Cost"].ToString(), out cost))
        {
            if(price < cost)
            {
                properties.ErrorMessage = "The cost must not be less than the price";
                properties.Cancel = true;
            }
        }
    }
}
```

**Bad example: using custom code – creating a custom event receiver on the item (the item adding event or item updating event)**

<figureEmbed figureEmbed={{
  preset: "goodExample",
  figure: '![Good example: using no code – just using the field validation on a list. A demo of this from Andrew Connell on http://channel9.msdn.com/learn/courses/SharePoint2010Developer/ListsAndSchemas/FieldandListItemValidation/](/uploads/rules/do-you-use-field-and-list-item-validation-in-2010/ListValidation.jpg?WT.mc_id=DP-MVP-33518)',
  shouldDisplay: true
} } />

  </>}
/>

Acknowledgements

Adam Cogan
Related rules

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star