Last active
May 12, 2023 13:01
-
-
Save iOnline247/db5d03c1fde98a749fbd to your computer and use it in GitHub Desktop.
Create/Delete Custom Actions in SharePoint
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
/*! | |
* Created by Matthew Bramer :: @iOnline247 | |
* Released under the MIT license | |
* Date: 2016-02-12 v1 | |
* Inspired from script written on: 2014-05-17 | |
* https://dl.dropboxusercontent.com/u/21583725/demos/SPSDC-ModalMania/demo/js/iframeDemo.js | |
* Tested using SharePoint Online. | |
* | |
* Helpful Links | |
* https://msdn.microsoft.com/en-us/library/office/dn531432.aspx#bk_UserCustomActionRequestExamples | |
* Default Custom Action Locations and IDs | |
* https://msdn.microsoft.com/en-us/library/bb802730(v=office.15).aspx | |
* https://msdn.microsoft.com/EN-US/library/office/jj246884.aspx | |
*/ | |
;(function() { | |
var context, | |
DEFAULT_TEXT = 'Created using Matthew Bramer\'s custom action helper.', | |
extend = function (props, target) { | |
props = props || {}; | |
for(var prop in props) { | |
target[prop] = props[prop]; | |
} | |
return target; | |
}, | |
error = function (sender, args) { | |
var message = args.get_message() + "\n" + args.get_errorCode(); | |
SP.UI.Notify.addNotification( message, false ); | |
}, | |
loadCustomActions = function (opt, cb) { | |
var siteType = opt.type.toLowerCase(), | |
customActions = context['get_' + siteType]().get_userCustomActions() | |
; | |
context.load(customActions); | |
context.executeQueryAsync( | |
cb.bind(null, opt, customActions), error | |
); | |
}, | |
_createCustomAction = function (opt, customActions) { | |
var customAction = customActions.add(); | |
customAction.set_name(opt.name); | |
customAction.set_description(opt.description); | |
customAction.set_location(opt.location); | |
customAction.set_sequence(opt.sequence); | |
if (opt.src) { | |
customAction.set_scriptSrc(opt.src); | |
} else if (opt.script) { | |
customAction.set_scriptBlock(opt.script); | |
} | |
customAction.update(); | |
context.executeQueryAsync( | |
function() { | |
SP.UI.Notify.addNotification('Custom Action: "' + opt.name + '" was added'); | |
}, | |
error | |
); | |
}, | |
_removeCustomAction = function (opt, customActions) { | |
var queued = [], | |
id = opt.id, | |
name = opt.name, | |
identity = id || name, | |
cb = function() { | |
SP.UI.Notify.addNotification('Custom Action: "' + identity + '" was removed'); | |
} | |
; | |
customActions = customActions.getEnumerator(); | |
while (customActions.moveNext()) { | |
var customAction = customActions.get_current(), | |
caGuid = customAction.get_id().toString() | |
; | |
if (caGuid === identity) { | |
queued.push(customAction); | |
} else if (customAction.get_name() === identity && customAction.get_location() === opt.location) { | |
queued.push(customAction); | |
} | |
} | |
if(!queued.length) { | |
var notFoundError = { | |
get_message: function () { | |
return 'No custom actions were removed. Couldn\'t find: ' + identity; | |
}, | |
get_errorCode: function () { | |
return ''; | |
} | |
}; | |
cb = error.bind(null, null, notFoundError); | |
} else { | |
queued.forEach(function (v) { | |
v.deleteObject(); | |
}); | |
} | |
context.executeQueryAsync(cb, error); | |
}, | |
createCustomAction = function (options) { | |
var opt = extend(options, { | |
description: DEFAULT_TEXT, | |
name: DEFAULT_TEXT, | |
location: 'ScriptLink', | |
sequence: 100, | |
type: 'web' | |
}); | |
loadCustomActions(opt, _createCustomAction); | |
}, | |
getCustomActions = function (options) { | |
var opt = extend(options, { | |
render: function (options, customActions) { | |
var numOfCustomActions = customActions.get_count(); | |
var customActionDetails = []; | |
customActions = customActions.getEnumerator(); | |
console.group(options.type.toUpperCase() + ' Custom Actions'); | |
while (customActions.moveNext()) { | |
var customAction = customActions.get_current(); | |
var caDeets = { | |
Name: customAction.get_name(), | |
"Script Src": customAction.get_scriptSrc() || customAction.get_scriptBlock(), | |
Id: customAction.get_id().toString(), | |
Sequence: customAction.get_sequence() | |
}; | |
customActionDetails.push(caDeets); | |
} | |
if (numOfCustomActions > 0) { | |
console.table(customActionDetails); | |
} else { | |
console.table([{ "Custom Actions": 'No custom actions found.' }]); | |
} | |
console.groupEnd(); | |
}, | |
type: 'web' | |
}); | |
loadCustomActions(opt, opt.render); | |
}, | |
removeCustomAction = function (options) { | |
var opt = extend(options, { | |
location: 'ScriptLink', | |
name: DEFAULT_TEXT, | |
type: 'web' | |
}); | |
loadCustomActions(opt, _removeCustomAction); | |
} | |
; | |
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() { | |
context = SP.ClientContext.get_current(); | |
}); | |
window.$sp = window.$sp || {}; | |
window.$sp.createCustomAction = createCustomAction; | |
window.$sp.getCustomActions = getCustomActions; | |
window.$sp.removeCustomAction = removeCustomAction; | |
}()); | |
/* | |
// Usage: | |
$sp.createCustomAction({ | |
name: 'Matthew.Bramer.Test', | |
src: 'https://code.jquery.com/jquery-2.2.0.min.js', | |
sequence: 1, | |
type: 'site' | |
}); | |
// Can use site tokens too: | |
// ~site/ | |
// ~sitecollection | |
// ~sitecollectionmasterpagegallery | |
$sp.createCustomAction({ | |
name: 'Matthew.Bramer.Tokens', | |
src: '~sitecollection/Web%20Dev/js/customAction-dependencies.js', | |
sequence: 2, | |
type: 'site' | |
}); | |
// Remove with id for the most accurate results. | |
$sp.removeCustomAction({ | |
id: '871988e5-fed4-4b5d-ab68-ce974e834f95', | |
type: 'web' | |
}); | |
// Only use name as last ditch effort. | |
// Other Custom Actions may have same name. | |
// To get id of a Custom Action, use $sp.getCustomActions | |
$sp.removeCustomAction({ | |
name: 'Matthew.Bramer.Tokens', | |
type: 'site' | |
}); | |
$sp.getCustomActions(); // Check the console. | |
$sp.getCustomActions({ | |
type: 'web', | |
render: function (options, customActions) { | |
// Implement your own rendering. | |
// Useful if building tools on page. | |
} | |
}); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment