How to Checkin and CheckOut in SharePoint 2013 using Javascript

Sathish Nadarajan
SharePoint MVP
Published On :   20 Mar 2015
Visit Count
Today :  1    Total :   13386

In one of the requirement, I met with a strange requirement like, on the Page, there will be a Ribbon Button, on click of that button, the page should get Checked In using Javascript. After the Success of the Checkin, the Page should re-load and make sure that its been checked in.

For that, I used the below piece of code.

 <script type="text/javascript">
 function checkIn_Success(sender, args)
           function checkIn_fail(sender, args)
             alert('Something went wrong');
           function checkIn()
               var ctx = SP.ClientContext.get_current();
               var web = ctx.get_web();
               var page = web.getFileByServerRelativeUrl(window.location.pathname);
                 var listItem = page.get_listItemAllFields();
 //We can set any value to any of the property by the below lines.
                 //listItem.set_item('PublishingPageContent', '{Updated with ECMA}');
               ctx.executeQueryAsync(Function.createDelegate(this, checkIn_Success),Function.createDelegate(this, checkIn_fail));

To checkout, the code piece would like,

 function CheckOutFile() {
             var clientContext = SP.ClientContext.get_current();            
                 var webSite = clientContext.get_web();
                 this.list = webSite.get_lists().getByTitle("Shared Documents");
                 this.item = list.getItemById(1);
                 this.file = this.item.get_file();
                 clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));            

Happy Coding.

Sathish Nadarajan.