I've been looking for a way to detect changes on the attachments of an item using synchronous event receivers in SharePoint 2013 developed in C#.
The ItemAdding event is not relevant as it's not a problem if attachments are uploaded at the same moment the item is created, however, the event ItemUpdating is more relevant. Indeed, I would like to be able to update another field if one (or more) attachment is added or deleted during the synchronous event only. Asynchronous events are not an option.
I tried the solution provided here without any luck :
- When an attachment gets added, Request.Files.Count is higher or equal to 1
- When an attachment gets removed, it's equal to 1 if other attachments still exist on the item
- When no changes are done, it's still equal to 1 if other attachments exist on the item.
Would you have any ideas how I can do it ?
Thank you in advance,
Kevin
EDIT 19 APRIL : After further investigation, I detected that an empty file was always sent in the Request.File, so I know now the reason why Request.File.Count was always at least equal to 1.
With a small piece of code added, I can detect one or more valid files are being added :
int attachCount = 0;
HttpFileCollection fileCollection = curContext.Request.Files;
for (int j = 0; j < fileCollection.Count; j++)
{
HttpPostedFile file = fileCollection[j];
if (file.ContentLength > 0)
{
attachCount++;
}
}
Only thing left is how to detect that attachments has been removed when being in the ItemUpdating event, any ideas ?
Thank you in advance