如何在站点和子站点之间使用Sharepoint 2013中的Rest API和javascript复制文件
我需要在文档库之间复制文件。图书馆A位于一个站点,图书馆B位于子站点。我知道如何在同一级别的库之间复制文件但问题是在不同级别之间复制。
I need to copy file between document libraries. Library A is located in one site and Library B is located in subsite. I know how to copy file between libraries on the same level but the problem is with copying between different level.
我用于在同一级别的库之间复制文件的代码。
The code I use to copy file between libraries on the same level.
$.ajax({
url : "http://xxx/PWA/_api/web/folders/GetByUrl('/PWA/CopyFromLibrary')/Files/getbyurl('Import.csv')/copyTo(strNewUrl = '/PWA/TargetLibrary/Import.csv',bOverWrite = true)",
method: 'POST',
headers: {
"Accept": "application/json; odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function () {
alert("Success! Your file was copied properly");
},
error: function () {
alert("Problem with copying");
}
});
对于不同级别,我只使用另一个目标网址:
For different level I use just another target URL:
url : "http://xxx/PWA/_api/web/folders/GetByUrl('/PWA/CopyFromLibrary')/Files/getbyurl('Import.csv')/copyTo(strNewUrl = '/PWA/Subsite/TargetLibrary/Import.csv',bOverWrite = true)",
它不起作用。
如何解决这个问题?
And it doesn't work. How to work around this problem?
今天刚刚想出这个问题来解决这个问题。诀窍是 - 不要使用$ .ajax下载文档。使用好的旧XMLHttpRequest。原因是JQuery根本不允许您从SharePoint获取原始二进制数据数组。但是,XMLHttpRequest之所以这样做是因为它允许你将一个arraybuffer作为其实现的一部分,这是SharePoint接受的!
Just figured this one out today for the cross site solution. The trick is-- don't use $.ajax for the download of the document. Use good old XMLHttpRequest. The reason is that JQuery simply doesn't let you get a raw binary data array from SharePoint. But, the XMLHttpRequest does because it allows you to get an arraybuffer as part of its implementation, which SharePoint accepts!
以下是代码,其中包含用于构建完整的源和目标REST URL。请注意,您可以使用$ .ajax上传文件。
The following is the code with the parts identified for building the full source and target REST urls. Note that you can use $.ajax to upload the file.
- sourceSite 是一个适合追加的sharepoint网站'_api'休息端点
- sourceFolderPath 是文档所在的相对文件夹路径
- sourceFileName 是文档的文件名
- targetSite , targetFolderPath , targetFileName 是镜像图片或来源,仅适用于目的地。
-
requestDigest 是SharePoint接受更新所需的特殊值。
- sourceSite is a sharepoint site suitable for appending the '_api' rest endpoint
- sourceFolderPath is the relative folder path your document is located within
- sourceFileName is the filename of the document
- targetSite, targetFolderPath and targetFileName are the mirror images or source, only for the destination.
requestDigest is that special value you need for SharePoint to accept updates.
function copyDocument(sourceSite, sourceFolderPath, sourceFileName, targetSite, targetFolderPath, targetFileName, requestDigest) {
var sourceSiteUrl = sourceSite + "_api/web/GetFolderByServerRelativeUrl('" + sourceFolderPath + "')/Files('" + sourceFileName + "')/$value";
var targetSiteUrl = targetSite + "_api/web/GetFolderByServerRelativeUrl('" + targetFolderPath + "')/Files/Add(url='" + targetFileName + "',overwrite=true)";
var xhr = new XMLHttpRequest();
xhr.open('GET', sourceSiteUrl, true);
xhr.setRequestHeader('binaryStringResponseBody', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function (e) {
if (this.status == 200) {
var arrayBuffer = this.response;
$.ajax({
url: targetSiteUrl,
method: 'POST',
data: arrayBuffer,
processData: false,
headers: { 'binaryStringRequestBody': 'true', 'Accept': 'application/json;odata=verbose;charset=utf-8', 'X-RequestDigest': requestDigest }
})
.done(function (postData) {
console.log('we did it!');
})
.fail(function (jqXHR, errorText) {
console.log('dadgummit');
});
}
}
xhr.send();
}