Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function EnableFinishedButton() {
- var context = SP.ClientContext.get_current();
- var list;
- var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
- var totalSelectedItems = CountDictionary(selectedItems);
- if (totalSelectedItems > 0) {
- var web = context.get_web();
- context.load(web);
- var listId = SP.ListOperation.Selection.getSelectedList();
- list = web.get_lists().getById(listId);
- // We will use this variable to determine whether EnableFinishedButton() is being called directly or by RefreshCommandUI()
- var hadToMakeCall = false;
- if (typeof this.itemRows == "undefined" || this.itemRows.length != totalSelectedItems) {
- // 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
- hadToMakeCall = true;
- GetItemsStatus();
- }
- // If we just issued the async call, do not enable the button yet
- if (hadToMakeCall) {
- return false;
- }
- else {
- // Once the call has returned, set the enabled status based on the returned value
- return this._can_be_enabled;
- }
- }
- else {
- this.itemRows = undefined;
- return false;
- }
- function GetItemsStatus() {
- // Store the selected list items in an array where their values can be checked
- itemRows = [];
- for (i in selectedItems) {
- itemRows[i] = list.getItemById(selectedItems[i].id);
- context.load(itemRows[i]);
- }
- context.executeQueryAsync(Function.createDelegate(this, onGetItemsSuccess), Function.createDelegate(this, onGetItemsQueryFailed));
- }
- function onGetItemsSuccess() {
- this._can_be_enabled = true;
- // 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
- for (i in itemRows) {
- this._can_be_enabled = this._can_be_enabled && itemRows[i].get_item("Status") == "In Progress";
- }
- // Now we can call the EnabledScript function again
- RefreshCommandUI();
- }
- function onGetItemsQueryFailed(sender, args) {
- alert(args.get_message());
- }
- }
- function MarkItemsFinished() {
- var context = SP.ClientContext.get_current();
- var web = context.get_web();
- var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
- var listId = SP.ListOperation.Selection.getSelectedList();
- var list = web.get_lists().getById(listId);
- var i;
- for (i in selectedItems) {
- // Update the "Status" field of each selected item to have a value of "Finished"
- var listItem = list.getItemById(selectedItems[i].id);
- listItem.set_item("Status", "Finished");
- listItem.update();
- }
- context.executeQueryAsync(Function.createDelegate(this, onUpdateItemsSuccess), Function.createDelegate(this, onUpdateItemsFailed));
- function onUpdateItemsSuccess() {
- alert("Items updated!");
- }
- function onUpdateItemsFailed() {
- alert(args.get_message());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement