<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("AddTrimmerIcon");
function AddTrimmerIcon() {
$(".ms-qcb-root ul li:nth-child(4)").each(function() {
$(this).first().before('<li class="ms-qcb-item">' +
'<button class="ms-qcb-button ms-qcb-buttons-alignmentfix
js-listview-qcbUploadButton js-callout-body js-qcb-button ms-qcb-glyph"
type="button"' +
'title="Select file and trim versions of the file" role="button"
accesskey="t" onclick="TrimDocument()"><span class="ms-qcb-glyph
ms-listview-glyph-withmargin ms-listview-sync-glyph
ms-core-form-heading"><img style="height:20px;width:25px;" src="' +
_spPageContextInfo.webServerRelativeUrl +
'/SiteAssets/VersionTrimmer/vtrimmer.png"/></span><spancb-glyph
">Trim</span></button></li>')
});
}
function TrimDocument() {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', Trim);
}
function Trim() {
var count = SP.ListOperation.Selection.getSelectedItems(ctx).length;
if (count == 0) {
alert("Please select an item from the list");
return false;
}
if (count > 1) {
alert("Please select only one file");
return false;
}
SP.UI.ModalDialog.showWaitScreenWithNoClose('Trimming Versions', 'Please wait while
the versions of the file are being deleted');
var title = ctx.ListTitle;
var items = SP.ListOperation.Selection.getSelectedItems(ctx);
//get file versions
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('" + title
+ "')/items(" + items[0].id + ")/File/Versions",
method: "GET",
headers: {
"Accept": "application/json; odata=verbose"
},
success: function(data) {
var totalversions = data.d.results.length;
$.each(data.d.results, function(index, item) {
var count = index;
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl +
"/_api/web/lists/getByTitle('" + title + "')/items(" + items[0].id
+ ")/File/Versions('" + item.ID + "')",
type: "POST",
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "DELETE",
"IF-MATCH": "*"
},
success: function(data) {
if (totalversions == (count + 1)) {
SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.c
ancel);
}
},
error: function(data) {
SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.cancel);
}
});
});
},
error: function(data) {
SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.cancel);
}
});
}
</script>
</head>
<body>
</body>
</html>