I would expect something like this to work but the ListItem, BeforeProperties, AfterProperties are all null/empty.
I need the file name and file content.
public class MyItemEventReceiver : SPItemEventReceiver {
public MyItemEventReceiver() {}
public override void ItemAdding(SPItemEventProperties properties) {
SPListItem item = properties.ListItem;
bool fail = item.File.Name.Equals("fail.txt");
if (fail) {
properties.ErrorMessage = "The file failed validation";
properties.Cancel = true;
}
}
}
I can’t use ItemAdded as it is asynchronous and I need to be synchronous, I may prevent the upload and display a message to the user.
Any suggestions would be appreciated. For example, is it possible to override the Upload.aspx?
You can use the HttpContext to retrieve the HttpFileCollection which should contain the uploaded files. This will only work for individual file uploads through the web UI. Doing multiple file uploads, or saving directly from Office won’t create an HttpContext. Try something like this: