Created
January 9, 2020 15:23
-
-
Save durgesh97025/671ec91395db96df653f5fb0fc4a6202 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
//SP.SOD.executeOrDelayUntilScriptLoaded(AddCustomUserAction, "sp.js"); | |
SP.SOD.executeOrDelayUntilScriptLoaded(DeleteCustomActions, "sp.js"); | |
}); | |
function DeleteCustomActions() { | |
var listTitle = "Pension Files"; | |
context = new SP.ClientContext(); | |
var web = context.get_web(); | |
var list = context.get_web().get_lists().getByTitle(listTitle); | |
customActions = list.get_userCustomActions(); | |
context.load(list); | |
context.load(customActions); | |
context.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFailure)); | |
} | |
function onSuccess() { | |
var customActionEnumerator = customActions.getEnumerator(); | |
while (customActionEnumerator.moveNext()) { | |
var oUserCustomAction = customActionEnumerator.get_current(); | |
console.log(oUserCustomAction.get_title()); | |
if (oUserCustomAction.get_commandUIExtension().indexOf('Show List ID') > -1){ | |
oUserCustomAction.deleteObject(); | |
context.load(oUserCustomAction); | |
context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededdelete), Function.createDelegate(this, this.onQueryFailed)); | |
} | |
} | |
function onQuerySucceededdelete() { | |
alert('Custom Actions Deleted Successfully'); | |
} | |
function onQueryFailed(sender, args) { | |
alert('Error Occurred.' + args.get_message() + '\n' + args.get_stackTrace()); | |
} | |
} | |
function onFailure(sender, args) { | |
alert('Error Occurred.' + args.get_message()); | |
} | |
function AddCustomUserAction() { | |
var listTitle = "Pension Files"; | |
//Get the client context and list object | |
var context = new SP.ClientContext.get_current(); | |
var list = context.get_web().get_lists().getByTitle(listTitle); | |
//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.EditForm'); | |
//Add the properties for the custom action | |
var userActionExtension = | |
'<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">' | |
+ '<CommandUIDefinitions>' | |
+ '<CommandUIDefinition Location="Ribbon.ListForm.Edit.Actions.Controls._children">' | |
+ '<Button Id="Ribbon.Documents.New.RibbonTest" Command="Notify" Sequence="0" Image16by16="/_layouts/images/NoteBoard_16x16.png" Image32by32="/_layouts/images/NoteBoard_32x32.png" Description="Shows the ID of the current list." LabelText="Show List ID" TemplateAlias="o1"/>' | |
+ '</CommandUIDefinition>' | |
+ '</CommandUIDefinitions>' | |
+ '<CommandUIHandlers>' | |
+ '<CommandUIHandler Command="Notify" CommandAction="javascript:SP.UI.Notify.addNotification(\'ListId={ListId}\');" />' | |
+ '</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()); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment