Home

Unable to get custom property value through Javascript

You are here

15 posts / 0 new
Last post
Unable to get custom property value through Javascript

Hi

I am having a javascript to extract the custom properties of a document when it is being added to Alfresco. I have a requirement to auto folder this document in a folder that is dynamically created based on the custom property of this document. So, I defined a rule and am having a javascript executed wheneever a document belonging to this type is being added to Alfresco. I have the following code snippet in my javascript that is returning null due to which the folder creation process is failing.

var empId = document.properties["dc:eId"];

Whereas, when I try to get the name of the document, I am able to get it using the following code.

var docName = document.properties["name"];

What I am failing to understand is that I am able to set the custom property from "Add Content" wizard. I can see the value of the same after the document is added. Also, I am able to search the document based on the custom property. The only thing that is failing is the retrieval of this property value by the javascript.

Any clues?

-Thanks
Srikant
Principal Consultant
IntrospeQt Software India Pvt Ltd
http://www.introspeQt.com

Re: Unable to get custom property value through Javascript

Hi,
Actions (i.e. your javascript) are triggered in the same way that any other behaviour is. It is likely that the metadata extraction is occuring after your custom action. When you apply the rule for your custom action, make it run asynchronously. At that point, the metadata extraction will be done.
Regards

Derek Hulley
Senior Software Engineer, Alfresco
http://docs.alfresco.com/ - Alfresco Docs
http://issues.alfresco.com/ - JIRA bug reporting site

Re: Unable to get custom property value through Javascript

Further to my previous post: I presume that you have set up a custom metadata extractor or configured one of the existing extractors to populate your custom property. Correct?

Derek Hulley
Senior Software Engineer, Alfresco
http://docs.alfresco.com/ - Alfresco Docs
http://issues.alfresco.com/ - JIRA bug reporting site

Re: Unable to get custom property value through Javascript

Derek
Thanks for responding.

I am not sure if I understood your response. As I mentioned in my post, I have created a custom document type called dc:doc. This has 2 properties called dc:eId and dc:eName.

My requirement is as follows. When a document of type dc:doc is added with the 2 properties (eId and eName), the javascript has to check if a folder with the name eId already exists in the alfresco repository. If it doesn't, I need to create a folder with name as eId and move the document under that folder.

For this to happen, when a document of this type is added, I need to upfront get the properties on this document. For this I am having the following javascript code.

......
var empId = document.properties["{http://www.myintrospeqt.com/model/content/1.0}eId"];
 
var empName = document.properties["{http://www.myintrospeqt.com/model/content/1.0}eName"];
........

What I observe is that both empId and empName are null when I print them. Because of this, the rest of the code is not getting executed.

I have also set up the rule to run in the background. I presume that this will ensure that the rule is run in an asynchronous manner.

Appreciate if you can respond to this.

-Thanks
Srikant
Principal Consultant
IntrospeQt Software India Pvt Ltd
http://www.introspeqt.com

Re: Unable to get custom property value through Javascript

There's no obvious reason why that shouldn't work - but it's difficult to tell exactly without seeing more of your script. Have you tried calling:

document.reset();

before retrieving the properties? As if you have just extracted meta-data via an action it is possible that the properties values are already cached in the script node from a previous operation.

Kevim

UI Team Manager and Technical Lead
http://twitter.com/kevinroast - Find me on Twitter!
http://wiki.alfresco.com - Alfresco Wiki docs and Community downloads
http://www.kevs3d.co.uk/dev - My personal HTML5 projects

Re: Unable to get custom property value through Javascript

Here is my js file.

var backupFolder = space.childByNamePath("Backup");
if (backupFolder == null && space.hasPermission("CreateChildren"))
{
   // create the folder for the first time
   backupFolder = space.createFolder("Backup");
}
 
   var logFile = backupFolder.childByNamePath("backuplog.txt");
   if (logFile == null)
   {
      logFile = backupFolder.createFile("backuplog.txt");
   }
   if (logFile != null)
   {
      logFile.content += "starting logging " ;
   }
document.reset();
var empId = document.properties["{http://www.Democo.com/model/content/1.0}eId"];
 
var empName = document.properties["{http://www.Democo.com/model/content/1.0}eName"];
 
logFile.content += "empId: " + empId + " empName: " + empName;

In the log file, I can see the values of empId and empName as null. Just to give you more information, I am also attaching the screenshot of properties page of the document I added. You can see the values of emp id and emp name on it. But these values are not visible in the js file.
http://i44.tinypic.com/9rsthi.jpg

As you mentioned, I included document.reset() call before getting the properties but I still am not getting the values.

-Thanks
Srikant
Principal Consultant
IntrospeQt Software India Pvt Ltd
http://www.introspeqt.com

Re: Unable to get custom property value through Javascript

Hi,

The first thing to do is to see what the state of the actual document is when your script is called. Put a break point in the script and then use the Node Browser to dig down to the document and examine its properties. If the properties show up in the Node Browser while the script is returning null, let us know. If the properties are null in the Node Browser, then the Javascript is giving the correct result and the properties really have not been set.

Get back to us on this; I have a suspicion on what might be causing it.

Derek Hulley
Senior Software Engineer, Alfresco
http://docs.alfresco.com/ - Alfresco Docs
http://issues.alfresco.com/ - JIRA bug reporting site

Re: Unable to get custom property value through Javascript

Derek

Can you let me know if there is any link / posting that will let me enable debugging the js? I am a newbie to this.

-Thanks
Srikant
Principal Consultant
IntrospeQt Software India Pvt Ltd
http://www.introspeQt.com

Re: Unable to get custom property value through Javascript

OK. I figured out how to enable the debugger. In the log4j properties I set the following values to debug.

log4j.logger.org.alfresco.repo.web.scripts.AlfrescoRhinoScriptDebugger=debug
log4j.logger.org.alfresco.repo.jscript=debug

After setting this and restarting tomcat, I get a swing window called as Alfresco Repository Javascript Debugger.

I have set a break point in the javascript and watched the "document". Below is the result of the same.

document        Node Type: {http://www.myintrospeqt.com/model/content/1.0}invoice, Node Aspects: [{http://www.alfresco.org/model/content/1.0}auditable, {http://www.alfresco.org/model/system/1.0}referenceable, {http://www.alfresco.org/model/content/1.0}titled, {http://www.alfresco.org/model/content/1.0}author, {http://www.alfresco.org/model/content/1.0}generalclassifiable]

One thing I observed is that javascript is able to resolve the type of the document correctly. But the custom properties are showing a value of null. However, in the node browser, I can see the custom property values for this document. Here is the URL for the node browser snapshot.
http://i39.tinypic.com/iqvrr7.jpg

I am sorry, the type and properties I mentioned here are slightly different from the one I mentioned earlier. However, I am doing the same thing in this as I was doing earlier.

-Thanks
Srikant
Principal Consultant
IntrospeQt Software India Pvt Ltd
http://www.introspeQt.com

Re: Unable to get custom property value through Javascript

Another way to launch the debugger that does not require a config change, go to the following:
http://:/alfresco/service/index
and select the Alfresco JavaScript Debugger link and Enable the debugger. You may need to "Refresh Webscripts" before to remove any compiled javascript from the cache.

Thanks,

Kev

UI Team Manager and Technical Lead
http://twitter.com/kevinroast - Find me on Twitter!
http://wiki.alfresco.com - Alfresco Wiki docs and Community downloads
http://www.kevs3d.co.uk/dev - My personal HTML5 projects

Re: Unable to get custom property value through Javascript

If you expand the node object in the debugger and look at the "properties" collection - does it show the values in that?

UI Team Manager and Technical Lead
http://twitter.com/kevinroast - Find me on Twitter!
http://wiki.alfresco.com - Alfresco Wiki docs and Community downloads
http://www.kevs3d.co.uk/dev - My personal HTML5 projects

Re: Unable to get custom property value through Javascript

Kevin

I can see the custom property names but the values for them is null in the debugger. However I can see values for certain properties like name and title.

-Thanks
Srikant
Principal Consultant
IntrospeQt Software India Pvt Ltd
http://www.introspeQt.com

Re: Unable to get custom property value through Javascript

Hi,

The code that handles the upload is AddContentDialog. This goes through two phases (see BaseDialogBean): finishImpl; doPostCommitProcessing. The post commit processing is actually where the user is given the opportunity to enter further metadata, but the document has already been uploaded into Alfresco i.e. the document upload transaction was committed. Your initial action is probably firing after the first commit and hence not seeing the custom metadata, even though it might appear, to the user, that the document upload is not complete.

Your script action will have to handle both initial upload (with or without full metadata) and updates of the metadata. To do this, you will have to trigger your actions off document updates as well.

Regards

Derek Hulley
Senior Software Engineer, Alfresco
http://docs.alfresco.com/ - Alfresco Docs
http://issues.alfresco.com/ - JIRA bug reporting site

Re: Unable to get custom property value through Javascript

Can you detail me or point to a location on how to do this?

-Thanks
Srikant
Principal Consultant
IntrospeQt Software India Pvt Ltd
http://www.introspeQt.com

Re: Unable to get custom property value through Javascript

Hi stallapragada,

I wrote following JavaScript for similar requirement (Loan related document upload)

var prop = document.properties["custom:applicationNo"];
var existsSpace = space.childByNamePath(prop);
var dest;
 
if(existsSpace == null)
{
dest = space.createFolder(prop);
document.move(dest);
}
else
{
document.move(existsSpace);
}

Created rule on update event instead of Inbound event.

Thanks,
Nikesh

forums index