Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. ;(function(){
  2. var recycleItems = function(listViewName){
  3. listViewName = listViewName || false;
  4. var successMessage = 'Item(s) Recycled',
  5. ctx = this.get_context(),
  6. list = this,
  7. items,
  8. views = this.get_views(),
  9. view,
  10. counter,
  11. noop = function(){},
  12. error = function(sender, args){
  13. var message = args.get_message() + 'n' + args.get_errorCode();
  14. console.error(message);
  15. },
  16. execute = function(payload,callback){
  17. payload = payload || null;
  18. callback = callback || noop;
  19. if(typeof(payload) === 'function'){
  20. callback = payload;
  21. }
  22. if(payload !== null && callback !== payload){
  23. ctx.load(payload);
  24. }
  25. ctx.executeQueryAsync(callback.bind(null,payload),error);
  26. },
  27. getView = function(){
  28. view = views.getByTitle(listViewName);
  29. execute(view,getItems);
  30. },
  31. getItems = function(){
  32. var camlQuery;
  33. if(!listViewName){
  34. camlQuery = new SP.CamlQuery.createAllItemsQuery();
  35. }
  36. else{
  37. camlQuery = new SP.CamlQuery();
  38. camlQuery.set_viewXml(view.get_listViewXml());
  39. }
  40. items = list.getItems(camlQuery);
  41. execute(items,recycleItems);
  42. },
  43. success = function(sender, args){
  44. console.log(counter + ' ' + successMessage);
  45. },
  46. recycleItems = function(){
  47. var enumerator = items.getEnumerator();
  48. var recycleitems = [];
  49. while(enumerator.moveNext()){
  50. var item = enumerator.get_current();
  51. recycleitems.push(item);
  52. }
  53. for(var i = 0; i < recycleitems.length; i++){
  54. recycleitems[i].recycle();
  55. counter = i+1;
  56. }
  57. execute(success);
  58. };
  59. if(!listViewName){
  60. console.error('Error: You must provide a View Name');
  61. }
  62. else{
  63. getView();
  64. }
  65. }
  66. if(_v_dictSod['sp.js'].state === Sods.loaded){
  67. window.SP.List.prototype.recycleItems = recycleItems;
  68. }
  69. else{
  70. RegisterSod("sp.res.resx", "/_layouts/15/ScriptResx.ashx");
  71. RegisterSod("sp.js", "/_layouts/15/sp.debug.js");
  72. RegisterSodDep("sp.js", "sp.res.resx");
  73. SP.SOD.executeFunc('sp.js',false,function(){
  74. window.SP.List.prototype.recycleItems = recycleItems;
  75. });
  76. }
  77. }())
  78.  
  79. var ctx = new SP.ClientContext.get_current();
  80. var list = ctx.get_web().get_lists().getByTitle('<List Title>');
  81. ctx.load(list);
  82. ctx.executeQueryAsync(
  83. function(){
  84. list.recycleItems('<Name of List View');
  85. },
  86. function(sender,args){
  87. console.error(args.get_message() + 'n' + args.get_errorCode();
  88. }
  89. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement