'use strict';
var hostweburl;
var appweburl;
// Load the required SharePoint libraries.
$(document).ready(function () {
//Get the URI decoded URLs.
hostweburl = decodeURIComponent(
getQueryStringParameter("SPHostUrl"));
appweburl = decodeURIComponent(
getQueryStringParameter("SPAppWebUrl"));
//Assign events to buttons
$("#enableforcecheckoutbutton").click(function (event) {
enableForceCheckOut();
event.preventDefault();
});
// 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.
// SP.RequestExecutor.js to make cross-domain requests
$.getScript(scriptbase + "SP.RequestExecutor.js");
});
// 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];
}
}
// Enable force check out for the document library
function enableForceCheckOut() {
var listnametext = document.getElementById("listnametext").value;
var executor;
// Initialize the RequestExecutor with the app web URL.
executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + listnametext + "')?@target='" + hostweburl + "'",
method: "POST",
body: "{ '__metadata': { 'type': 'SP.List' }, 'ForceCheckout': true}",
headers: {
"IF-MATCH": "*",
"X-HTTP-Method": "MERGE",
"content-type": "application/json;odata=verbose"
},
success: enableForceCheckOutSuccessHandler,
error: enableForceCheckOutErrorHandler
});
}
// Success Handler
function enableForceCheckOutSuccessHandler(data) {
alert("Force Check-Out is enabled successfully for the document library.")
}
// Error Handler
function enableForceCheckOutErrorHandler(data, errorCode, errorMessage) {
alert("Could not enable force Check-Out: " + errorMessage);
}