2

I have files in a document library which needs to be checkout by some automatic scripts from other web application(Asp.net/javascript) using SharePoint REST API. I'm working on Sharepoint Server 2013 On Premise

I found that we can achieve it only in

  1. Sharepoint Apps
  2. SharePoint Server Project

I gone through this links

  1. https://msdn.microsoft.com/en-us/library/office/jj164022.aspx#ClientAPIs
  2. https://msdn.microsoft.com/en-us/library/office/dn292553.aspx

Is it Possible?

2
  • What exactly is the problem? You don't know REST endpoint for check-out, or how to make REST call between two web applications? Commented Mar 3, 2016 at 8:54
  • How to make SharePoint REST API call from other application(Not in SP Web application). Is it possible
    – Light
    Commented Mar 3, 2016 at 9:08

1 Answer 1

1

APP

'use strict';  
var hostweburl;   
var appweburl;   

    // This code runs when the DOM is ready and creates a context object which is   
    // needed to use the SharePoint object model  
    $(document).ready(function () {  

            //Get the URI decoded URLs.   
    hostweburl =   
        decodeURIComponent(   
            getQueryStringParameter("SPHostUrl"));   
    appweburl =   
        decodeURIComponent(   
            getQueryStringParameter("SPAppWebUrl"));   
               // Resources are in URLs in the form:  
        // web_url/_layouts/15/resource  
        var scriptbase = hostweburl + "/_layouts/15/";    

        // Load the js file and continue to load the page with information about the list top level folders.  
        // SP.RequestExecutor.js to make cross-domain requests  

         // Load the js files and continue to the successHandler  
            $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);  
             });  

             // Function to prepare and issue the request to get  
          //  SharePoint data  
          function execCrossDomainRequest() {  
            // executor: The RequestExecutor object  
            // Initialize the RequestExecutor with the app web URL.  
            var executor = new SP.RequestExecutor(appweburl);             

            var metatdata = "{ '__metadata': { 'type': 'SP.Data.TestListListItem' }, 'Title': 'changelistitemtitle'}";  

            // Issue the call against the app web.  
            // To get the title using REST we can hit the endpoint:  
            //      appweburl/_api/web/lists/getbytitle('listname')/items  
            // The response formats the data in the JSON format.  
            // The functions successHandler and errorHandler attend the  
            //      sucess and error events respectively.  
            executor.executeAsync(  
                {            
          url:    
            appweburl +    
            "/_api/SP.AppContextSite(@target)/web/ GetFileByServerRelativeUrl(  
  '/Shared Documents/a.txt')/CheckOut()?@target='" +    
            hostweburl + "'",    
        method: "POST",    
        body: metatdata ,    
        headers: { "Accept": "application/json; odata=verbose", "content-type": "application/json; odata=verbose", "content-length": metatdata.length, "X-HTTP-Method": "MERGE", "IF-MATCH": "*" },            
            success: function (data) {  
                alert("success: " + JSON.stringify(data));  
            },  
            error: function (err) {  
                alert("error: " + JSON.stringify(err));  
            }    
                }            
            );                

          }                    
    // This function prepares, loads, and then executes a SharePoint query to get   
    // the current users information        
//Utilities   

// Retrieve a query string value.   
// For production purposes you may want to use   
// a library to handle the query string.   
function getQueryStringParameter(paramToRetrieve) {   
    var params =   
        document.URL.split("?")[1].split("&");     
    for (var i = 0; i < params.length; i = i + 1) {   
        var singleParam = params[i].split("=");   
        if (singleParam[0] == paramToRetrieve)   
            return singleParam[1];   
    }   
}   

source: http://www.c-sharpcorner.com/UploadFile/472cc1/check-out-files-in-sharepoint-library-2013-using-rest-api/

JS

 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));           
         }

source: http://www.sharepointpals.com/post/How-to-Checkin-and-CheckOut-in-SharePoint-2013-using-Javascript

CSOM

//get the connection
ClientContext ctx = new ClientContext("http://sitename");

//get the home page
File home = ctx.Web.GetFileByServerRelativeUrl("/SitePages/home.aspx");

//checkout
home.CheckOut();

source: Check- in & check-out files using client object model

7
  • what will be hostweburl & appweburl
    – Light
    Commented Mar 3, 2016 at 10:23
  • When the App first launches, SharePoint provides three values in the query string named: SPHostUrl, SPAppWebUrl and SPLanguage. Commented Mar 3, 2016 at 10:40
  • I don't want in Sharepoint App, can I run above script in basic html with javascript ?
    – Light
    Commented Mar 3, 2016 at 10:51
  • you can use apps, CSOM, JS, C#, just choose your flavor Commented Mar 3, 2016 at 10:59
  • if you want to run automatic scripts form an external location, I would recommend a c# console application using the client object model Commented Mar 3, 2016 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.