远程事件接收器-如何在更新或添加列表项之前获取列表项的附件

问题描述

我想添加限制,以仅在列表项中上传一个附件。我实现了attachmentadding事件,但是如果用户删除第二个附件,则无法获得最新的附件计数。

解决方法

您可以使用以下CSOM代码检查附件计数:

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