在Alfresco存储库中创建新文件夹的脚本

时间:2017-07-24 13:59:12

标签: alfresco alfresco-webscripts

我想创建一个允许我在Alfresco存储库中创建新文件夹的脚本,但我不知道如何实现这一点。

有没有人可以告诉我如何管理它。

很抱歉没有发布任何代码,因为我对alfresco很新,我不知道如何管理它。

2 个答案:

答案 0 :(得分:4)

远程创建文件夹(或执行其他CRUD功能)的最佳方法是使用CMIS,例如从命令行程序创建文件夹。根据您希望使用的语言,有许多CMIS客户端实现。这些是在Apache Chemistry project处理的。

以下是使用cmislib(Python CMIS客户端)创建文件夹的示例:

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

这是一个使用Groovy的Java实现OpenCMIS的例子:

@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);
}

在Groovy示例中,我正在登录并获取票证,以便我可以进行CMIS和非CMIS呼叫,但在此示例中我没有显示任何非CMIS呼叫。

答案 1 :(得分:1)

var nodeNew = parentNode.createFolder("Name of folder");

上面的代码将使用alfresco javascript创建文件夹.parentNode是Node的一个对象。

以下链接有更多详细信息。

https://community.alfresco.com/thread/166358-webscript-to-create-folder-space