5

How to programmatically publish the main version of the document through Javascript?enter image description here

6

You can use the SP.File.publish() method present in sp.js to publish the major version of the document.

Reference

Sample Code :

<script language="javascript" type="text/javascript"
src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
 SP.SOD.executeFunc('sp.js', 'SP.ClientContext',publishFile);
});
 var oFile;
 function publishFile() {
  //Get client context and web
 clientContext = new SP.ClientContext();
 var oWeb = clientContext.get_web();
 //Get List and File object
 var oList = oWeb.get_lists().getByTitle('Demo Library');
 oFile=oWeb.getFileByServerRelativeUrl('/sites/Playground/Demo
 Library/Document.docx');
  //Publish the file and execute the batch
 oFile.publish();
 clientContext.load(oFile);
  clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
 }
  function QuerySuccess() {
   //Get major version

    var majorVersion =oFile.get_majorVersion();
    console.log("Major Version - "+ majorVersion );
 }
  function QueryFailure(sender,args) {
      console.log('Request failed - '+args.get_message());
  }
</script>

Successful output of the published major version is shown below : enter image description here

If you need more info you can download the JSOM book I have written from here

How to publish the file is present in Page No : 58 .

  • 2
    Are you missing a ctx.executeQueryAsync(...)? – Nils Aug 2 '17 at 8:43
  • Only C#, JS no options...(( – user37251 Aug 2 '17 at 9:58
  • @MikhailZhuykov - Please see the updated answer with output – Priyaranjan KS Aug 2 '17 at 10:10
  • 1
    Awesome ! Great to know it worked ! – Priyaranjan KS Aug 2 '17 at 11:03
3

You can try below code to publish major version:

var library;
var clientContext;
var listItemCol;


function GetFileByFileName(itemName) {
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(
'<View><Query><Where><Contains><FieldRef Name=\'FileLeafRef\'/>' +
'<Value Type=\'Text\'>'+itemName+'</Value></Contains></Where></Query>' +
'<RowLimit>10</RowLimit></View>'
);
listItemCol = library.getItems(camlQuery);
clientContext.load(listItemCol);
clientContext.executeQueryAsync(Function.createDelegate(this, this.updateItemProperties), Function.createDelegate(this, this.genericFail));=
}

function updateItemProperties(sender, args) {
var listItemEnumerator = listItemCol.getEnumerator();
var listItem;
while (listItemEnumerator.moveNext()) {
listItem = listItemEnumerator.get_current();
break;
}
// this is how we set properties on list item representing a file
listItem.set_item('Title', "some file title");
listItem.update();
var spfile = listItem.get_file();
spfile.publish("Published by app");
clientContext.load(listItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.genericSuccess), Function.createDelegate(this, this.genericFail));
}

function genericSuccess(sender, args) {
alert('yay!');
}

function genericFail(sender, args) {
alert('oh no!');
}

Reference:

  • Please check updated answer. – Dikesh Gandhi Aug 2 '17 at 8:00
  • 1
    can you explain your usage of Function.this ? – Nils Aug 2 '17 at 8:42
  • This code isn't work, some errors. – user37251 Aug 2 '17 at 9:02
  • Please check updated answer. – Dikesh Gandhi Aug 2 '17 at 10:38

Your Answer

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