How to Checkin and CheckOut in SharePoint 2013 using Javascript


Sathish Nadarajan
SharePoint MVP
Published On :   20 Mar 2015
Visit Count
Today :  6    Total :   16322
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


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)
           {
               window.location.reload();
           }
           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}');
                 listItem.update();
                
               page.checkIn();
               page.publish();
               ctx.executeQueryAsync(Function.createDelegate(this, checkIn_Success),Function.createDelegate(this, checkIn_fail));
           }
 </script>
 

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();
                 this.file.checkOut();
                 clientContext.load(this.file)
                 clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));            
         }
 
 

Happy Coding.

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Migratiin Tools for SharePoint