Tuesday, December 1, 2009

JavaScript API Examples in Alfresco



Script to get entire alfresco folder structure using recursion


-----------------------------------------------------------------------------------------------

// JavaScript Document

var log = "Script Excecuted at "   new Date()   "\n\n";
var node;

var logFile = companyhome.childByNamePath("log.txt");

if (logFile == null)
{
   logFile = userhome.createFile("log.txt");
}

for(i=0; i<companyhome.children.length; i  )
{
    var childName = companyhome.children[i];
  
    if(childName != null)
{
if(childName.isContainer)
{
   log  = childName.properties.name   "\t"    childName.displayPath   "\n" ;
          recursion(childName);  
       }
    
else
        log  = childName.properties.name   "\t"    childName.displayPath   "\n" ;
}
}

function recursion(node)
{
  if(node != null)
  {
    for(var j=0; j<node.children.length; j  )
   {
     if(node.children[j] != null)
     {
       if(node.children[j].isContainer)
       {
       log  = node.children[j].properties.name   "\t"    node.children[j].displayPath   "\n" ;
       recursion(node.children[j]);  
       }
       else
       {
            log  = node.children[j].properties.name   "\t"    node.children[j].displayPath   "\n" ;    
        }
       }
    }
  }
}

logFile.content  = log;

__________________________________________________________________


Script to send Email to Multiple users in Alfresco.
-----------------------------------------------------------------------------------------------
var mail = actions.create("mail");
var recep = new Array();
var i = 0;
recep[0] = "a@gmail.com";
recep[1] = "b@gmail.com";
//doc = document.properties.name;
for(i=0;i
{
 mail.parameters.to = recep[i];

mail.parameters.subject = "Hello from JavaScript";
mail.parameters.from = "sukumarpant@alfresco.com";
//mail.parameters.template = root.childByNamePath("Company Home/Data Dictionary/invite/invite-email.ftl");
mail.parameters.text = "some text, in case template is not found" ; 
mail.execute(document);

}
---------------------------------------------------------------------------


Create Backup of a Document
Creates a backup of a document as it is added to a space:

// find the backup folder - create if not already exists
var backupFolder = space.childByNamePath("Backup");
if (backupFolder == null && space.hasPermission("CreateChildren"))
{
   // create the folder for the first time
   backupFolder = space.createFolder("Backup");
}
if (backupFolder != null && backupFolder.hasPermission("CreateChildren"))
{
   // copy the doc into the backup folder
   var copy = document.copy(backupFolder);
   if (copy != null)
   {
      // change the name so we know it's a backup
      copy.name = "Backup of " + copy.name;
      copy.save();
   }
}


Create Backup of a Document And Log Doc Properties

Creates a backup of a document and logs the doc properties to a log text file:

// find the backup folder - create if not already exists
var backupFolder = space.childByNamePath("Backup");
if (backupFolder == null && space.hasPermission("CreateChildren"))
{
   // create the folder for the first time
   backupFolder = space.createFolder("Backup");
}
if (backupFolder != null && backupFolder.hasPermission("CreateChildren"))
{
   // copy the doc into the backup folder
   var copy = document.copy(backupFolder);
   if (copy != null)
   {
      // change the name so we know it's a backup
      var backupName = "Backup of " + copy.name;
      copy.name = backupName;
      copy.save();
   }
   
   // record the time of the backup to a log file
   var logFile = backupFolder.childByNamePath("backuplog.txt");
   if (logFile == null)
   {
      logFile = backupFolder.createFile("backuplog.txt");
   }
   if (logFile != null)
   {
      logFile.content += "File: " + backupName +
                         "\tDate: " + new Date().toGMTString() +
                         "\tSize: " + copy.size + "\r\n";
   }
}



Append Copyright Line To File

Appends a copyright line of content to plain text and HTML files:
if (document.hasPermission("Write"))
{
   if (document.mimetype == "text/plain")
   {
      document.content += "\r\n\r\nCopyright (C) 2006";
   }
   else if (document.mimetype == "text/html")
   {
      document.content += "

Copyright © 2006";
   }
}





Create Document, Make it Versionable, Modify It

Creates a document, makes it versionable, checks it out, modifies the content of the working copy, checks it in again and then repeats the process but checks in the document with a version history note and as a major version increment:
// create file, make it versionable
var doc = userhome.createFile("checkmeout.txt");
doc.addAspect("cm:versionable");
doc.content = "original text";

// check it out and update content on the working copy
var workingCopy = doc.checkout();
workingCopy.content = "updated text 1";

// check it in
doc = workingCopy.checkin();

// check it out again
workingCopy = doc.checkout();
workingCopy.content = "updated text 2";

// check it in again, but with a version history note and as major version increment
doc = workingCopy.checkin("a history note", true);





Add Aspects

Adding several aspects to a document:
var props = new Array(1);
props["cm:template"] = document.nodeRef;
document.addAspect("cm:templatable", props);

props = new Array(1);
props["cm:lockIsDeep"] = true;
document.addAspect("cm:lockable", props);

props = new Array(1);
props["cm:hits"] = 1;
document.addAspect("cm:countable", props);


Return Result Value

Returning a result value. This is useful for scripts that are processed using URLs via the Script Command Servlet, as the results are returned as the HTML response from the servlet:
function result()
{
   return "The name of my home space is: " + userhome.name;
}
result();


Change Mime Type of a Document

Changes the mimetype of a document after setting the content:
var file = userhome.createFile("testfile.html");
file.content = "some HTML here";
file.mimetype = "text/html";



Create Document and Transform it

Creates document content and converts it to new formats using the transformation API:
// create a plain text doc and convert to PDF, generated file will be placed in same space as original
var doc1 = userhome.createFile("transform_me1.txt");
doc1.mimetype = "text/plain";
doc1.content = "This is plain text";
var trans1 = doc1.transformDocument("application/pdf");

// create an HTML doc and convert to plain text, generated file will be created under the companyhome space
var doc2 = userhome.createFile("transform_me2.html");
doc2.mimetype = "text/html";
doc2.content = "This is an HTML document!";
var trans2 = doc2.transformDocument("text/plain", companyhome);

// create an HTML doc and convert to flash swf file, generated file will be created under the companyhome space
var doc3 = userhome.createFile("transform_me3.html");
doc3.mimetype = "text/html";
doc3.content = "This is an HTML document!";
var trans3 = doc3.transformDocument("application/x-shockwave-flash", companyhome);



Creating different child node types, including via a specific named child association and with default properties

var node1 = userhome.createNode("create test1.txt", "cm:content");
node1.content = "node1 content";

var node2 = userhome.createNode(null, "sys:base");

var props = new Array();
props["cm:name"] = "create test3.txt";
var node3 = userhome.createNode(null, "cm:content", props);

props["cm:name"] = "node name4.txt";
props["cm:title"] = "node title4";
var node4 = userhome.createNode(null, "cm:content", props, "cm:contains");

var result = "nodes created ok";
result;






Deleting Special Charterers and white Spaces from a String Using JavaScript

--------------------------------------------------------------------------

<html>
<head>

<script language="JavaScript">

<!--
var temp = new String('Certificate of Origin (issued by Beneficiary)');
document.write(temp '<br>');
temp = temp.replace(/[^a-zA-Z 0-9] /g,'').toUpperCase();
var temp2 = temp.replace(/\s /g,'');
document.write(temp2 '<br>');
//-->

</script>

</head>

<body>

</body>

</html>
------------------------------------------------------------------

2 comments:

  1. Hi sir . I saw your post about sending an email with multiple users .. Is that possible in sending an email with multiple attachment ?. Thanks

    ReplyDelete
  2. hi sir,
    I saw your post but the transform is not working.
    plz help me to solve that.
    Thanks in advance.

    ReplyDelete