Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. // example: registerScriptLink('LoQutus.BreadCrumb', '~sitecollection/siteassets/js/breadcrumb.js', 'Adds a breadcrumb navigation across the site collection');
  2. // example: removeScriptLink('LoQutus.BreadCrumb', null, null, true);
  3.  
  4. function onError(sender, args) {
  5. console.log('Error: ' + args.get_message());
  6. }
  7.  
  8. function registerScriptLink(id, url, description) {
  9.  
  10. var ctx = SP.ClientContext.get_current(),
  11. site = ctx.get_site(),
  12. userActions = site.get_userCustomActions();
  13.  
  14. ctx.load(userActions);
  15. ctx.executeQueryAsync(
  16. function () {
  17. var userActionsEnumerator = userActions.getEnumerator(),
  18. userAction,
  19. found;
  20. while (userActionsEnumerator.moveNext() && !found) {
  21. userAction = userActionsEnumerator.get_current();
  22. if (userAction.get_title() === id) {
  23. found = true;
  24.  
  25. }
  26. }
  27.  
  28. if (!found) {
  29. console.log('Creating new UserCustomAction');
  30. userAction = userActions.add();
  31. }
  32.  
  33. userAction.set_title(id);
  34. userAction.set_sequence(2);
  35. userAction.set_location('ScriptLink');
  36. userAction.set_scriptSrc(url);
  37. userAction.set_description(description);
  38. userAction.update();
  39. ctx.executeQueryAsync(
  40. function () {
  41. console.log('UserCustomAction has been saved');
  42. },
  43. onError
  44. );
  45. },
  46. onError
  47. );
  48. }
  49.  
  50. function removeScriptLink(id) {
  51. var ctx = SP.ClientContext.get_current(),
  52. site = ctx.get_site(),
  53. userActions = site.get_userCustomActions();
  54.  
  55. ctx.load(userActions);
  56. ctx.executeQueryAsync(
  57. function () {
  58. var userActionsEnumerator = userActions.getEnumerator(),
  59. userAction,
  60. found;
  61. while (userActionsEnumerator.moveNext() && !found) {
  62. userAction = userActionsEnumerator.get_current();
  63. if (userAction.get_title() === id) {
  64. found = true;
  65.  
  66. }
  67. }
  68.  
  69. if (found) {
  70. userAction.deleteObject();
  71. ctx.executeQueryAsync(
  72. function () {
  73. console.log('UserCustomAction has been deleted');
  74. },
  75. onError
  76. );
  77. } else {
  78. console.log('UserCustomAction not found');
  79. }
  80. },
  81. onError
  82. );
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement