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();
    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.

OpenKM Scripting – Adding UserRole

A while back, when I first starting working with OpenKM, I accidentally removed the UserRole role from the root directory. For the longest time, I had know clue how to add the UserRole role back to that folder, or any other folder for that matter. Today, after playing around with the example scripts, I developed the following script that can be run from the Administrators tab:

import com.openkm.api.OKMAuth;
import com.openkm.core.JcrSessionManager;
import com.openkm.bean.Permission;
String token = JcrSessionManager.getInstance().getSystemToken();
OKMAuth.getInstance().grantRole(token, "/okm:root", "UserRole", Permission.READ, false);