SharePoint 2013: Create And Delete A User Custom Action Using CSOM

Introduction

In this article we explore how to create and delete a user custom action on the Site Actions menu of a website in SharePoint. Custom actions are types of app experiences where you can develop and deploy them for menu item to provide new functionality.

Creating a user custom action on the Site Actions menu of a Web site is similar to creating an action for list items: You call the Add () method, set properties for the action, and then call Update (). The following example specifies Microsoft.SharePoint.StandardMenu for Location, and Site Actions for Group, to place the new action on the Site Actions menu.

For example, how to add a link to Site Actions menu using JavaScript CSOM.

Hereby, I have demonstrated the above functionality through snapshots of the implemented work and through code.

Before:

before

Solution

Here are the steps:

Step 1: Navigate to your SharePoint 2013 site.

Step 2: From this page select the Site Actions | Edit Page.

Edit the page, go to the "Insert" tab in the Ribbon and click the "Web Part" option. In the "Web Parts" picker area, go to the "Media and Content" category, select the "Script Editor" Web Part and press the "Add button".

Step 3:

Once the Web Part is inserted into the page, you will see an "EDIT SNIPPET" link; click it. You can insert the HTML and/or JavaScript as in the following:

embed

  1. <script type="text/javascript"src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js"></script>  
  2. <script type="text/javascript"language="javascript">  
  3. var clientContext;  
  4. var oUserCustomAction;  
  5. // Delete user custom action on the Site Actions menu   
  6. function deleteUserCustomAction()  
  7.  {  
  8. clientContext = new SP.ClientContext()  
  9. var oWebsite = clientContext.get_web();  
  10. collUserCustomAction = oWebsite.get_userCustomActions();  
  11. clientContext.load(oWebsite, 'UserCustomActions''Title');  
  12. clientContext.executeQueryAsync(Function.createDelegate(thisthis.deleteCustomAction), Function.createDelegate(thisthis.onQueryFailed));  
  13. }  
  14. function deleteCustomAction()  
  15.  {  
  16. var CstomAName = $("#text1").val();  
  17. var customActionEnumerator = collUserCustomAction.getEnumerator();  
  18. while (customActionEnumerator.moveNext())  
  19.  {  
  20. var oUserCustomAction = customActionEnumerator.get_current();  
  21. if (oUserCustomAction.get_title() == CstomAName)   
  22. {  
  23. oUserCustomAction.deleteObject();  
  24. clientContext.load(oUserCustomAction);  
  25. clientContext.executeQueryAsync(Function.createDelegate(thisthis.onQuerySucceededdelete), Function.createDelegate(thisthis.onQueryFailed));  
  26. }  
  27. }  
  28. }  
  29. function onQuerySucceededdelete()  
  30.  {  
  31.   
  32. $("#result").empty();  
  33. $("#result").text('Custom action removed\n\nTo view the new menu item, refresh the page.');  
  34. }  
  35. function onQueryFailed(sender, args)  
  36.  {  
  37. $("#result").empty();  
  38. $("#result").text('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
  39. }  
  40.   
  41. //Create user custom action on the Site Actions menu   
  42.   
  43. function CreateUserCustomAction()  
  44.  {  
  45. var CstomAName = $("#text1").val();  
  46. var clientContext = new SP.ClientContext();  
  47. var site = clientContext.get_web();  
  48. var collUserCustomAction = site.get_userCustomActions();  
  49.   
  50. var newUserCustomAction = collUserCustomAction.add();  
  51. new UserCustomAction.set_location('Microsoft.SharePoint.StandardMenu');  
  52. new UserCustomAction.set_group('SiteActions');  
  53. new UserCustomAction.set_sequence(1000);  
  54. new UserCustomAction.set_title(CstomAName);  
  55. new UserCustomAction.set_imageUrl('/_layouts/images/myIcon.jpg');  
  56. new UserCustomAction.set_description('Menu item added via ECMAScript');  
  57. new UserCustomAction.set_url('/_layouts/create.aspx');  
  58. new UserCustomAction.update();  
  59. clientContext.executeQueryAsync(Function.createDelegate(thisthis.onQuerySucceeded), Function.createDelegate(thisthis.onQueryFailed));  
  60. }  
  61.   
  62. function onQuerySucceeded(sender, args)  
  63.  {  
  64. $("#result").empty();  
  65. $("#result").text('New menu item added to Site Actions menu.\n\nTo view the new menu item, refresh the page.')  
  66. }  
  67.   
  68. function onQueryFailed(sender, args)  
  69.  {  
  70. $("#result").empty();  
  71. $("#result").text('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
  72. }  
  73.   
  74. </script>  
  75.   
  76.   
  77.   
  78. <h2>Example Custom Action using SharePoint 2013 CSOM </h2>  
  79. </br>  
  80. </br>  
  81. Custom Action Name: <inputtype="text"id="text1"/>  
  82. </br>  
  83. </br>  
  84. <input type="button"value="Create User CustomAction"id="create"onclick="CreateUserCustomAction()"/>  
  85. </br>  
  86.   
  87. </br>  
  88. <input type="button"value="Delete User CustomAction"id="Delete"onclick="deleteUserCustomAction()"/>  
  89. </br>  
  90. <div id="result"></div>  
Create a user custom action on the Site Actions menu:

create

Delete a user custom action on the Site Actions menu:

delete
Summary:

I have tried creating and seleting a user custom action on the Site Actions menu, which would provide you a greater flexibility in user interaction on the application. I have achieved this using CSOM and JavaScript in SharePoint 2013. I hope this article is helpful to you and hereby I expect you to revert back to it in case of any queries.

Reference: