CheckIn File in Document Library Using REST

Develop the project using the following method in the NAPA Tool.

On your Developer Site, open the "Napa" Office 365 Development Tools and then choose Add New Project.

  • Choose the App for SharePoint template, name the project Create Site and then choose the Create button.
  • Replace APP.js with the following source code below.
  • Publish Your App.

Prerequisites

The following are the important steps to be done before creating the app. Specify the permissions that your app needs as in the following. Choose the Properties button at the bottom of the page.
  • In the Properties window, choose Permissions.
  • In the Content category, set Write permissions for the Tenant scope.
  • In the Social category, set Read permissions for the User Profiles scope.
  • Close the Properties window.


Understanding the Code



Source Code
  1. 'use strict';  
  2.   
  3. var hostweburl;  
  4. var appweburl;  
  5.   
  6. // Get the URLs for the app web the host web URL from the query string.  
  7.   
  8. $(document).ready(function() {  
  9.   
  10.     //Get the URI decoded URLs.  
  11.     hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));  
  12.     appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));  
  13.   
  14.     // Resources are in URLs in the form:  
  15.     // web_url/_layouts/15/resource  
  16.   
  17.     // Load the js file and continue to load the page with information about the folders.  
  18.     // SP.RequestExecutor.js to make cross-domain requests  
  19.     $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js", filecheckin);  
  20. });  
  21.   
  22.   
  23. //Retrieve all of the folders from root Site  
  24. function filecheckin()   
  25. {  
  26.     var executor;  
  27.   
  28.     // Initialize the RequestExecutor with the app web URL.  
  29.     executor = new SP.RequestExecutor(appweburl);  
  30.   
  31.     executor = new SP.RequestExecutor(appweburl);  
  32.   
  33.     executor.executeAsync({  
  34.         url: appweburl + "/_api/SP.AppContextSite(@target)/web/getfilebyserverrelativeurl('/sites/apps/Shared Documents/filename1.txt')/checkin(comment='Check-in comment.',checkintype=0)?@target='" + hostweburl + "'",  
  35.         method: "POST",  
  36.   
  37.   
  38.         headers:   
  39.         {  
  40.             "accept""application/json; odata=verbose"  
  41.         },  
  42.         success: function(data)  
  43.         {  
  44.             alert("success:File Checked IN");  
  45.         },  
  46.         error: function(err)   
  47.         {  
  48.             alert("error: " + JSON.stringify(err));  
  49.         }  
  50.     });  
  51. }  
  52.   
  53. //Utilities  
  54. // Retrieve a query string value.  
  55. // For production purposes you may want to use a library to handle the query string.  
  56. function getQueryStringParameter(paramToRetrieve)  
  57. {  
  58.     var params = document.URL.split("?")[1].split("&");  
  59.     for (var i = 0; i < params.length; i = i + 1)   
  60.     {  
  61.         var singleParam = params[i].split("=");  
  62.         if (singleParam[0] == paramToRetrieve) return singleParam[1];  
  63.     }  
  64. }  
Publish

Publish the App and Click the Trust it Button.



Output

File updated successfully in Document Library SharePointLibraray: