如何使用pnpjs从Sharepoint列表中删除项目附件?

时间:2018-08-29 05:10:22

标签: sharepoint vuejs2 sharepoint-2013

您好,我想先从列表项中删除附件,然后使用pnp js(在vuejs中)在sharepoint中上传新的附件文件! 我跟踪代码并删除部分正在敲响,但我不知道为什么附件没有删除!!!

这是我删除附件项的代码

      public async DeleteItemsAttachment(itemId: number): Promise<any> {
        let item = pnp.sp.web.lists.getById('{128EF67A-FDSF-4F42-8E8F-D3FC9523273E}').items.getById(itemId)
        return await item.attachmentFiles.deleteMultiple()
      }

  public async AddanAttachment(itemId: number, fileName: string, arrayBuffer: File): Promise<any> {
    let item = pnp.sp.web.lists.getById('{128EF6AA-FD8F-4F42-8E8F-D3FC9523273E}').items.getById(itemId)
    return await item.attachmentFiles.add(fileName, arrayBuffer)
  }

uploadFile(){
  if (this.itemId != null && this.myfiles) {
        if (this.HasUploadFile) {
          this.spService.DeleteItemsAttachment(this.itemId).then(response => {
            this.AddAnAttachmentToRecord()
          }).catch(e => {
            this.message = ` exception : ${e}`
          })
        }
        else {
          this.AddAnAttachmentToRecord()
        }
}

我该如何解决我的问题? 我的代码的错误区域在哪里?

1 个答案:

答案 0 :(得分:1)

根据attachmentfiles.ts deleteMultiple函数期望附件文件名数组,因此可以通过显式提供文件名来删除附件:

item.attachmentFiles.deleteMultiple("{attachment-file-name-1}","{attachment-file-name-2}")

或(以更动态的方式),通过读取附件名称并在deleteMultiple函数中指定附件名称:

let item = sp.web.lists.getByTitle(listTitle).items.getById(itemId);
//1. get all attachments
let attachments = await item.attachmentFiles.get(); 
let attachmentNames = attachments.map(a => a.FileName);
//2. delete all attachmanents
await item.attachmentFiles.deleteMultiple(...attachmentNames);