Create a file inside repository via Java backed Web Script.

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2011 06:38 PM
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
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:
Thanks.
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

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.
Labels:
- Labels:
-
Archive
3 REPLIES 3

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2011 08:46 PM
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.

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2011 09:10 PM
YEY! I discovered how to put a content:
–
Once you discover it, it looks so darm simple.
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.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2011 04:01 AM
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.
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.
