如何根据文件名获取文档库中新上传文件的ID? PNP JS

时间:2019-04-07 03:39:10

标签: sharepoint spfx

在获取我刚刚在文档库中上传的文件的ID时遇到问题。谁能帮我检查我使用的pnp-js函数是否正确?

  success: function (file) {
    let web: Web = new Web(_context.pageContext.web.absoluteUrl);

    console.log("FileUpload success")
    web.lists.getById(_listName).rootFolder.files.getByName(file.name).get().then(t => {
      //add your code here if you want to do more after deleting the file
      console.log(t);
    });
  }
};

2 个答案:

答案 0 :(得分:0)

You can get the ID of the recently uploaded document as below:

let web: Web = new Web(_context.pageContext.web.absoluteUrl);
// change the path as per your requirement
web.getFolderByServerRelativeUrl("/sites/dev/Shared%20Documents/test/").
files.add(file.name, file, true).then(f => {

    console.log("FileUpload success");

    f.file.getItem().then(item => {    
        console.log(item.ID);
    });
});

Reference - Working With: Files

答案 1 :(得分:0)

我可以使用以下内容找到我刚刚上传的文件的ID:

  success: function () {
    let web: Web = new Web(_context.pageContext.web.absoluteUrl);
    web.lists.getById(_listName).items.orderBy('Id', false).top(1).get().then((items: any[]) => {
      item = items[0].Id;
    });