簡體   English   中英

共享點錯誤“通過REST根據內容類型添加空白項目時,“將項目添加到文檔庫,請使用SPFileCollection.Add()”

[英]Sharepoint error 'To add an item to a document library, use SPFileCollection.Add()' when adding blank item based on content type via REST

我試圖將一個項目添加到文檔庫中,該項目是文檔庫中內容類型的空白版本。 本質上,我將復制內容類型,並使用名稱將其扔到文檔庫中,但不更改內容。 我收到此錯誤...

在此處輸入圖片說明

樂碼...

// Submit add action form
submitAddActionForm = function() {
    // Get submitted values
    var aptype = document.getElementById('form-action-aptype').value;
    var system = document.getElementById('form-action-system').value;
    var name = document.getElementById('form-action-name').value + ".xlsx";
    var start = document.getElementById('form-action-start').value;
    var duration = parseInt(document.getElementById('form-action-duration').value);
    var end = new Date(start).addHours(duration);
    var className = 'red';
    var shape = 'range';

    // Add item to sharepoint
    createAPinSPList(aptype,name,system,start,duration);

    // Add item to timeline
    addToTimeline(system,name,start,end,className,type,shape);

    // Remove form and grayout
    $('#form-action, #gray-out').remove();

    // Log change
    console.log("New Item:");
    console.log(aptype,system,name,start,end,className,shape);
}

// Creates AP in SP List
function createAPinSPList(aptype,actionName,system,start,duration) {

    // vars
    var contentTypeId;

    // designate content type
    switch (aptype) {
        case "SCN":
            contentTypeId = '0x0101003C20115C4BCA9548B977850AFE3B786E030043111C2A772F8C48B261CEF7F48FF87D';
            break;

        case "SRC":
            contentTypeId = '0x0101003C20115C4BCA9548B977850AFE3B786E010026F40826B07DEB48A77D5EC704A27343';
            break;

        case "DL":
            contentTypeId = '0x0101003C20115C4BCA9548B977850AFE3B786E0200E9BCBF0AE424FC4AA04F78ECC2569BDF';
            break;

        default:
            break;
    }

    // create metadata
    var data = { 
        __metadata: {'type': 'SP.Data.NXE_x0020_AP_x0020_TrackerItem'},
        Title: actionName,
        FileLeafRef: actionName,
        Action: actionName,
        System: system,
        Planned_x0020_Start: start,
        Required_x0020_Time_x0020__x0028_hrs_x0029_: duration,
        ContentTypeId: contentTypeId        
    };

    // create item in SP list
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('NXE Action Plan Tracker')/items",
        type: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "content-Type": "application/json;odata=verbose"
        },
        data: JSON.stringify(data),
        success: function (data) {
            console.log("Created successfully!")
        },
        error: function (error) {
            alert("cannot add for some reason");
            alert(JSON.stringify(error));
        }
    }); 
}

我有一個表單,當用戶提交一些信息時會調用“ submitAddActionForm”。

有些字段不是我必須與__metadata並行傳遞的,但我認為這不是問題所在。 另外,vis.js時間軸還有一些額外的代碼,希望不會造成混淆。

任何幫助深表感謝。 我找不到具有這種特性的任何資源。

要將文件添加到文檔庫,請使用以下端點:

/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files/Add(url='file name', overwrite=true)
    method: POST
    body: contents of binary file
    headers:
        Authorization: "Bearer " + accessToken
        X-RequestDigest: form digest value
        content-type: "application/json;odata=verbose"
        content-length:length of post body

要么

/_api/web/lists/getbytitle('Documents')/rootfolder/files/add(url='file name',overwrite=true)

您可以檢查Steve Curran共享的示例代碼。

暫無
暫無

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

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