簡體   English   中英

REST上載到SharePoint 2013聯機文檔庫后獲取項ID

[英]Getting Item ID after REST Upload to SharePoint 2013 Online Document Library

有人可以幫助我連接這些功能之間的點。 我可以上傳,但是如何獲取我剛上傳的文件的ID以更新主機文檔庫中文件的元數據列?

非常感謝!

function uploadDocument(buffer, fileName) {
    var url = String.format("{0}/_api/Web/Lists/getByTitle('Project Documents')/RootFolder/Files/Add(url='{1}', overwrite=true)",
        _spPageContextInfo.webAbsoluteUrl, fileName);

    var call = jQuery.ajax({
        url: url,
        type: "POST",
        data: buffer,
        processData: false,
        headers: {
            Accept: "application/json;odata=verbose",
            "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
            "Content-Length": buffer.byteLength
        }
    });

    return call;
}

function getItem(file) {
    var call = jQuery.ajax({
        url: file.ListItemAllFields.__deferred.uri,
        type: "GET",
        dataType: "json",
        headers: {
            Accept: "application/json;odata=verbose"
        }
    });

    return call;
}

function updateItemFields(item) {
    var now = new Date();
    var call = jQuery.ajax({
        url: _spPageContextInfo.webAbsoluteUrl +
            "/_api/Web/Lists/getByTitle('Project Documents')/Items(" +
            item.Id + ")",
        type: "POST",
        data: JSON.stringify({
            "__metadata": { type: "SP.Data.Project_x0020_DocumentsItem" },
            CoordinatorId: _spPageContextInfo.userId,
            Year: now.getFullYear()
        }),
        headers: {
            Accept: "application/json;odata=verbose",
            "Content-Type": "application/json;odata=verbose",
            "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
            "IF-MATCH": item.__metadata.etag,
            "X-Http-Method": "MERGE"
        }
    });

    return call;
}

將“?$ expand = ListItemAllFields”添加到uploadDocument函數的url中。 所以

var url = String.format("{0}/_api/Web/Lists/getByTitle('Project Documents')/RootFolder/Files/Add(url='{1}', overwrite=true)", _spPageContextInfo.webAbsoluteUrl, fileName);

會變成

var url = String.format("{0}/_api/Web/Lists/getByTitle('Project Documents')/RootFolder/Files/Add(url='{1}', overwrite=true)?$expand=ListItemAllFields", _spPageContextInfo.webAbsoluteUrl, fileName);

在返回的ajax調用的success / complete函數中,您現在應該可以訪問與新創建的文件關聯的listItem的字段。 以下是一個例子。

$(document).ready(function () {
    uploadDocument(toUrl, FileName, binary, function (file) {
        updateItemFields(file.ListItemAllFields, function(){
            alert("Updated Succeeded");
        }, function(){
            alert("Update Failed");
        });
    }, function(error){
            alert(error);
    });
}

function uploadDocument(url, fileName, arrayBuffer, complete, failure) {
    $.ajax({
        url: url + "/_api/web/lists/getByTitle('Project Documents')/RootFolder/Files/Add(url='" + fileName + "', overwrite=true)?$expand=ListItemAllFields",
        type: "POST",
        data: arrayBuffer,
        processData: false,
        headers: {
            "Accept": "application/json; odata=verbose",
            "content-length": arrayBuffer.length,
            "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
        },
        success: function (data) {
                complete(data.d);
        },
        error: function (err) {
            failure(err);
        }
    });
}

function updateItemFields(item, complete, failure) {
    var now = new Date();
    jQuery.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('Project Documents')/Items(" + item.Id + ")",
        type: "POST",
        data: JSON.stringify({
            "__metadata": { type: "SP.Data.Project_x0020_DocumentsItem" },
            CoordinatorId: _spPageContextInfo.userId,
            Year: now.getFullYear()
        }),
        headers: {
            Accept: "application/json;odata=verbose",
            "Content-Type": "application/json;odata=verbose",
            "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
            "IF-MATCH": item.__metadata.etag,
            "X-Http-Method": "MERGE"
        },
        success: function (data) {
            complete(data.d);
        },
        error: function (err) {
            failure(err);
        }
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM