cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing Aspects with JavaScript

slothrop
Champ in-the-making
Champ in-the-making
I have a custom aspect defined as follows:
   <aspects>
      <aspect name="custom:Reviewers">
         <title>Reviewers</title>
         <properties>
            <property name="custom:Reviewer1">
               <title>Reviewer1</title>
               <type>d:text</type>
            </property>
       <property name="custom:Reviewer2">
               <title>Reviewer2</title>
               <type>d:text</type>
            </property>
       <property name="custom:Reviewer3">
               <title>Reviewer3</title>
               <type>d:text</type>
            </property>
         </properties>
      </aspect>
   </aspects>

The aspect appears as it should and I can add reviewers to documents with no problem.  I would like to write a script that accesses these reviewers and sends emails to them when the document is moved to a certain space. 

document.hasAspect("custom:Reviewers") returns true as it should but document.aspects.length is undefined as is document.aspects.properties. 

document.aspects.size() returns 6 but I can't step through the aspects with document.aspects.properties since document.aspects.properties is undefined.  I can get an iterator with

   var docAspects = document.aspects;
   var it = docAspects.iterator();

but it.next() doesn't seem to return anything I can use.  One of the objects returned with it.next() does have a toString() that returns "{custom.model}Reviewers" but it.next().properties is undefined.

What am I missing?

Thanks in advance.
10 REPLIES 10

davidc
Star Contributor
Star Contributor
Are you using Alfresco 2.1?

slothrop
Champ in-the-making
Champ in-the-making
Are you using Alfresco 2.1?

Yes.

It looks like document.aspects returns a Java HashSet but the objects returned by an iterator do not have the properties defined in the aspect, Reviewers.  There is no such thing as casting in JavaScript, right?

davidc
Star Contributor
Star Contributor
The aspects property just returns the names of the aspects applied to the node.

You can retrieve properties (defined by an aspect) using the standard doc.properties collection.

e.g. reviewer = document.properties["custom:Reviewers"];

slothrop
Champ in-the-making
Champ in-the-making
document.properties["custom:Reviewers"] returns null even though "if(document.hasAspect("custom:Reviewers"))" returns true.

document.properties returns an array of length 17.  When I step through the array:

var propertyAccumulator;
for(i=0; i<l; i++)
{
   s = new String(document.properties);
   if(s != null && s.indexOf("Reviewer") >= 0)
   {
      name = s;
   }
   propertyAccumulator = propertyAccumulator + " " + s;
}

where l = document.properties.length, s is always null.  If instead of s = new String(document.properties) I do s = document.properties.toString() I get an error that says "can't call method toString() on null," of course (just trying to cover all bases here).

slothrop
Champ in-the-making
Champ in-the-making
After trying everything I can think of, I am forced to conclude that document aspects cannot be accessed by JavaScript.  This includes not just custom aspects but also the Alfresco-defined aspects like inlineeditable, copiedfrom, etc.  I have tried every variation of document.properties["custom:Reviewers"] that I can imagine.  I can access Alfresco-defined properties like name, creator, title, etc but no others.

davidc
Star Contributor
Star Contributor
Let me try here.

davidc
Star Contributor
Star Contributor
Document aspect properties can be get & set with Javascript - I've just tested - it's working fine.

Although the aspect may be applied, it's possible the property has not been set.  You can inspect any node in the repository using the node browser (http://localhost:8080/alfresco/faces/jsp/admin/store-browser.jsp) - take a look at your node in question.

In the content model you can assign a default value to any property - if you do, the property is set on the node at creation time.

slothrop
Champ in-the-making
Champ in-the-making
I don't know if the property (which property?) has been set.  What I know is that "Company Home > Pending Publication > Submissions" space has a content rule that adds the custom aspect "Reviewers" added to incoming documents.  I can open up the Details page on the documents and add values for the 3 properties of the Reviewers aspect, Reviewer1, Reviewer2 and Reviewer3.  This aspect and its property values persist when the document is copied or moved into other spaces.

I have defined a content rule for the "Company Home > Pending Publication > Submissions > In Progress > Under Review" space that is applied to incoming documents that have the Reviewers aspect.  This rule executes a JavaScript file.  However, when I copy a document which has the "Reviewers" aspect and has had values entered for the Reviewer1, Reviewer2 and Reviewer3 properties into the "Under Review" space, in this JavaScript code even though document.hasAspect("custom.Reviewers") returns true, document.properties["custom:Reviewers"] returns null and document.aspects["custom:Reviewers"] is undefined.

If I need to set the property, how do I do it?  The Node Browser has been of no help.  Its results are pretty much meaningless to me.

davidc
Star Contributor
Star Contributor
I don't know if the property (which property?) has been set.

The property you're trying to access!!!

What are the names of your properties?

custom:Reviewer1
custom:Reviewer2
custom:Reviewer3

So, try

document.properties["custom:Reviewer1"]

or

document.properties["custom:Reviewer2"]

or

document.properties["custom:Reviewer3"]