无法使用sp pnp js从SharePoint其他网站文档库复制文件



我需要将文件从一个SharePoint在线网站文档库复制到同一租户下的另一个SharePoint联机文档库。

我使用了";sp-pnp-js":"3.0.10〃;在我的解决方案中。

下面是我的代码片段,

// let sourceFIleUrl = "https://mytentant.sharepoint.com/contoso1/Shared Documents/folder1/new-file.docx";
// let destinationFileUrl = "https://mytentant.sharepoint.com/contoso2/Shared Documents/folder2/new-file.docx";

// I have tried adding server relative url like below

let sourceFIleUrl = "/contoso1/Shared Documents/folder1/new-file.docx";
let destinationFileUrl = "/contoso2/Shared Documents/folder2/new-file.docx";

sp.web.getFileByServerRelativePath(sourceFIleUrl).copyTo(destinationFileUrl, false).then((res) => {
console.log("Files added", res);
}).catch((err) => { console.log("Error in copy file", err) });

使用上述代码。如果我尝试复制同一网站文档库中的文档,则它工作正常。但是如果我尝试将文档从一个站点复制到另一个站点。它抛出了以下错误,

">服务器相对URL必须以SPWeb.ServerRelativeUrl";

请注意,我使用了";sp-pnp-js":"3.0.10〃;。因此,无法替代copyTo命令。如果我用copyByPath替换copyTo,则PnP在构建解决方案时会抛出错误。

有人能帮我做同样的事吗?

感谢

尝试使用文件/库的服务器相关URL,而不是完整/绝对URL。

例如:

// sourceFIleUrl is a server-relative url of a existing file
const sourceFIleUrl = "/sites/contoso1/Shared Documents/test.docx";
// destinationUrl is a server-relative url of a new file
const destinationUrl = `/sites/contoso2/Shared Documents/new-file.docx`;
await sp.web.getFileByServerRelativePath(sourceFIleUrl).copyByPath(destinationUrl, false, true);

文档:按路径复制文件


类似线程:

  1. REST API错误";服务器相对URL必须以SPWeb开头。ServerRelativeUrl">
  2. GetFolderByServerRelativeUrl Rest API返回服务器相对URL必须以SPWeb.ServerRelativeUrl开头

最新更新