cancel
Showing results for 
Search instead for 
Did you mean: 

Add a category to a document with Javascript

nparry
Champ in-the-making
Champ in-the-making
How do you add a category to a document with javascript?  I have done something like the following:


//Get category node
var nodeRef = search.luceneSearch("…");

//Get categories from file
var cats = file.properties["cm:categories"];

//Add new category
cats.push(nodeRef);

//Update file categories
file.properties["cm:categories"] = cats;

I'm using v2.0.  What am I doing wrong here?  I'm getting an error that says

Java class "java.lang.Object" has no public instance method or field named "1".

Thanks a lot,
Nick P
9 REPLIES 9

kevinr
Star Contributor
Star Contributor
In Alfresco 2.0 the various JavaScript API objects mostly did not return "real" JavaScript Array objects, instead they returned simple java List objects, which are accessable by index but do not support the Array functions such as push(). This has been fixed in 2.1 and all appropriate objects return real Array objects.

For 2.0 you need to do something like this to copy the existing values to a new Array and add the new value:

//Get category node
var nodeRef = search.luceneSearch("…");

//Get categories from file
var cats = file.properties["cm:categories"];

//Add new category
var newCats = new Array();
for (var i=0; i<cats.length; i++)
{
   newCats[i] = cats[i];
}
newCats.push(nodeRef);

//Update file categories
file.properties["cm:categories"] = newCats;

Hope this helps,

Kevin

nparry
Champ in-the-making
Champ in-the-making
Thanks, Kevin.  Works great.

wayne
Champ in-the-making
Champ in-the-making
Is it just me, or does this solution not work?

I'm trying to do much the same thing as nparry, again using alfresco 2.0.

If I try to set the categories property using an array of nodeRefs to the categories I want, the result is an exception saying that alfresco can't convert between an ArrayList and a NodeRef (it would seem it's not expecting an array here).

I can assign single categories fine by just assigning the nodeRef of the category directly, but this overwrites any categories that have previously been set.

Is it just the case that you can't append categories using javascript?


Thanks,
Wayne

nparry
Champ in-the-making
Champ in-the-making
Wayne,

I did pretty much exactly what Kevin showed, and it worked well.  It's sounds like you may be assigning the new category node ref into an array, but it's hard to say for sure.  Post your javascript and let's have a look at it.

-Nick P

wayne
Champ in-the-making
Champ in-the-making
Thanks for the quick response.

Here's the interesting part of the javascript:


var catNodeRef = search.luceneSearch("…");

var cats = document.properties["cm:categories"];

var newCats = new Array();
for (var i=0; i < cats.length; i++)
{
      newCats[i] = cats[i];
}
newCats.push(catNodeRef);

document.properties["cm:categories"] = newCats;
document.save();

nparry
Champ in-the-making
Champ in-the-making
Ah, I think I see the problem.  search.luceneSearch() returns an array of Nodes that represent all the search matches.  Even if the search only returns one node, it will still give it to you in array form.  Instead of

var catNodeRef = search.luceneSearch("…");

do like this:

var catNodeRef = search.luceneSearch("…")[0];

-Nick P

wayne
Champ in-the-making
Champ in-the-making
What an annoying thing for me to have missed!!

Fantastic help there Nick - works like a charm now.

Many thanks indeed,

Wayne

lock999
Champ in-the-making
Champ in-the-making
Hi,

When i try to assign a category that has a space within the category  name, it throws an error shown below.

Example1: This code throws an error. Please note the space in category name "Northern America". Code:
search.luceneSearch("PATH:\"/cm:generalclassifiable//cm:Northern America\"")[0]

Example 2: This code does not throw an error. Please note there are no spaces in the category name "Europe". Code:
search.luceneSearch("PATH:\"/cm:generalclassifiable//cm:Europe\"")[0]


Error:
Failed to run Actions due to error: Failed to execute script 'workspace://SpacesStore/6f5b4e4a-502c-11dc-936f-f31ee0725da9': Failed to execute script 'workspace://SpacesStore/6f5b4e4a-502c-11dc-936f-f31ee0725da9': Wrapped org.alfresco.service.cmr.repository.datatype.TypeConversionException: The property value is not compatible with the type defined for the property: property: {http://www.alfresco.org/model/content/1.0}categories value: [workspace://SpacesStore/fe6f9bbd-41dc-11dc-8596-ef2a34b171a0, workspace://SpacesStore/0558b6b3-41dd-11dc-8596-ef2a34b171a0, workspace://SpacesStore/17bd92ff-41dd-11dc-8596-ef2a34b171a0, workspace://SpacesStore/054ccfcc-41dd-11dc-8596-ef2a34b171a0, org.mozilla.javascript.Undefined@5a9737] value type: class java.util.ArrayList (AlfrescoScript#18)

Any clues?
Thanks

lock999
Champ in-the-making
Champ in-the-making
Found the solution: Replace space by '_x0020_'.