Reputation: 483
I am having a hard time looking for an answer to my question. I am trying to create a folder/space in Alfresco. But I don't have any idea doing it? Can someone help me with this? I'm using Java webscript.
All I am at is this:
package org.alfresco.module.demoscripts;
import java.io.IOException;
import org.alfresco.web.scripts.AbstractWebScript;
import org.alfresco.web.scripts.WebScriptException;
import org.alfresco.web.scripts.WebScriptRequest;
import org.alfresco.web.scripts.WebScriptResponse;
import org.json.JSONException;
import org.json.JSONObject;
public class SimpleWebScript extends AbstractWebScript
{
public void execute(WebScriptRequest req, WebScriptResponse res)
throws IOException
{
try
{
// build a json object
JSONObject obj = new JSONObject();
// put some data on it
obj.put("field1", "data1");
// build a JSON string and send it back
String jsonString = obj.toString();
res.getWriter().write(jsonString);
}
catch(JSONException e)
{
throw new WebScriptException("Unable to serialize JSON");
}
}
}
Upvotes: 1
Views: 3585
Reputation: 10538
As was pointed out, you may want to start by using JavaScript instead of Java. Also, I'm noticing that you are creating a new JSON object in your code, but I'm not sure why.
For example, using JavaScript, a web script that accepts the name of a folder to be created under company home might have a controller that would look like this:
function main() {
var folderName = args.folderName;
if (folderName != null && folderName != "") {
// continue
} else {
status.setCode(500);
status.setMessage("Missing folder name argument");
status.setRedirect(true);
return;
}
var createdFolder = companyhome.createFolder(folderName);
model.createdFolder = createdFolder;
}
main();
The code looks for an argument passed to it called folderName and then uses the built-in "companyhome" root scoped variable to create the new folder. It then passes the newly-created folder node to the model so that some data about the new node can be echoed back to the user.
The name of the JavaScript controller (shown above) is createFolder.post.js. I checked it in to the repository under Data Dictionary/Web Scripts/example. Along with that, I checked in a file called createFolder.post.desc.xml, which is the web script descriptor:
<webscript>
<shortname>Create folder example</shortname>
<description>
<![CDATA[
Creates a new folder in Company Home using the name specified in the folderName argument.
]]>
</description>
<url>/example/createFolder?folderName={folderName}</url>
<format default="html">argument</format>
<authentication>user</authentication>
<transaction>required</transaction>
<family>Example</family>
</webscript>
And a file called createFolder.post.html.ftl, which is the HTML version of the web script view:
<html>
<head>
<title>Create folder web script</title>
</head>
<body>
Successfully created a folder with the following metadata:<br />
Name: ${createdFolder.name}<br />
ID: ${createdFolder.id}<br />
Noderef: ${createdFolder.nodeRef}<br />
</body>
</html>
Once checked in, I refreshed the web script index by going to http://localhost:8080/alfresco/s/index and clicking the "refresh web scripts" button.
I then invoked the web script using CuRL, like this:
curl -uadmin:admin -X POST "http://localhost:8080/alfresco/s/example/createFolder?folderName=foobar"
You'll notice that I chose to make my web script accept POSTs. If you wanted to use a different HTTP method, you'd simply change the file names accordingly.
Jeff
Upvotes: 2
Reputation: 48326
If you're new to both Java and Alfresco, you may well find that writing your WebScript in JavaScript is a better bet for you. It's normally easier to get started with. There are lots of examples too to help you.
If you do decide to stick with Java, the starting points in the Alfresco wiki for you are Java Backed WebScripts and the Java Foundation API. One of the Java webscript examples show you how to create nodes (you can use either the NodeService or the FileFolderService, depending on if you want full control or an easy way).
You might also want to look at some of the advice in the Alfresco Wiki and on the forums for how to structure your data, as creating lots of new nodes straight under Company Home may or may not be the best option for you.
Upvotes: 2