3

I need to update the version number of a document in a document library in SharePoint Online. I used postman to test this. Here is the POST Request I used.

https://MySharePointServer/sites/MySite/_api/web/getfilebyserverrelativeurl('/sites/MySite/JobApplications/Application001.docx')/ListItemAllFields

Request Headers

Accept          : application/json;odata=verbose
Authorization   : 'My Authorization Code Here'
X-HTTP-Method   : MERGE
If-Match        : *
Content-Type    : application/json;odata=verbose
X-RequestDigest : 'MyRequestDigestCodeHere'

Request Body

{ '__metadata': { 'type': 'SP.Data.JobApplicationsItem' }, 'OData__UIVersionString': '4.0' }

But when I send this request I always get the following response.

{
"error": {
"code": "-2147024891, System.UnauthorizedAccessException",
"message": {
  "lang": "en-US",
  "value": "Access denied. You do not have permission to perform this action or access this resource."
  }
 }
}

Do you have any idea what I'm doing wrong here? Any suggestions would be highly appreciated.

  • 1
    You are missing request digest in the header – Atish Dipongkor - MVP Dec 30 '15 at 12:47
  • Sorry, I missed it in my question. I already added that in my request. – Jake Dec 30 '15 at 12:52
  • 1
    How did you find that? using _api/contextinfo? – Atish Dipongkor - MVP Dec 30 '15 at 12:53
  • Yes. Using _api/contextinfo – Jake Dec 30 '15 at 12:55
  • SharePoint usually assigns the version number to the item on the server when you update it, why are you trying to manually assign it? – John-M Dec 30 '15 at 13:51
5

You can't update the Version number directly.

To make a major version you have to use the CheckIn method with checkInType=1.

Or depending on your list Publish+Approve

  • '/approve' just updated the minor version to the next major version which was exactly what I was searching for..! Thank you very much. – Jake Dec 30 '15 at 17:11
3

You can't set the version number manually (even with the server object model you can only 'get' the version property, no setter identified); just doing a major version check-in should accomplish what you're looking for and let SharePoint figure out what version number to provide though:

function majorCheckInAsync(fileUrl, comment) {
    var dfd = $.Deferred();

    $.ajax({
        url: "/_api/web/GetFileByServerRelativeUrl('" + fileUrl + "')/CheckOut()",
        type: "POST",
        headers:  {
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        }
    }).fail(failHandler).then(function() {
        $.ajax({
            url: "/_api/web/GetFileByServerRelativeUrl('" + fileUrl + "')/CheckIn(comment='" + comment +"', checkintype=1)",
            type: "POST",
            headers:  {
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            }
        }).fail(failHandler).then(function(data) {
            dfd.resolve(data);
        });
    });

    return dfd.promise();

    function failHandler(jqXHR, textStatus, errorThrown) {
            dfd.reject(jqXHR, textStatus, errorThrown);
    }
}

Call this with something like:

majorCheckInAsync("/Shared%20Documents/my%20new%20file.txt", "Checkin from REST")
    .then(function(data) { console.log("item checked in") });

Note the checkintype in my REST url is set to 1 -- this corresponds to a MajorCheckIn in the SP.CheckInType enumeration:

checkInType

Type: Int32

The SP.CheckinType for the file: MinorCheckIn = 0; MajorCheckIn = 1; OverwriteCheckIn = 2.

Doing a major check in is what will give us the x.0 major version number.

  • Thanks for your answer John. I tried your approach. But the document should be in 'checked-out' before we do this. – Jake Dec 30 '15 at 16:42
  • 1
    @JanakaLP if the document is already checked out you can just skip the CheckOut endpoint call and proceed directly to CheckIn, the technique would still be the same - and I realize now you didn't even ask in the context of JavaScript but the approach should be similar – John-M Dec 30 '15 at 16:53
  • Thank you very much John. Just replaced the '/CheckIn' in the end of the URL with '/Approve' and it updated the minor version to the next major version successfully..! Thanks again. – Jake Dec 30 '15 at 17:03

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.