0

I have list event receiver which creates item in another list.

Code:

newItem[fieldName1] = cellItem1;
newItem[fieldName2] = cellItem2;
newItem.Update();

The problem is that if any required field is not specified by me, the element will be saved. For example: if the "Title" field is required, but I don't specify newItem["Title"] = cellItem1 - it will still be saved.

I need to prevent this - If a list has required fields and I do not specify their values - Item should not be created.

Yes I can get all list fields and create additional check - If (list.fields[field].Required)..... but may be there are other ways to achieve this?

1 Answer 1

2

After research I can write that there are no OOTB settings to achieve this.

So the simpliest solution is to add the following check:

foreach (SPField f in properties.ListItem.Fields)
{
   if (!f.ReadOnlyField && f.Required && properties.AfterProperties[f.InternalName] == null)
    {
      //cancel adding item
    }
}

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.