0

I want to add restriction to upload only one attachment in list item. I implement attachmentadding event, but if user delete the second attachment, I am not able to get the latest attachment count.

user9989023
  • 11
  • 1
  • 4

1 Answers1

0

You can check the attachment count using CSOM code below:

List targetList = clientContext.Web.Lists.GetByTitle("List Name");
ListItem oItem = targetList.GetItemById(1);
 
AttachmentCollection oAttachments = oItem.AttachmentFiles;
clientContext.Load(oAttachments);
clientContext.ExecuteQuery();

Console.WriteLine(oAttachments.Count)

Get All Attachments From List Item in SharePoint using CSOM

Jerry
  • 3,480
  • 1
  • 10
  • 12
  • Actually I wanted to get the latest attachment count before saving the list item i.e. on ItemAdding or ItemUpdating event – user9989023 Oct 12 '20 at 04:55
  • In ItemAdding/Updating event, add the code logic like above to check attachment count, you may need to change the Item Id with yours – Jerry Oct 13 '20 at 00:39
  • Yes I tried the same logic in ItemAdding/ItemUpdating event,but no luck this event is also not giving latest count of attachement – user9989023 Oct 14 '20 at 12:02
  • Attachment should be got after ListItem added/updated, as the attachment is based on list item, need to get the list item firstly then attachment. – Jerry Oct 14 '20 at 14:04
  • Yes right, Do you have any alternative option to put validation on list item before adding in list I wanted to check if attachment is missing and it should not be more than one. – user9989023 Oct 15 '20 at 11:43