cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing WCM Content through Web Scripts

himanshu
Champ in-the-making
Champ in-the-making
Hi,
I am a newbie to alfresco. I have created a sample wcm project and want to create a web script that will return me the contents of a specified file.
I saw an example in the forum for the same that returned the contents of a file in Guest Home. Now when i try to access the files in my web project i can either
1) Traverse the node via the path.
The script for that I used for accessing the file Guest Home directory is :

var data = companyhome.childByNamePath("/Guest Home/a.xml");
if(data == null ){
logger.log("No files found");
      status.code = 404;
      status.message = "No files found";
      status.redirect = true;
} else {

model.storeid = data.getId();
   model.dataRetrieved = data;
}
2)The second way could be performing a lucene search for the ID of that file.
The code for the same is:

var files = search.luceneSearch("ID:\"workspace://SpacesStore/0d18339a-1c6e-4b92-975f-1d9004e96264\"");

if (files == null || files.length == 0) {
      logger.log("No files found");
      status.code = 404;
      status.message = "No files found";
      status.redirect = true;
} else {
          var data = files[0];
          model.storeid = data.getId();
   model.dataRetrieved = data;
}
Now both the codes work fine for the file in the guest home directory.
But when i try to perform a lucene search for a file in my wcm directory (again with the help of its storeid), it gives me no file found and i have no idea how to get the name path for that particular file!!!!!
Please help!!!!!!!!
3 REPLIES 3

mark_smithson
Champ in-the-making
Champ in-the-making
Hi himanshu,

Content stored in WCM is stored in a different type of repository (AVM) so you have to access it differently.

Code like this below has worked for us:


script: {
   var store="youwebapp–sandboxname";
   var contentPath ="/www/avm_webapps/ROOT/path/to/your/content";
   if (storeExists(store)) {   
      var node = avm.lookupNode(store + ":" + contentPath);
           if (node == null) {
              status.code = 404;
         status.message = store + " could not be found.";
         status.redirect = true;
         return;
           }
           model.data = node;
   }
}

function storeExists(store){
   var storeRoot = avm.lookupStore(store);
   if (storeRoot == null){
      status.code = 404;
      status.message = store + " could not be found.";
      status.redirect = true;
      return false;
   }   
   return true;
}

himanshu
Champ in-the-making
Champ in-the-making
Hi Mark,

Thanks for the help. The code works Smiley Happy.

I was now trying to do the same thing with java backed web scripts. But as usual, I am again stuck with which classes to use.

I was able to access a file in my web project by instantiating an object of the NodeRef class with that file's "Alfresco Node Reference" value.

But i want to access it by using the name (or the path) of the file and not its reference. My tasks include retrieving the list of the files in a particular folder(space) within the web project.

Thanks again.

Regards
Himanshu

mark_smithson
Champ in-the-making
Champ in-the-making
Hi Himanshu,

Is there a particular reason why you need to use a Java backed web script? Having the script in JS and FTL gives you a lot of flexibility and we have found performance to be acceptable.

There is API documentation in the wiki - http://wiki.alfresco.com/wiki/Alfresco_Content_Management_Java_API.

I have also found it useful to look at the java code for the webscript objects, usch as ScriptNode in the SDK.

Regards

Mark
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.