cancel
Showing results for 
Search instead for 
Did you mean: 

Create a file inside repository via Java backed Web Script.

cile87
Champ on-the-rise
Champ on-the-rise
I'm using Alfresco 4.0 version.

I'm trying to write a webscript which creates a new file inside alfresco repository (inside folder "/Company Home/myfolder"), but I can't find a way to do it Smiley Sad

By following http://wiki.alfresco.com/wiki/Java-backed_Web_Scripts_Samples examples, I managed to write a webscript which returns a content of a file to user, but how can a java backed webscript generate and store a new (binary) file inside Alfresco repository.

I read a lot of examples and forum posts, but none seem to work for this Alfresco version, or at least I didn't realize how to do it.

I'm using something like:
NodeRef companyHome = repository.getCompanyHome();
List<String> nodes = new ArrayList<String>();
nodes.add("myfolder");

NodeRef where = registry.getFileFolderService().resolveNamePath(companyHome, nodes).getNodeRef();
registry.getFileFolderService().create(where, "myfile",QNAME);
but I don't know what to put for QName parameter. It can't be null.

Thanks.
3 REPLIES 3

cile87
Champ on-the-rise
Champ on-the-rise
I managed to create an empty file:

import …
import org.alfresco.model.ContentModel;
import org.alfresco.repo.model.Repository;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;

private ServiceRegistry registry;

NodeRef companyHome = repository.getCompanyHome();
List<String> nodes = new ArrayList<String>();
nodes.add("myfolder");

NodeRef where = registry.getFileFolderService().resolveNamePath(companyHome, nodes).getNodeRef();
registry.getFileFolderService().create(where, "myfile", ContentModel.TYPE_CONTENT);
but now I'm trying to write an byte stream into that file. In fact, what I'm trying to do, is store serilized java object inside Alfresco repository.

cile87
Champ on-the-rise
Champ on-the-rise
YEY! I discovered how to put a content:
ContentWriter writer = registry.getFileFolderService().getWriter(createdFile);
writer.putContent("lorem ipsum dolorem");
putContent method also receives an InputStream so it shouldn't be a problem to work with java serialization.


Once you discover it, it looks so darm simple.

openpj
Elite Collaborator
Elite Collaborator
An alternative way to create a new node in the repository is to use the Alfresco Foundation API using the NodeService and the ContentService.

From the registry you could get the NodeService and create the node for the content with all the properties.
Then you can use the ContentService to write the byte stream for the node.

Alfresco Foundation API:
http://wiki.alfresco.com/wiki/Java_Foundation_API

Anyway if you download the Alfresco SDK you will find many examples about how to use the Alfresco Foundation API, it could be very useful to understand all the features and operations that you can execute against the repository.

Hope this helps.