How to Checkin and CheckOut in SharePoint 2013 using Javascript

Sathish Nadarajan
 
Solution Architect
March 20, 2015
 
Rate this article
 
Views
24796

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.

Category : JavaScript, SharePoint

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment