"格式不正确的JSON流”;当从SharePoint 2013 REST API对文件使用moveto或cop

"Not well formed JSON stream" when using moveto or copyto on file from SharePoint 2013 REST API

本文关键字:文件 API REST cop moveto 2013 SharePoint 不正确 格式 quot JSON      更新时间:2023-09-26

我正在尝试对sharepoint 2013文档库中的文件启用文件移动和复制操作。我有列表和删除操作,但当我尝试调用/moveto(newfile=…,flags=1)或/copyto(strnewfile,boverwrite=true)时,我会收到一个错误"格式不正确的JSON流"。

我尝试过带有和不带有站点前缀的newurl(或strnewurl)(例如/sites/dev)。我已经单独验证了getfilebyserverrelativeurl实际上正在返回该文件。

以前有人碰到过这个吗?

function copyFile() {
    var executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync({
        url: appweburl + "/_api/SP.AppContextSite(@target)/web" +
            "/getfilebyserverrelativeurl('/sites/dev/Sample1/Customer.dim')" +
            "/copyto(strnewurl='/Sample1/filename.docx',boverwrite=false)" +
            "?@target='" + hostweburl + "'",
        method: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "IF-MATCH": "*"
        },
        success: function (data) {
            alert(JSON.stringify(data));
        },
        error: errorHandler
    });
}
function moveFile() {
    var executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync({
        url: appweburl + "/_api/SP.AppContextSite(@target)/web" +
            "/getfilebyserverrelativeurl('/sites/dev/Sample1/Customer.dim')" +
            "/moveto(newurl='/Sample1/filename.docx',flags=1)" +
            "?@target='" + hostweburl + "'",
        method: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "IF-MATCH": "*",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
            alert(JSON.stringify(data));
        },
        error: errorHandler
    });
}

我刚刚遇到了同样的问题。我的解决方案是使用ajax而不是执行器:

$.ajax({
  url: SPAppWebUrl + "/_api/SP.AppContextSite(@target)/web/getfilebyserverrelativeurl('" + file + "')/copyto(strnewurl='" + target + "',boverwrite=false)?@target='" + hostweburl + "'",
  type: "POST",
  dataType: 'json',
  headers: {
    "Accept": "application/json; odata=verbose",
    "content-type": "application/json; odata=verbose",
    "IF-MATCH": "*",
    "X-RequestDigest": $('#__REQUESTDIGEST').val()
  },
  success: successHandler,
  error: errorHandler
});

奇怪的是,当我在RequestExecutor中使用相同的方法时,它不起作用。

我发现copytoURL参数需要是相对于服务器的,而不是相对于站点的。所以strnewurl='/sites/dev/Sample1/filename.docx'而不是strnewurl='/Sample1/filename.docx'

我知道所有的文档都说这是相对于站点的,但这对我来说是有效的。我也只包括accept头。

Sharepoint在线工作url/_api/web/getFileByServerRelativeUrl('/sites/test/Template.xlsx')/copyTo(strNewUrl='/sites/test/Exports/Template_copy_5.xlsx',bOverWrite=true)