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