2

I wrote some code to update a file in My SharePoint library using SharePoint-rest-Api and sp-request.
I tried to replicate this example from msdn pages, and it works.

return spr.post('http://vm2008sharepo/_api/web/GetFileByServerRelativeUrl(\'/Documents/MyDoc.docx\')/$value', {
                body: file,
                json: false,
                headers: {
                    'X-RequestDigest': digest,
                    'X-HTTP-Method': 'PUT',
                    'IF-MATCH': '*',

                }
            });

the problem is that when it updates a file the version does not change. So I tried these adding parameters in the post, but no luck :

UIVersionLabel: '0.2',
UIVersion: 2,

I also tried to use POST, MERGE and PATCH instead of PUT but all I got is exceptions.
I suspect that this web service does not undate the file (since it uses PUT) but changes it's content, so the version does not change.

Is it possible to change the version when updating or is there an other web service to use in order to do so ?

3

Normally if versioning enabled, you need to do the following:

  1. Check out
  2. Upload
  3. Check in

Check out:

sprequest.post("https://my_sharepoint/sites/dev/_api/web/GetFileByServerRelativeUrl(@FileUrl)/CheckOut()?@FileUrl='" + encodeURIComponent(fileServerRelativeUrl) + "'", {
    headers: {
      'X-RequestDigest': digest
    }
 });  

Upload:

sprequest.post('https://my_sharepoint/sites/dev/_api/web/GetFolderByServerRelativeUrl(@FolderName)/Files/add(url=@FileName,overwrite=true)' +
            ("?@FolderName='" + encodeURIComponent(folder) + "'&@FileName='" + encodeURIComponent(fileName) + "'"), {
    headers: {
        'X-RequestDigest': digest
    },
    body: fileContent,
    json: false
});  

Check in:

sprequest.post('https://my_sharepoint/sites/dev/_api/web/GetFileByServerRelativeUrl(@FileUrl)/CheckIn(comment=@Comment,checkintype=@Type)' +
            ("?@FileUrl='" + encodeURIComponent(fileServerRelativeUrl) + "'&@Comment='" + (checkinMessage) + "'") +
            ("&@Type='" + checkinType + "'"), {
    headers: {
        'X-RequestDigest': digest
    }
});   

CheckInType is a number:

  • MinorCheckIn: 0
  • MajorCheckIn: 1
  • OverwriteCheckIn: 2
2
  • Thanks for the answer @Kai that was it. One thing though the execution of these functions has to be sequential. I had to force them using promises.
    – Ali
    Nov 25 '16 at 10:53
  • Right, absolutely true Nov 25 '16 at 10:58
1

Make your request as below:

http://vm2008sharepo/_api/web/GetFileByServerRelativeUrl('/Documents/MyDoc.docx')/CheckIn(comment='Comment', checkintype=0)
0
0

In case of "OverwriteCheckIn: 2" there is no new version created.

MinorCheckIn: 0 -> version 1.0, 1.1, 1.2, ...

MajorCheckIn: 1 -> version 1.0, 2.0, 3.0, ...

OverwriteCheckIn: 2 -> version 1.0, 1.0, 1,0 ...

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.