【问题标题】:How to delete items attachement from sharepoint List using pnpjs?如何使用 pnp js 从共享点列表中删除项目附件?
【发布时间】:2018-08-29 19:53:32
【问题描述】:

您好,我尝试先从列表项中删除附加项目,然后使用 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()
        }
}

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

【问题讨论】:

    标签: sharepoint vuejs2 sharepoint-2013


    【解决方案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);
    

    【讨论】:

    • 非常感谢。这对我很有帮助。再次感谢您
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多