Skip to content

Instantly share code, notes, and snippets.

@CesarCapillas
Last active September 10, 2024 14:15
Show Gist options
  • Save CesarCapillas/6f7692a1d657ce43851598ca9d5ecaa1 to your computer and use it in GitHub Desktop.
Save CesarCapillas/6f7692a1d657ce43851598ca9d5ecaa1 to your computer and use it in GitHub Desktop.
Alfresco Javascript Console Recipes
// Export ACP
var nodeToExport = companyhome.childByNamePath("Sites/swsdp/documentLibrary")
var exportAction= actions.create("export");
exportAction.parameters['store'] = "workspace://SpacesStore";
exportAction.parameters['package-name'] = "ACPexport";
exportAction.parameters['destination'] = companyhome;
exportAction.parameters['include-children'] = true;
exportAction.parameters['include-self'] = false;
exportAction.parameters['encoding'] = "UTF-8";
exportAction.execute(nodeToExport);
var ctxt, synchronizer;
ctxt = Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
synchronizer = ctxt.getBean('userRegistrySynchronizer', Packages.org.alfresco.repo.security.sync.UserRegistrySynchronizer);
// forceUpdate = false, isFullSync = false - change for full update / with deletions
synchronizer.synchronize(false, false);
var ctxt = Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
var properties = ctxt.getBean('global-properties', java.util.Properties);
logger.log(properties["db.user"]);
var def = {
query: "ASPECT:'qshare:shared'",
language: "fts-alfresco"
};
var nodes = search.query(def);
for each(var node in nodes) {
logger.log(node.nodeRef + " ; " + node.properties["qshare:sharedId"] + " ; " + node.webdavUrl);
}
var users = people.getPeople(null);
for each(user in users){
user = search.findNode(user);
logger.log(user.properties.userName+"," + user.properties.firstName+"," + user.properties.lastName + ","+user.properties.email);
}
// Import ACP
var targetNodeForImport = companyhome.childByNamePath("Sites/swsdp2/documentLibrary");
var ACPFile = companyhome.childByNamePath("ACPexport.acp");
var importAction = actions.create("import");
importAction.parameters.encoding = "UTF-8";
importAction.parameters.destination = targetNodeForImport;
importAction.execute(ACPFile);
Import a script from the repository using a name-based path:
<import resource="/Company Home/Data Dictionary/Scripts/library.js">
Import a script from the repository using a NodeRef reference:
<import resource="workspace://SpacesStore/6f73de1b-d3b4-11db-80cb-112e6c2ea048">
Import a script from a Java classpath location:
<import resource="classpath:alfresco/extension/myutils.js">
var s = siteService.getSites("", "", 0);
s.forEach(function(entry) {
var members = entry.listMembers(null, "", 0, false);
for (userName in members) {
logger.log(entry.shortName+ " :: "+ userName +" :: "+entry.getMembersRole(userName));
}
});
var ctxt, scheduler;
// get Spring context and Quartz scheduler
ctxt = Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
scheduler = ctxt.getBean('schedulerFactory', Packages.org.quartz.Scheduler);
// For custom js based job - public urls for expiration
//scheduler.triggerJob('jobD', 'jobGroup');
// For LDAP sync
// fire (unless explicitly defined in Job detail Spring bean, scheduler group is always DEFAULT)
scheduler.triggerJob('ldapPeopleJobDetail', 'DEFAULT');
function sendMail() {
var site = siteService.getSite(document.siteShortName);
var members = site.listMembers(null, "SiteManager", 0, false);
for (userName in members) {
var person = people.getPerson(userName);
var email = person.properties["cm:email"];
var mail = actions.create("mail");
mail.parameters.to = email;
mail.parameters.subject = "New document available: " + document.name;
mail.parameters.template =
companyhome.childByNamePath("Data Dictionary/Email Templates/Notify Email Templates/notify_user_email.html.ftl");
mail.execute(document);
}
}
sendMail();
var context = Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
var model = Packages.org.alfresco.model.ContentModel;
var policyBehaviourFilter = context.getBean('policyBehaviourFilter', Packages.org.alfresco.repo.policy.BehaviourFilter);
setProperty();
function setProperty() {
var referenceType = "node";
var reference = ["workspace", "SpacesStore", "a8abc98e-bf2d-48ec-b3d8-002fa0cc7b97"];
var node = search.findNode(referenceType, reference);
logger.log(node.name);
try {
logger.log("Modifiying node: " + node.name + " " + node.nodeRef);
policyBehaviourFilter.disableBehaviour(node.nodeRef, model.ASPECT_AUDITABLE);
node.properties['cm:title'] = 'Modified just now';
node.save();
} catch (e) {
logger.log(e.message);
} finally {
policyBehaviourFilter.enableBehaviour(node.nodeRef, model.ASPECT_AUDITABLE);
//Log modifier and modified date
logger.log("Modifier: " + node.properties['cm:modifier'] + " Modified: " + node.properties['cm:modified']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment