PNP-JS从文件库模板创建文件

时间:2018-08-06 17:01:46

标签: sharepoint sharepoint-online

我需要从文件库创建/添加新文件,然后需要使用唯一ID设置文件名并更新其他文件。我可以毫无问题地更新字段,但找不到创建文件的方法。

我怎么可能实现New =>从模板创建(如下图所示)

enter image description here

我尝试了很多方法,但是没有什么可以满足我的要求。

1

this.web.lists.getByTitle('myFilesLib')。items.add()=>我收到有关需要使用SPFileCollection.Add()的错误

2

this.web.getFolderByServerRelativeUrl(url).files.addTemplateFile =>自定义模板没有任何内容

3

this.web.getFolderByServerRelativeUrl(url).files.add =>我需要提供一个文件。

2 个答案:

答案 0 :(得分:0)

  1. 对于JSOM,您可以尝试以下操作:

    newFile = parentList.get_rootFolder()。get_files()。add(fileCreateInfo);

对于CSOM:

StoredProcedure

以这种方式的某些Wiki:https://social.technet.microsoft.com/wiki/contents/articles/37575.sharepoint-online-working-with-files-inside-document-library-using-jsom.aspx

  1. this.web.getFolderByServerRelativeUrl(url).files.addTemplateFile用于添加页面,因此不适合您的情况。

http://www.ktskumar.com/2016/08/pnp-js-core-create-new-page-sharepoint-library/

  1. PnP JS方法需要一个文件DOM或BLOB对象才能将文件上传到SharePoint。基于这些,我们将尝试两个选项将文件上传到SharePoint库或库中的文件夹:BLOB / FileAPI http://www.ktskumar.com/2016/09/pnp-js-core-upload-file-sharepoint/

Microsoft提供的更多参考资料: https://msdn.microsoft.com/en-us/library/office/dn450841.aspx

答案 1 :(得分:0)

这是当您要将现有ContentType上传到SharePoint上的内容库时使用的示例代码。

const templateUrl = '/sites/App/Template/Forms/Template/test.docx'
const name = 'test.docx'
const depositUrl = '/sites/App/Template'

const web = new Web('http://localhost:8080/sites/App');   // Proxy URL for Dev
web.getFileByServerRelativeUrl(templateUrl)
    .getBuffer()
    .then((templateData: ArrayBuffer) => { 
        web.getFolderByServerRelativeUrl(depositUrl).files.add(name, templateData)); 
});

如果您当前使用SP-Rest-Proxy(SharePoint上的文件已损坏),则会出现问题,但应尽快修复。

如果您在SharePoint上部署应用程序,它将按预期运行。

相关链接:

https://github.com/pnp/pnpjs/issues/196#issuecomment-410908170

https://github.com/koltyakov/sp-rest-proxy/issues/61