Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <input type="button" value="AddCustomUserActionToRibbon" onclick="AddCustomUserActionToRibbon()"/>
- <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
- <script language="javascript" type="text/javascript">
- function AddCustomUserActionToRibbon(){
- SP.SOD.executeOrDelayUntilScriptLoaded(AddCustomUserAction, "sp.js");
- }
- var oListItem;
- function AddCustomUserAction() {
- //Get the client context and list object
- var context = new SP.ClientContext.get_current();
- var list = context.get_web().get_lists().getByTitle("TestCustomList");
- //Get the custom user action collection and add the user action
- var customUserAction = list.get_userCustomActions().add();
- //Set the location of the user action
- customUserAction.set_location('CommandUI.Ribbon.ListView');
- //Add the properties for the custom action
- 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>';
- //Add the command UI extension and update the custom user action
- customUserAction.set_commandUIExtension(userActionExtension)
- customUserAction.update();
- //Load the client context and execute the batch
- context.load(list, 'UserCustomActions');
- context.executeQueryAsync(function() {
- console.log("Custom User Action added successfully to ribbon.");
- }, function(sender, args) {
- console.log(args.get_message());
- });
- }
- 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());
- }
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement