Reputation: 223
I want to create a script who allow me to create a new folder in Alfresco repository, but i haven't any idea to how achieve this.
Is there anyone who can tell me how to manage this.
Sorry for not posting any code, because i'm very new to alfresco and i haven't idea how to manage this.
Upvotes: 0
Views: 1280
Reputation: 10538
The best way to create a folder (or perform other CRUD functions) remotely, such as from a command line program, is to use CMIS. There are a number of CMIS client implementations depending on what language you'd like to use. These are managed at the Apache Chemistry project.
Here is an example that uses cmislib, a Python CMIS client, to create a folder:
from cmislib.model import CmisClient
from cmislib.browser.binding import BrowserBinding
client = CmisClient('http://localhost:8080/alfresco/api/-default-/cmis/versions/1.1/browser', 'admin', 'admin', binding=BrowserBinding())
repo = client.defaultRepository
folder = repo.getObjectByPath("/User Homes")
createdFolder = folder.createFolder("another test folder")
print "Done, created: %s" % createdFolder.id
Here is an example that uses OpenCMIS, a Java implementation, from Groovy:
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
@Grab(group="org.apache.chemistry.opencmis", module="chemistry-opencmis-commons-api", version="0.13.0")
@Grab(group="org.apache.chemistry.opencmis", module="chemistry-opencmis-commons-impl", version="0.13.0")
@Grab(group="org.apache.chemistry.opencmis", module="chemistry-opencmis-client-api", version="0.13.0")
@Grab(group="org.apache.chemistry.opencmis", module="chemistry-opencmis-client-impl", version="0.13.0")
@Grab(group="org.apache.chemistry.opencmis", module="chemistry-opencmis-client-bindings", version="0.13.0")
import org.apache.chemistry.opencmis.commons.*;
import org.apache.chemistry.opencmis.commons.enums.*;
import org.apache.chemistry.opencmis.client.*;
import org.apache.chemistry.opencmis.client.api.*;
import org.apache.chemistry.opencmis.client.runtime.*;
import org.apache.chemistry.opencmis.commons.data.*;
import org.apache.chemistry.opencmis.commons.impl.dataobjects.*;
import org.apache.chemistry.opencmis.commons.exceptions.*;
import java.nio.file.Paths
import groovy.json.JsonOutput
import groovyx.net.http.RESTClient
import static groovyx.net.http.ContentType.JSON
import java.text.NumberFormat;
def ALF_SERVICE = '/alfresco/s'
def CMIS_SERVICE = '/alfresco/api/-default-/public/cmis/versions/1.1/browser'
final BindingType CMIS_BINDING = BindingType.BROWSER;
// Get options
def url = System.console().readLine('Alfresco server URL [http://localhost:8080]: ')
if (url == null || url == '') url = "http://localhost:8080"
def folderPath = System.console().readLine('Folder path [/User Homes]: ')
if (folderPath == null || folderPath == '') folderPath = "/User Homes"
def folderName = System.console().readLine('Folder name to create: ')
def userName = System.console().readLine('Your username: ')
def password = System.console().readPassword('Your password: ')
println 'WARNING: About to modify folders on ' + url + ' as ' + userName + '.'
def confirm = System.console().readLine('Are you sure (Y/N): ')
if (confirm.toLowerCase() != 'y' && confirm.toLowerCase() != 'yes') {
println "Quitting"
System.exit(0)
}
// Login to Alfresco
def client = new RESTClient(url)
def resp = client.get(path : ALF_SERVICE + '/api/login', query: ['u': userName, 'pw': password.toString(), 'format': 'json'])
assert resp.status == 200
def ticket = resp.data.data.ticket
println "Successfully logged in to Alfresco..."
// Leave the username as an empty string to auth with a ticket
Session session = createCMISSession(url + CMIS_SERVICE, CMIS_BINDING, "", ticket);
Folder folder = findFolder(session, folderPath)
if (folder == null) {
println "ERROR: Could not find: " + folderPath
System.exit(0)
}
println "Found: " + folder.name + " (" + folder.id + ")"
Map<String,String> newFolderProps = new HashMap<String, String>()
newFolderProps.put("cmis:objectTypeId", "cmis:folder");
newFolderProps.put("cmis:name", folderName);
Folder createdFolder = folder.createFolder(newFolderProps)
println "Done, created: " + createdFolder.id
Session createCMISSession(final String cmisEndpointUrl,
final BindingType cmisBinding,
final String cmisUser,
final String cmisPassword) {
SessionFactory sf = SessionFactoryImpl.newInstance();
Session result = null;
Map<String, String> parameters = new HashMap<String, String>();
parameters.put(SessionParameter.BINDING_TYPE, cmisBinding.value());
parameters.put(SessionParameter.BROWSER_URL, cmisEndpointUrl);
parameters.put(SessionParameter.USER, cmisUser);
parameters.put(SessionParameter.PASSWORD, cmisPassword);
// Note: grabbing the first repository may not work as expected on multi-tenant Alfresco (most notably Cloud)
result = sf.getRepositories(parameters).get(0).createSession();
return (result);
}
Folder findFolder(Session session, String folderPath) {
Folder result = null;
try {
CmisObject folder = session.getObjectByPath(folderPath);
if (folder != null &&
BaseTypeId.CMIS_FOLDER.equals(folder.getBaseTypeId())) {
result = (Folder) folder;
}
} catch (CmisObjectNotFoundException confe) {
// Swallow and move on - we return null in this case
println "ERROR: getObjectByPath threw a CmisObjectNotFoundException"
}
return (result);
}
In the Groovy example, I am logging in and getting a ticket so that I can make both CMIS and non-CMIS calls, although I am not showing any non-CMIS calls in this example.
Upvotes: 4
Reputation: 3175
var nodeNew = parentNode.createFolder("Name of folder");
Above code will create folder using alfresco javascript.parentNode is an object of Node.
Below link have some more details on it.
https://community.alfresco.com/thread/166358-webscript-to-create-folder-space
Upvotes: 1