0

I'm trying to update file metadata in document library using rest api. I'm getting Success alert but file's Title is not getting modified. below is code,

 function updateFileMetadata() {
        var def = jQuery.Deferred();
        var restSource=serverUrl+ "/_api/Web/Lists/getByTitle('Documents')/Items(1)";        
        var itemPayload = {"__metadata": {"type":"SP.Data.Shared_x0020_DocumentsItem"},"Title":"Updated"};
        var dfd = jQuery.Deferred();
        $.ajax(
        {
            url: restSource,
            method: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(itemPayload),
            headers:
                {
                    "Accept": "application/json;odata=verbose",
                    "X-RequestDigest": $('#__REQUESTDIGEST').val(),
                    "X-HTTP-Method":"MERGE",
                    "If-Match": "*"

                },
            success: function (data) {
                alert("Success");
                dfd.resolve(data);
            },
            error: function (err) {
                dfd.reject(err);
                alert("Error:"+itmID);
            }
        });
        return dfd.promise();
    }

Where I'm doing wrong? Please help.

  • Can you share your library where you are updating the metadata. – Sunil Sahu Sep 12 '18 at 13:26
0

Hello Updated script and its working, just check for the library name "Documents" and the {"type":"SP.Data.Shared_x0020_DocumentsItem"} in the script.

function updateFileMetadata() {

    var def = jQuery.Deferred();
    var restSource=_spPageContextInfo.webAbsoluteUrl+ "/_api/Web/Lists/getByTitle('Documents')/Items(1)";        
    var itemPayload = {"__metadata": {"type":"SP.Data.DocumentsItem"},"Title":"Updated"};
    var dfd = jQuery.Deferred();
    jQuery.ajax(
    {
        url: restSource,
        method: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(itemPayload),
        headers:
            {
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest": jQuery('#__REQUESTDIGEST').val(),
                "X-HTTP-Method":"MERGE",
                "If-Match": "*"

            },
        success: function (data) {
            alert("Success");
            dfd.resolve(data);
        },
        error: function (err) {

            dfd.reject(err);
            alert("Error:");
        }
    });
    return dfd.promise();
}
0

Tested the snippet in my side and it can update the library Title field:

<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script type="text/javascript">
updateFileMetadata();
function updateFileMetadata() {
        var def = jQuery.Deferred();
        var restSource= "http://sp/_api/Web/Lists/getByTitle('Documents')/Items(11)";        
        var itemPayload = {"__metadata": {"type":"SP.Data.Shared_x0020_DocumentsItem"},"Title":"Updated"};
        var dfd = jQuery.Deferred();
        $.ajax(
        {
            url: restSource,
            method: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(itemPayload),
            headers:
                {
                    "Accept": "application/json;odata=verbose",
                    "X-RequestDigest": $('#__REQUESTDIGEST').val(),
                    "X-HTTP-Method":"MERGE",
                    "If-Match": "*"

                },
            success: function (data) {
                alert("Success");
                dfd.resolve(data);
            },
            error: function (err) {
                dfd.reject(err);
                alert("Error");
            }
        });
        return dfd.promise();
    }
</script>

Check if the item type is valid with this endpoint:

http://sp/_api/Web/Lists/getByTitle('Documents')/Items(11)

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.