Introduction
IT and business users commonly use the custom action. In this article, I have created a tool that can add features to SharePoint through a distinctive design interface with ribbon controls.
Here we have created 4 buttons for the custom action operations for the list and site.
Button 1 - Create User CustomAction for list
This button calls the function ‘createUserCustomActionList ()’, in this function, we are retrieving the list in which we want to add the custom action. Then get the custom action collections by using ‘get_userCustomActions()’ and add the new custom action and set the properties and update it. (Here we have set the title as ‘My First User Custom Action’).
Button 2 - Delete User CustomAction for list
This button calls the function ‘deleteUserCustomAction()’, in this function, we are retrieving all the list custom action collection. Then get the user custom action by its name and delete it by using ‘deleteObject()’.
Button 3 - Modify User CustomAction for list
This button calls the function ‘modifyUserCustomAction()’, in this function, we are retrieving all the list custom action collection. Then get the user custom action by its name and set the title or you can also set the properties which you want to modify, here we are modifying the title of the custom action by ‘Custom action’.
Button 4 - Create User CustomAction for Site
This button calls the function ‘createUserCustomActionSite()’, in this function, we are retrieving the web. Then get the site custom action collections by using ‘get_userCustomActions()’ and add the new custom action in the site and set the properties and update it. (Here we have set the title as ‘Website User Custom Action’).
Follow the below code snippet,
<script type = "text/javascript" language = "javascript">
function createUserCustomActionList() {
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getByTitle('My List');
var CustomActionColl = list.get_userCustomActions();
var custAction = CustomActionColl.add();
custAction.set_location('EditControlBlock');
custAction.set_sequence(100);
custAction.set_title('My First User Custom Action');
custAction.update();
ctx.load(list, 'Title', 'UserCustomActions');
ctx.executeQueryAsync(function() {
alert('Custom action created successfully for ' + list.get_title());
}, function(s, a) {
alert('Request failed. ' + a.get_message());
});
}
function createUserCustomActionSite() {
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var CustomActionColl = web.get_userCustomActions();
var custAction = CustomActionColl.add();
custAction.set_location('Microsoft.SharePoint.StandardMenu');
custAction.set_group('SiteActions');
custAction.set_sequence(101);
custAction.set_title('Website User Custom Action');
custAction.set_description('Description for custom action.');
custAction.update();
ctx.load(web, 'Title', 'UserCustomActions');
ctx.executeQueryAsync(function() {
alert('Custom action created successfully for ' + web.get_title());
}, function(sender, args) {
alert('Error ' + args.get_message());
});
}
Function modifyUserCustomAction() {
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getByTitle('My List');
var CustomActionColl = list.get_userCustomActions();
ctx.load(list, 'UserCustomActions', 'Title');
ctx.executeQueryAsync(function() {
var customActionEnum = CustomActionColl.getEnumerator();
while (customActionEnum.moveNext()) {
var custAction = customActionEnum.get_current();
if (custAction.get_title() == 'My First User Custom Action') {
custAction.set_title('Custom action');
custAction.update();
ctx.load(custAction);
ctx.executeQueryAsync(function() {
alert('Custom action modified successfully for ' + list.get_title());
}, function(a, s) {
alert('Error ' + a.get_message());
});
}
}
}, function(a, s) {
alert('Error ' + a.get_message());
});
}
function deleteUserCustomAction() {
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getByTitle('My List');
var CustomActionColl = list.get_userCustomActions();
ctx.load(list, 'UserCustomActions', 'Title');
ctx.executeQueryAsync(function() {
var customActionEnum = CustomActionColl.getEnumerator();
while (customActionEnum.moveNext()) {
var custAction = customActionEnum.get_current();
if (custAction.get_title() == 'My First User Custom Action') {
custAction.deleteObject();
ctx.load(custAction);
ctx.executeQueryAsync(function() {
alert('Custom action deleted successfully for ' + list.get_title());
}, function(s, a) {
alert('Error ' + a.get_message());
});
}
}
}, function(a, s) {
alert('Error ' + a.get_message());
});
}
</script>
<input type="button" value="Create User CustomAction for list" id="create" onclick="createUserCustomActionList()" />
</br >
</br >
<input type="button" value="Delete User CustomAction for list" id="Delete" onclick="deleteUserCustomAction()" />
</br >
</br >
<input type="button" value="Modify User CustomAction for list" id="Delete" onclick="modifyUserCustomAction()" />
</br >
</br >
<input type="button" value="Create User CustomAction for site" id="Delete" onclick="createUserCustomActionSite()" />
</br >
Button 1 - Create User CustomAction for list
Click on the button.
In the below screenshot you can see the alert-success message. Click on ok.
In the below screenshot, you can see the custom action is added successfully.
Button 2 - Delete User CustomAction for list
Click on the button. You can see the alert-success message, click on OK.
You can see in the screenshot custom action is deleted successfully.
Button 3 - Modify User CustomAction for list
Click on the button. You can see the alert-success message, click on OK.
You can see in the below screenshot custom action name is change into ‘Custom action’.
Button 4 - Create User CustomAction for Site
Click on the button. You can see the alert-success message, click on ok.
In the below screenshot you can see the custom action is successfully added to the site.