Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. function EnableFinishedButton() {
  2. var context = SP.ClientContext.get_current();
  3. var list;
  4. var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
  5. var totalSelectedItems = CountDictionary(selectedItems);
  6.  
  7. if (totalSelectedItems > 0) {
  8. var web = context.get_web();
  9. context.load(web);
  10. var listId = SP.ListOperation.Selection.getSelectedList();
  11. list = web.get_lists().getById(listId);
  12.  
  13. // We will use this variable to determine whether EnableFinishedButton() is being called directly or by RefreshCommandUI()
  14. var hadToMakeCall = false;
  15.  
  16. if (typeof this.itemRows == "undefined" || this.itemRows.length != totalSelectedItems) {
  17. // This will be true if this is the first time an item has been selected in the list OR if the selected items have changed, forcing the need to check again
  18. hadToMakeCall = true;
  19. GetItemsStatus();
  20. }
  21.  
  22. // If we just issued the async call, do not enable the button yet
  23. if (hadToMakeCall) {
  24. return false;
  25. }
  26. else {
  27. // Once the call has returned, set the enabled status based on the returned value
  28. return this._can_be_enabled;
  29. }
  30. }
  31. else {
  32. this.itemRows = undefined;
  33. return false;
  34. }
  35.  
  36. function GetItemsStatus() {
  37. // Store the selected list items in an array where their values can be checked
  38. itemRows = [];
  39.  
  40. for (i in selectedItems) {
  41. itemRows[i] = list.getItemById(selectedItems[i].id);
  42. context.load(itemRows[i]);
  43. }
  44.  
  45. context.executeQueryAsync(Function.createDelegate(this, onGetItemsSuccess), Function.createDelegate(this, onGetItemsQueryFailed));
  46. }
  47.  
  48. function onGetItemsSuccess() {
  49. this._can_be_enabled = true;
  50.  
  51. // Iterate through all selected items. If one is false, the value of _can_be_enabled will be false and the button will not be enabled
  52. for (i in itemRows) {
  53. this._can_be_enabled = this._can_be_enabled && itemRows[i].get_item("Status") == "In Progress";
  54. }
  55.  
  56. // Now we can call the EnabledScript function again
  57. RefreshCommandUI();
  58. }
  59.  
  60. function onGetItemsQueryFailed(sender, args) {
  61. alert(args.get_message());
  62. }
  63. }
  64.  
  65. function MarkItemsFinished() {
  66. var context = SP.ClientContext.get_current();
  67. var web = context.get_web();
  68. var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
  69. var listId = SP.ListOperation.Selection.getSelectedList();
  70. var list = web.get_lists().getById(listId);
  71. var i;
  72. for (i in selectedItems) {
  73. // Update the "Status" field of each selected item to have a value of "Finished"
  74. var listItem = list.getItemById(selectedItems[i].id);
  75. listItem.set_item("Status", "Finished");
  76. listItem.update();
  77. }
  78. context.executeQueryAsync(Function.createDelegate(this, onUpdateItemsSuccess), Function.createDelegate(this, onUpdateItemsFailed));
  79.  
  80. function onUpdateItemsSuccess() {
  81. alert("Items updated!");
  82. }
  83.  
  84. function onUpdateItemsFailed() {
  85. alert(args.get_message());
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement