cancel
Showing results for 
Search instead for 
Did you mean: 

How to run JavaScript file from Alfresco share?

SashaB
Champ in-the-making
Champ in-the-making

I try running  script file in directory /Company Home/Data Dictionary/Scripts/xxxx.js, but do not it. How I can to run a script file, for example, on create user with this code:

function main()
{
if (logger.isLoggingEnabled()){
logger.log("Start create User");
}
 
var testUser = people.createPerson("Sasha2", "Sasha23", "Sasha24", "sdfsdf@alfresco.com", "123", true, false);
if (testUser !=null){
// user account created
}
 
 
 
if (logger.isLoggingEnabled()){
logger.log("End create user");
}
}

main();
1 REPLY 1

abhinavmishra14
World-Class Innovator
World-Class Innovator

If i understand correctly, you want to implement and execute repo tier webscript.

I would suggest you go through these docs and understand webscript concepts:

https://docs.alfresco.com/5.2/concepts/ws-types.html

https://docs.alfresco.com/5.2/concepts/ws-reference.html

https://docs.alfresco.com/5.2/tasks/ws-hello-world-create.html

You can also refer this tutorial:

http://ecmarchitect.com/alfresco-developer-series-tutorials/webscripts/tutorial/tutorial.html

Now, coming to your script, you should be copying your webscript under "/Company Home/Data Dictionary/Web Scripts" or " Web Scripts Extensions" directory and register the webscript as documented here

You would need a "web script description document, name ends with '.desc.xml'" as well apart from the javascript controller. If you follow the documents shared above, you will get clear idea of webscripts. 

Sample code for your reference, upload these files under "/Company Home/Data Dictionary/Web Scripts Extensions" and register:

create-user.get.js

function main() {
	if (logger.isLoggingEnabled()) {
		logger.log("Start create User");
	}

    try {
        var testUser = people.createPerson("Sasha2", "Sasha23", "Sasha24", "sdfsdf@alfresco.com", "123", true, false);
		if (testUser != null) {
			// user account created
			model.message = "User account created";
		}
	} catch(ex) {
		logger.log("Exception occurred: "+ex.message);
		model.message = "User account creation failed due to: "+ex.message;
	}

	if (logger.isLoggingEnabled()) {
		logger.log("End create user");
	}
}

main();

create-user.get.desc.xml

<webscript>
	<shortname>Create User</shortname>
	<description>
	  Script to create user
	</description>
	<url>/create-user</url>
  	<authentication>user</authentication>
</webscript>

create-user.get.html.ftl

<html>
<head>
<title>Create User</title>
</head>
<body>
     <h3>Status: ${message}</h3>
</body>
</html>
~Abhinav
(ACSCE, AWS SAA, Azure Admin)