Getting Alfresco Content Types via JavaScript

I was asked if it is possible to access Alfresco’s DictionaryService via JavaScript…Well, not directly using an offical Alfresco JS Root-Object, but it isn’t a big task if you use some functions of the embedded Rhine engine (& of course Florian’s famous Alfresco JavaScript Console http://alfresco.fme.de/JavaScript-Console.923.0.html):

10 thoughts on “Getting Alfresco Content Types via JavaScript

  1. Hi Jan,

    in fact, there is something general here with far broader scope than just getting content types … at least for some people. 😉

    cheers
    Andreas

  2. well, DictionaryService defines a few methods regarding constraints, e.g. Collection getConstraints(QName model);


    var ctx = Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
    var model = Packages.org.alfresco.model.ContentModel;
    var qname = Packages.org.alfresco.service.namespace.QName;
    var ns = Packages.org.alfresco.service.namespace.NamespaceService;

    var dictionaryService = ctx.getBean("DictionaryService");
    constraints = dictionaryService.getConstraints(qname.createQName(ns.CONTENT_MODEL_1_0_URI, "contentmodel"), true).toArray();
    for each(var constraint in constraints){
    print(constraint.name + " - " + constraint.constraint.shortName);
    }

    If you’d like to get all constraints of all models just call getAllModels before & iterate.

  3. If you find yourself using the Foundation Services APIs regularly, you may find it easier to use the Groovy Console instead (see http://blogs.alfresco.com/wp/pmonks/2010/08/19/alfresco-and-groovy-baby/). Because it uses Groovy as the “interpreted” language, there’s less impedance mismatch between the scripted code and the native Java APIs, plus most (all?) of the Alfresco Java samples should just work (though in some cases they may need slight tweaks before being accepted by the Groovy compiler).

  4. Pingback: Why Groovy Scripting rocks : Use Case Alfresco | CONTENTREICH

  5. I tried a similar approach for retrieving the Authority Service, but it didn’t work for me:

    var authorityService = ctx.getBean(“AuthorityService”);
    var authorities = authorityService.getAllRootAuthorities();

    I get an error:
    Can’t find method $Proxy56.getAllAuthorities().

    Although in the Rhino debugger it appears that the Authority Service object is there.

  6. George:

    The getAllRootAuthorities() function requires an authorityType as parameter.

    Try this…

    var authorityService = ctx.getBean(“AuthorityService”);
    var authorityType = Packages.org.alfresco.service.cmr.security.AuthorityType;
    var authorities = authorityService.getAllRootAuthorities(authorityType.GROUP);
    print(authorities);

    Cheers.

  7. I’ve used your JavaScript fragment successfully up to Alfresco version 4.2.f, but on 5.0.x the line
    var dictionaryService = ctx.getBean(“DictionaryService”);
    fails with the following exception:
    Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Can only specify arguments for the getBean method when referring to a prototype bean definition

    Have you encountered this problem before?

  8. when i am accessing ur code it’s giving below error:

    500 Internal Error
    An error inside the HTTP server which prevented it from fulfilling the request.
    11090110 Wrapped Exception (with status template): 11090384 Failed to execute script ‘Javascript Console Script’: Can only specify arguments for the getBean method when referring to a prototype bean definition

Leave a Reply

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