Domanda

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 ?

È stato utile?

Soluzione

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

Altri suggerimenti

Make your request as below:

http://vm2008sharepo/_api/web/GetFileByServerRelativeUrl('/Documents/MyDoc.docx')/CheckIn(comment='Comment', checkintype=0)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top