2

I need to validate a string that user submit against SPField.ValidationFormula while adding new record to custom list in c#.

Line to add new record is

SPListItem item = mylist.Items.Add();

I have formula in

column.ValidationFormula

and my string to validate is in this variable

mystringtovalidate

How can I validate mystringtovalidate against column.ValidationFormula then if is valid I'll do item.Update();

1 Answer 1

2

You can use following pattern

try {
    item.Update();
}
catch(SPListDataValidationException ex) {
    //ex.Message
}

https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistdatavalidationexception_members(v=office.14).aspx

6
  • is there a manual way to validate it? reason is I don't want to update it yet since I check many other things that sharepoint does not then update my record. If I do this, it will add record with several empty fields when current column formula validates.
    – Ergec
    Commented Jan 28, 2015 at 14:23
  • in that case you should perform your validation first then do the item.update(). Else I think you should end up parsing the Formula & then validating. Commented Jan 28, 2015 at 14:24
  • is it possible to catch errors for more than one field? let say I have 3 columns with different validationformula. will this try catch give me all 3 errors so I can use them? I'm new to all this c# and sharepoint so sorry :/
    – Ergec
    Commented Jan 28, 2015 at 14:26
  • I think so, you can check the Exception Message Property. You can test it and confirm. Commented Jan 28, 2015 at 14:27
  • I think it will give the first validation error it encounters not all of them. Commented Jan 28, 2015 at 14:28

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.