cancel
Showing results for 
Search instead for 
Did you mean: 

NodeRef syntax in JavaScript API

simon
Champ in-the-making
Champ in-the-making
Hi Alfresco,

I'm trying to add categories to a document but started with a locale as this seems less complex to start with. Problem is I need to assign a nodeRef to the property but how should this nodeRef look like? In the node browser a nodeRef looks like workspace://SpacesStore/666345ce-1d81-11db-b398-13c0b9d26600 but it isn't accepted in my JavaScript. What am I doing wrong?

var folder = companyhome;
var copy = document.copy(folder);
if (copy != null)
{
   copy.name = "new_";
   // Add the English category to the document
   var nodeRef = "workspace://SpacesStore/666345ce-1d81-11db-b398-13c0b9d26600";
   document.properties["cm:locale"] = nodeRef;
   copy.save();
}
4 REPLIES 4

kevinr
Star Contributor
Star Contributor
The JavaScript API does not deal with NodeRef values - everything is wrapped in an OO style API, so it deals with scriptable Node objects. This means any 'd:noderef' property value should be assigned a Node object directly.

So you need to get hold the Node that represents your category - by query i expect or from an existing Node that has the category you need already assigned.


var folder = companyhome;
var copy = document.copy(folder);
if (copy != null)
{
   copy.name = "new_";
   // Add the English category to the document
   var node = search.luceneSearch(……)[0];
   document.properties["cm:locale"] = node;
   copy.save();
}

Thanks,

Kevin

simon
Champ in-the-making
Champ in-the-making
Thanks Kevin, it works like it should! I can add a locale with JavaScript now. One small extra question… how to realize this for collection aspects, the generalclassifiable for example?
var nodeRef = search.luceneSearch("TYPE:\"{http://www.alfresco.org/model/content/1.0}category\" AND  PATH:\"/cm:generalclassifiable//cm:MyCategory\"")[0];
copy.properties["cm:generalclassifiable"] = nodeRef;
Doesn't work because the copy.properties excepts a collection element. Tried to remove the index ([0]) at the end of the nodRef and to add an index at the end of the copy.properties but this doesn't solve it.

How do I add this nodeRef without removing the other categories?

kevinr
Star Contributor
Star Contributor
The 1.4 JavaScript engine now correctly supports multi-value properties - so you will be able to do this:


var categories = new Array(1);
var nodeRef = search.luceneSearch("TYPE:\"{http://www.alfresco.org/model/content/1.0}category\" AND  PATH:\"/cm:generalclassifiable//cm:MyCategory\"")[0];
categories[0] = nodeRef;
// … add further categories here to the array
copy.properties["cm:generalclassifiable"] = categories;

So to set a multi-value property, you simply fill an array with the appropriate object type and assign that.

Thanks,

Kevin

simon
Champ in-the-making
Champ in-the-making
Thanks Kevin, again something that I'll try out once we get to the 1.4 release.