7

I have a jquery script that uploads a file to document library.

I'm trying to set a choice column in the same call, but can't seem to get it to work. The upload is working, but Col1 is a choice column and 'Personal Statement' is a valid choice, but it does not take so I end up with the default value.

If I can't do this with one REST call, how do I get the ID of the file just uploaded to do a REST Merge?

reqExecutor.executeAsync({
    url: url,
    method: "POST",
    headers: {
        "Accept": "application/json; odata=verbose",
        "X-RequestDigest": digest
    },
    data: JSON.stringify({ '__metadata': { 'type': 'SP.List', 'co1': 'Personal Statement' }, 'Title': 'New title'  }),


    contentType: "application/json;odata=verbose",
    binaryStringRequestBody: true,
    body: fileData,
    success:successHandler,
    error: errorHandler

1 Answer 1

15

You can't update field values in the same call that uploads the document, you have to do that in a separate call. The response from the call that uploads the document will be a SP.File object representing the file that was just created. This object will have a deffered property called ListItemAllFields, which you can use to get the URL needed to make a call to get the list item associated with the file. Once you have the list item you can then make a call to set the field value.

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;
}

I cover all of this in my SharePoint 2013 Client Object Model and REST API course on Pluralsight. If you don't have a subscription you get can a free trial here so you can watch the clip.

4
  • Thank you! Nice. Wish I could see a complete example somewhere on the full process. I was looking at that course and it looks excellent.
    – Hell.Bent
    Commented Sep 6, 2013 at 14:53
  • The complete solution is shown in the course. It's "Demo: Uploading Documents II (REST)" in the "Working with Documents and Document Libraries" module. Commented Sep 6, 2013 at 15:37
  • Hi @Rob, I am able to upload file and update metadata in two different REST calls but when I see version history it created 2 versions, Is it possible so that it create 1 version only Commented Feb 7, 2018 at 6:59
  • @VishwanathMishra I just found out recently that you can using ValidateUpdateListItem. Here are a couple resources that cover how to use it. sharepointfieldnotes.blogspot.com/2014/04/… and blogs.ibs.com/2016/01/29/… Commented Feb 8, 2018 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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