Add a category to a document with Javascript
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2007 03:23 PM
//Get category nodevar nodeRef = search.luceneSearch("…");//Get categories from filevar cats = file.properties["cm:categories"];//Add new categorycats.push(nodeRef);//Update file categoriesfile.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
- Labels:
-
Archive

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2007 08:27 AM
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 nodevar nodeRef = search.luceneSearch("…");//Get categories from filevar cats = file.properties["cm:categories"];//Add new categoryvar newCats = new Array();for (var i=0; i<cats.length; i++){ newCats[i] = cats[i];}newCats.push(nodeRef);//Update file categoriesfile.properties["cm:categories"] = newCats;
Hope this helps,
Kevin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2007 01:00 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2007 07:18 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2007 08:26 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2007 08:57 AM
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();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2007 09:07 AM
var catNodeRef = search.luceneSearch("…");
do like this:
var catNodeRef = search.luceneSearch("…")[0];
-Nick P
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2007 10:05 AM
Fantastic help there Nick - works like a charm now.
Many thanks indeed,
Wayne

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2007 11:55 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2007 01:20 PM
