Book Image

Alfresco Developer Guide

Book Image

Alfresco Developer Guide

Overview of this book

Table of Contents (17 chapters)
Alfresco Developer Guide
Credits
About the Author
About the Reviewers
Preface
Index

API Examples


Step-by-step examples for working with the various Alfresco APIs are provided throughout the book. But the examples are limited in scope to the specific requirement at hand. This section shows how to perform the same functional task (for example, "Create an object") using the three major APIs: Foundation, JavaScript, and Web Services.

Create a Node

These examples show how to create a new node in the repository.

Foundation

ChildAssociationRef association = 	nodeService.createNode(companyHome, 
   ContentModel.ASSOC_CONTAINS, 
   QName.createQName(NamespaceService.CONTENT_MODEL_PREFIX,
   name),
   ContentModel.TYPE_CONTENT,
   contentProps);

JavaScript

var newNode = space.createNode(nodeName, contentType);

Web Services

// Construct CML statement to create content node            
CMLCreate createDoc = new CMLCreate("ref1", docParent, null, null, null, Constants.createQNameString(SomeCoModel.NAMESPACE_SOMECO_CONTENT_MODEL, getContentType()), contentProps);

// Construct CML Block
CML cml = new CML();
cml.setCreate(new CMLCreate[] {createDoc});
        
// Execute CML Block
UpdateResult[] results = WebServiceFactory.getRepositoryService().update(cml);  

Perform a Search

These examples show how to execute a Lucene search.

Foundation

StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
ResultSet resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, queryString);

JavaScript

var queryResults = search.luceneSearch(queryString);

Web Services

Query query = new Query(Constants.QUERY_LANG_LUCENE, queryString );
QueryResult queryResult = getRepositoryService().query(getStoreRef(), query, false);

Persist Content

These examples show how to write content to a node.

Foundation

ContentWriter writer = contentService.getWriter(content, ContentModel.PROP_CONTENT, true);
writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
writer.setEncoding("UTF-8");
String text = "The quick brown fox jumps over the lazy dog";
writer.putContent(text);

JavaScript

var newDoc = targetFolder.createFile(filename);
newDoc.properties.content.write(content);
newDoc.properties.content.mimetype = mimetype;
newDoc.save();

Web Services

ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
ContentFormat contentFormat = new ContentFormat("text/plain", "UTF-8");
String docText = "This is a sample " + getContentType() + " document called " + getContentName();
Content docContentRef = contentService.write(docRef, Constants.PROP_CONTENT, docText.getBytes(), contentFormat);

Add an Aspect

These examples show how to add an aspect to a node.

Foundation

nodeService.addAspect(nodeRef, aspectQName, props);

JavaScript

document.addAspect(aspectQName, props);

Web Services

CMLAddAspect addWebableAspectToDoc = new CMLAddAspect(Constants.createQNameString(SomeCoModel.NAMESPACE_SOMECO_CONTENT_MODEL, SomeCoModel.ASPECT_SC_WEBABLE), null, null, "ref1");
// Construct CML Block
CML cml = new CML();
cml.setUpdate(new CMLUpdate[] {updateDoc});
cml.setAddAspect(new CMLAddAspect[] {addWebableAspectToDoc});
// Execute CML Block
UpdateResult[] results = WebServiceFactory.getRepositoryService().update(cml);

Set a Property

These examples set a single-value property on a node.

Foundation

nodeService.setProperty(nodeRef, propertyQName, value);

JavaScript

document.properties["sc:published"] = new Date();
document.save();

Web Services

// Create a reference to the doc to be updated
Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
Reference doc = new Reference(storeRef, this.targetUuid, null);
            
// Create an array of NamedValue objects with the props to set
NamedValue nameValue = Utils.createNamedValue(Constants.PROP_NAME, this.contentName);
NamedValue[] contentProps = new NamedValue[] {nameValue};

// Construct CML statement to update node            
CMLUpdate updateDoc = new CMLUpdate(contentProps, new Predicate(new Reference[] {doc}, storeRef, null), null);

// Construct CML Block
CML cml = new CML();
cml.setUpdate(new CMLUpdate[] {updateDoc});

// Execute CML Block
UpdateResult[] results = WebServiceFactory.getRepositoryService().update(cml);

Set Permissions

These examples grant the custom PortalPublisher permission created in Chapter 9 to the Publisher group for a specific node.

Foundation

permissionService.setPermission(nodeRef, "GROUP_Publisher", "PortalPublisher", true);

JavaScript

document.setPermission("PortalPublisher", "GROUP_Publisher")

Web Services

Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
Reference doc = new Reference(storeRef, this.targetUuid, null);
ACE ace = new ACE("GROUP_Publisher", "PortalPublisher", AccessStatus.acepted);
WebServiceFactory.getAccessControlService().addACEs(new Predicate(new Reference[] {doc}, storeRef, null), new ACE[] {ace});

Start a Workflow

These examples start the out of the box "adhoc" workflow, assign it to tuser1 and give it a description. In 2.2 Enterprise, there is no workflow API for JavaScript for Web Services. To work around that, the code simply executes the start-workflow action.

Foundation

Map<QName, Serializable> workflowParameters = new HashMap<QName,Serializable>();
workflowParameters.put(QName.createQName("bpm", "assignee"), "tuser1");
workflowParameters.put(QName.createQName("bpm", "description"), "Started from Foundation");
workflowService.startWorkflow("jbpm$wf:adhoc", workflowParameters);

JavaScript

var workflow = actions.create("start-workflow");

workflow.parameters.workflowName = "jbpm$wf:adhoc";

workflow.parameters["bpm:workflowDescription"] = "Workflow from JavaScript";

workflow.parameters["bpm:assignee"] = "tuser1";

workflow.execute(document);

Web Services

Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
Reference doc = new Reference(storeRef, this.targetUuid, null);
            
NamedValue workflowValue = Utils.createNamedValue("workflowName", this.workflow);
NamedValue descriptionValue = Utils.createNamedValue("bpm:workflowDescription", "Submitted from web service");
NamedValue assigneeValue = Utils.createNamedValue("bpm:assignee", this.assignee);
NamedValue[] actionArguments = new NamedValue[] {workflowValue, descriptionValue, assigneeValue};
            
Action startWorkflowAction = new Action(null, this.targetUuid, "start-workflow", null, null, actionArguments, null, null, null);
            
WebServiceFactory.getActionService().executeActions(new Predicate(new Reference[] {doc}, storeRef, null), new Action[] {startWorkflowAction});