OpenKM Scripting – Creating Folders

We recently had the need to create hundreds of folders but didn’t want to do so manually. Unfortunately, this wasn’t as easy as simply going into the database and adding them. Fortunately, it was as easy as using the following script:

import com.openkm.api.OKMFolder;
import com.openkm.core.JcrSessionManager;
import com.openkm.bean.Folder;

String token = JcrSessionManager.getInstance().getSystemToken();

String[] arrDepts = {"A","B","C","D1","D2","E"};
String path;
Folder folder;

for(String dept : arrDepts) {
  try {
    (folder = new Folder()).setPath(path = "/okm:root/Department " + dept);
    OKMFolder okmFolder = OKMFolder.getInstance();
    okmFolder.create(
      JcrSessionManager.getInstance().getSystemToken(),
      folder
    );
    print("\"" + path + "\" created
");
  }
  catch(Exception e) {
    print("\"" + path + "\" failed
");
  }
}

That script creates the following folders with the same access that the /okm:root folder has:

  • /okm:root/A
  • /okm:root/B
  • /okm:root/C
  • /okm:root/D1
  • /okm:root/D2
  • /okm:root/E

The output that will be shown will indicate which folders were successfully created.  As usual, play around with the script and make it your own!  If you think you have something really cool to add, let me know because I am always willing to do follow-up posts, of course giving FULL credit to contributors.

2 Replies to “OpenKM Scripting – Creating Folders”

Leave a Reply

Your email address will not be published. Required fields are marked *