Sharepoint使用新的库详细信息将Excel文件从库复制到另一个库

时间:2018-07-10 19:54:57

标签: javascript rest sharepoint xmlhttprequest sharepoint-online

我将文件模板存储在文档库中。我正在通过表单收集元数据,并且在提交表单时,我想将模板之一与用户指定的元数据复制到另一个文档库中。

我尝试使用createNewDocumentWithProgID,但是我不知道如何将新的元数据与项目一起传递。

我正在将文件读入arraybuffer,然后通过rest上传...

function createAPinLibrary(name,system,competence,manpower,start,duration) {

    var srcURL = "https://example.com/teams/Library/_api/web/GetFileByServerRelativeUrl('/teams/site/library/file.xlsm')";
    var fileName = name + ".xlsm";
    var destURL = "https://example.com/teams/Library/_api/web/lists/getbytitle('Library Name')/rootfolder/files/add(url='"+fileName+"',overwrite=true)";

    try {
        if (srcURL) {
            var xhr = new XMLHttpRequest()                
            xhr.onreadystatechange = function (data) {
                if (this.readyState == 4 && this.status == 200) {
                    var fileData = this.response;

                    console.log("XHR output...");
                    console.log(data);

                    var u8 = new Uint8Array(this.response);
                    var b64encoded = btoa(String.fromCharCode.apply(null, u8)); 

                    uploadFile(fileData);
                }
            }
            xhr.open('GET', srcURL, true);
            xhr.responseType = 'arraybuffer';
            xhr.send();
        }
    } catch (e) {
        console.log(e);
    }

    function uploadFile(uploadFileObj) {
        var webUrl = _spPageContextInfo.webAbsoluteUrl;
        var documentLibrary = "Example_Library";
        var folderName = "RootFolder";

        uploadFileToFolder(uploadFileObj, function (data) {
            var file = data.d;
            console.log("File...");
            console.log(file);

            var details = {
                __metadata: {
                    type: file.__metadata.type
                },
                System: system,
                Competence: competence
            };

            generatedDestURL = webUrl + "/_api/Web/lists/getbytitle('"+documentLibrary+"')/items(" + file.__metadata.id + ")";

            updateFileMetadata(generatedDestURL, details, file, function (data) {
                alert("File uploaded & meta data updating done successfully");
            }, function (data) {
                alert("File upload done but meta data updating FAILED");
                console.log(data);
            });
        }, function (data) {
            alert("File uploading and meta data updating FAILED");
            console.log(data);
        });
    }

    function uploadFileToFolder(arrayBuffer, success, failure) {
        $.ajax({
            url: destURL,
            type: "POST",
            data: arrayBuffer,
            processData: false,
            async: false,
            headers: {
                "accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            },
            success: function (data) {
                success(data);
            },
            error: function (data) {
                failure(data);
            }
        });
    }

    function updateFileMetadata(generatedDestURL, details, file, success, failure) {
        $.ajax({
            url: generatedDestURL,
            type: "POST",
            async: false,
            data: JSON.stringify(details),
            headers: {
                "accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "Content-Type": "application/json;odata=verbose",
                "X-Http-Method": "MERGE",
                "IF-MATCH": file.__metadata.ETag,
            },
            success: function (data) {
                success(data);
            },
            error: function (data) {
                failure(data);
            }
        });
    }
}

有人可以帮我解决这个问题吗?我只能找到用户从本地计算机上载时的示例,但我想从文档库中获取文件。

0 个答案:

没有答案