Advertisement
Guest User

Marke as Finished Button

a guest
Sep 5th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <input type="button" value="AddCustomUserActionToRibbon" onclick="AddCustomUserActionToRibbon()"/>
  2. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
  3. <script language="javascript" type="text/javascript">  
  4. function AddCustomUserActionToRibbon(){
  5.     SP.SOD.executeOrDelayUntilScriptLoaded(AddCustomUserAction, "sp.js");
  6. }
  7. var oListItem;  
  8. function AddCustomUserAction() {  
  9.     //Get the client context and list object  
  10.     var context = new SP.ClientContext.get_current();  
  11.     var list = context.get_web().get_lists().getByTitle("TestCustomList");  
  12.     //Get the custom user action collection and add the user action  
  13.     var customUserAction = list.get_userCustomActions().add();  
  14.     //Set the location of the user action  
  15.     customUserAction.set_location('CommandUI.Ribbon.ListView');  
  16.     //Add the properties for the custom action  
  17.     var userActionExtension = '<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">' + '<CommandUIDefinitions>' + '<CommandUIDefinition Location="Ribbon.ListItem.Actions.Controls._children">' + '<Button Id="DannyJessee.MarkItemsFinishedButton" ' + 'Command="cmdMarkItemsFinished" ' + 'Sequence="0" ' + 'Image16by16="/_layouts/images/kpinormallarge-0.gif" ' + 'Image32by32="/_layouts/images/kpinormallarge-0.gif" ' + 'Description="Mark as Finished" ' + 'LabelText="Mark as Finished" ' + 'TemplateAlias="o2"/>' + '</CommandUIDefinition>' + '</CommandUIDefinitions>' + '<CommandUIHandlers>' + '<CommandUIHandler Command="cmdMarkItemsFinished" ' + 'CommandAction="javascript:MarkItemsFinished();" EnabledScript="javascript:EnableFinishedButton();" />' + '</CommandUIHandlers>' + '</CommandUIExtension>';  
  18.     //Add the command UI extension and update the custom user action  
  19.     customUserAction.set_commandUIExtension(userActionExtension)  
  20.     customUserAction.update();  
  21.     //Load the client context and execute the batch  
  22.     context.load(list, 'UserCustomActions');  
  23.     context.executeQueryAsync(function() {  
  24.         console.log("Custom User Action added successfully to ribbon.");  
  25.     }, function(sender, args) {  
  26.         console.log(args.get_message());  
  27.     });  
  28. }
  29.  
  30. function EnableFinishedButton() {
  31.     var context = SP.ClientContext.get_current();
  32.     var list;
  33.     var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
  34.     var totalSelectedItems = CountDictionary(selectedItems);
  35.  
  36.     if (totalSelectedItems > 0) {
  37.         var web = context.get_web();
  38.         context.load(web);
  39.         var listId = SP.ListOperation.Selection.getSelectedList();
  40.         list = web.get_lists().getById(listId);
  41.  
  42.         // We will use this variable to determine whether EnableFinishedButton() is being called directly or by RefreshCommandUI()
  43.         var hadToMakeCall = false;
  44.  
  45.         if (typeof this.itemRows == "undefined" || this.itemRows.length != totalSelectedItems) {
  46.             // 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
  47.             hadToMakeCall = true;
  48.             GetItemsStatus();
  49.         }
  50.  
  51.         // If we just issued the async call, do not enable the button yet
  52.         if (hadToMakeCall) {
  53.             return false;
  54.         }
  55.         else {
  56.             // Once the call has returned, set the enabled status based on the returned value
  57.             return this._can_be_enabled;
  58.         }
  59.     }
  60.     else {
  61.         this.itemRows = undefined;
  62.         return false;
  63.     }
  64.     function GetItemsStatus() {
  65.         // Store the selected list items in an array where their values can be checked
  66.         itemRows = [];
  67.  
  68.         for (i in selectedItems) {
  69.             itemRows[i] = list.getItemById(selectedItems[i].id);
  70.             context.load(itemRows[i]);
  71.         }
  72.  
  73.         context.executeQueryAsync(Function.createDelegate(this, onGetItemsSuccess), Function.createDelegate(this, onGetItemsQueryFailed));
  74.     }
  75.  
  76.     function onGetItemsSuccess() {
  77.         this._can_be_enabled = true;
  78.  
  79.         // 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
  80.         for (i in itemRows) {
  81.             this._can_be_enabled = this._can_be_enabled && itemRows[i].get_item("Status") == "In Progress";
  82.         }
  83.  
  84.         // Now we can call the EnabledScript function again
  85.         RefreshCommandUI();
  86.     }
  87.  
  88.     function onGetItemsQueryFailed(sender, args) {
  89.         alert(args.get_message());
  90.     }
  91. }
  92. function MarkItemsFinished() {
  93.     var context = SP.ClientContext.get_current();
  94.     var web = context.get_web();
  95.     var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
  96.     var listId = SP.ListOperation.Selection.getSelectedList();
  97.     var list = web.get_lists().getById(listId);
  98.     var i;
  99.     for (i in selectedItems) {
  100.         // Update the "Status" field of each selected item to have a value of "Finished"
  101.         var listItem = list.getItemById(selectedItems[i].id);
  102.         listItem.set_item("Status", "Finished");
  103.         listItem.update();
  104.     }
  105.     context.executeQueryAsync(Function.createDelegate(this, onUpdateItemsSuccess), Function.createDelegate(this, onUpdateItemsFailed));
  106.  
  107.     function onUpdateItemsSuccess() {
  108.         alert("Items updated!");
  109.     }
  110.  
  111.     function onUpdateItemsFailed() {
  112.         alert(args.get_message());
  113.     }
  114. }  
  115. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement