0

We have the following code to delete all access requests b/c we are controlling permissions via AD Groups.

What we'd like to do is delete access requests from a certain date and later because that would give us a point in time where we though we had permission groups setup properly, but maybe something was missed.

Code so far is as followings, but we'd like to put a date clause to keep more recent requests to analyse what was missed:

<script src=" http://spsite.com/sites/assets/include/scripts/1.11.1/jquery-1.11.1.min.js"></script>

<script>
$(document).ready(function() 
{
$("#btnClickDeleteAccessRequests").click(deleteFunction);

function deleteFunction() {

 ExecuteOrDelayUntilScriptLoaded(function(){


   var ctx = SP.ClientContext.get_current(),
   list = ctx.get_web().get_lists().getByTitle('Access Requests'),
   query = new SP.CamlQuery(),
   items = list.getItems(query);
ctx.load(items, "Include(Id)");
ctx.executeQueryAsync(function () {
    var enumerator = items.getEnumerator(),
        simpleArray = [];
    while (enumerator.moveNext()) {
        simpleArray.push(enumerator.get_current());
    }
    for (var s in simpleArray) {
        simpleArray[s].deleteObject();
    }
    ctx.executeQueryAsync();
});   

}, "sp.js");
}});
</script>
<input type="button" id="btnClickDeleteAccessRequests" value="Click Here" />
  • i would suggest you post it as an answer and mark it.. – Amal Hashim Aug 8 '16 at 18:36
0

08.08.2016 Update >> got the code to work as follows with date range:

<script src="/sites/assets/include/scripts/1.11.1/jquery-1.11.1.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/Documents/styleDatePicker.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>


<script>
$(document).ready(function() 
{
$("#btnClickDeleteAccessRequests").click(deleteFunction);

function deleteFunction() {

 ExecuteOrDelayUntilScriptLoaded(function(){


   var query;
var startDate = $("#fromdatepicker").datepicker("getDate");
var endDate = $("#todatepicker").datepicker("getDate");
if(startDate==null){
alert('Please select Start Date.');
return;
}
if(endDate==null){
alert('Please select End Date.');
return;
}
if(startDate !=null && endDate !=null){
startDate  =  startDate.getFullYear()+"-"+(startDate.getMonth()+1)+"-"+startDate.getDate()+'T00:00:01Z'; 
endDate  =  endDate.getFullYear()+"-"+(endDate.getMonth()+1)+"-"+endDate.getDate()+'T00:00:01Z'; 

query = "<View Scope='RecursiveAll'><Query><Where><And><Geq><FieldRef Name='Created'/><Value Type='DateTime' IncludeTimeValue='FALSE'>"+startDate+ "</Value></Geq><Leq><FieldRef Name='Created'/><Value Type='DateTime' IncludeTimeValue='FALSE'>"+ endDate + "</Value></Leq></And></Where></Query></View>";
}
var ctx = SP.ClientContext.get_current(),
list = ctx.get_web().get_lists().getByTitle('Access Requests');

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(query);
items = list.getItems(camlQuery);

ctx.load(items, "Include(Id)");
ctx.executeQueryAsync(function () {
    var enumerator = items.getEnumerator(),
        simpleArray = [];
    while (enumerator.moveNext()) {
        simpleArray.push(enumerator.get_current());
    }
    for (var s in simpleArray) {
        simpleArray[s].deleteObject();
    }
    ctx.executeQueryAsync();
});   

}, "sp.js");
}});


  $( function() {
    $( "#fromdatepicker" ).datepicker();
  } );

$( function() {
    $( "#todatepicker" ).datepicker();
  } );

</script>

<table width="300px">
<tr>
<td>Start Date:</td><td><input type="text" id="fromdatepicker"></td>
</tr>
<tr>
<td>End Date:</td><td><input type="text" id="todatepicker"></td>
</tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
<input type="button" id="btnClickDeleteAccessRequests" value="Delete Date Range Access Requests" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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