SharePoint Remote Event Receiver - ItemAttachmentAdding not providing proper count value

%3CLINGO-SUB%20id%3D%22lingo-sub-1779141%22%20slang%3D%22en-US%22%3ESharePoint%20Remote%20Event%20Receiver%20-%20ItemAttachmentAdding%20not%20providing%20proper%20count%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1779141%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20SharePoint%20Online%20List%2C%20our%20requirement%20is%20to%20restrict%20the%20user%20to%20upload%20more%20than%201%20attachment%20hence%20ewe%20implemented%20an%20RER.%3C%2FP%3E%3CP%3ENow%20after%20implementing%20ItemAttachmentAdding%20the%20proper%20count%20of%20the%20attachments%20is%20not%20coming%20properly.%3C%2FP%3E%3CP%3EAny%20other%20alternative%20to%20get%20the%20attachment%20count%20on%20the%20fly%20%3F%3C%2FP%3E%3CP%3EThanks%20in%20advance.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Ecase%20SPRemoteEventType.ItemAttachmentAdding%3A%0Aresult%20%3D%20new%20SPRemoteEventResult()%3B%0AList%20list%20%3D%20clientContext.Web.Lists.GetById(properties.ItemEventProperties.ListId)%3B%0AListItem%20listItem%20%3D%20list.GetItemById(properties.ItemEventProperties.ListItemId)%3B%0AclientContext.Load(listItem)%3B%0AclientContext.ExecuteQuery()%3B%0AAttachmentCollection%20oAttachments%20%3D%20listItem.AttachmentFiles%3B%20clientContext.Load(oAttachments)%3B%0AclientContext.ExecuteQuery()%3B%0Aif%20(null%20!%3D%20oAttachments)%0A%7B%E2%80%8B%E2%80%8B%E2%80%8B%E2%80%8B%0Aif%20(oAttachments.Count%20%26gt%3B%200)%0A%7B%E2%80%8B%E2%80%8B%E2%80%8B%E2%80%8B%0Aresult.ErrorMessage%20%3D%20string.Format(%22More%20than%20one%20attachment%20is%20not%20allowed%22)%3B%0Aresult.Status%20%3D%20SPRemoteEventServiceStatus.CancelWithError%3B%0A%7D%E2%80%8B%E2%80%8B%E2%80%8B%E2%80%8B%0A%7D%E2%80%8B%E2%80%8B%E2%80%8B%E2%80%8B%0Aelse%0A%7B%E2%80%8B%E2%80%8B%E2%80%8B%E2%80%8B%0Aresult.ErrorMessage%20%3D%20string.Format(%22Something%20went%20wrong.%20Please%20try%20again%22)%3B%0Aresult.Status%20%3D%20SPRemoteEventServiceStatus.CancelWithError%3B%0A%7D%E2%80%8B%E2%80%8B%E2%80%8B%E2%80%8B%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1779141%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
Occasional Contributor

In SharePoint Online List, our requirement is to restrict the user to upload more than 1 attachment hence ewe implemented an RER.

Now after implementing ItemAttachmentAdding the proper count of the attachments is not coming properly.

Any other alternative to get the attachment count on the fly ?

Thanks in advance.

case SPRemoteEventType.ItemAttachmentAdding:
result = new SPRemoteEventResult();
List list = clientContext.Web.Lists.GetById(properties.ItemEventProperties.ListId);
ListItem listItem = list.GetItemById(properties.ItemEventProperties.ListItemId);
clientContext.Load(listItem);
clientContext.ExecuteQuery();
AttachmentCollection oAttachments = listItem.AttachmentFiles; clientContext.Load(oAttachments);
clientContext.ExecuteQuery();
if (null != oAttachments)
{​​​​
if (oAttachments.Count > 0)
{​​​​
result.ErrorMessage = string.Format("More than one attachment is not allowed");
result.Status = SPRemoteEventServiceStatus.CancelWithError;
}​​​​
}​​​​
else
{​​​​
result.ErrorMessage = string.Format("Something went wrong. Please try again");
result.Status = SPRemoteEventServiceStatus.CancelWithError;
}​​​​
0 Replies