Expand all | Collapse all

Velocity - loop through item types

  • 1.  Velocity - loop through item types

    Posted 05-03-2018 17:29
    ​I've been trying to figure out how to loop through item types of the current project.  My goal is to list the field names defined for each item type.
    But I can't even get to first base (declaring the class).  I've tried the following but all of these cause the script to fail.
      #set($projectTypes = $applicationContext.getBean("ProjectType"))
      #set($projectTypeFields = $applicationContext.getBean("ProjectTypeField"))
      #set($projectTypes = $applicationContext.getBean("ProjectTypeDao"))
      #set($projectTypeFields = $applicationContext.getBean("ProjectTypeFieldDao"))
      #set($documentTypes = $applicationContext.getBean("DocumentTypeDao"))
      #set($documentNodeManager = $applicationContext.getBean("documentNodeManager"))

    I have 3 questions:
    1. Which class can I use to loop through the item types?
    2. Are all 6 of these non-operational or am I doing something wrong?
    3. If all 6 of these are non-operational then is there a master list of classes that do work?


    Debra Shannon
    Sentek Global
    San Diego CA

  • 2.  RE: Velocity - loop through item types

    Posted 05-18-2018 05:45
    Edited by Anuj Sadani 05-20-2018 21:50
    Hi, I guess you can do it via REST API, by looping and recursion

    Try -

    which will have all the itemtypes, then call the abstractitems API for all the itemtypes for the project, if totalResults is 0 the itemtype is not present for the project, if >0 the its available.

    Anuj Sadani

  • 3.  RE: Velocity - loop through item types

    Posted 05-18-2018 09:42

    Anuj is correct. Our API uses pagination so either you need to implement a while loop or recursion in order to retrieve all the records for a particular endpoint. 

    The logic to accomplish this is as follows:

    1. initial GET request with the "startAt" parameter set to 0. you can also specify the "maxRecords" with a value of 1-50 (if not specified this will default to 20)

    2. this is where the looping/recursion takes place; look into the response of the previous GET request which will have a "meta.pageinfo" section. this meta information will contain more information on how many more pages you need to retrieve.

    3. Continue to make requests until you retrieve all the data, by procedurally incrementing your "startAt" parameter by your specified "maxRecords" amount. 

    4. Finally, look into the meta.pageInfo for knowing when you need to exit this loop/recursion. 

    Here is also some good coding examples to help illustrate how to accomplish this in various programming languages.  

    Also I find it very helpful to use our the swagger documentation when working with the API. This documentation gives you the ability to make actual live requests to your Jama instance, where you can see the actual responses from the API. You can reach this by appending "api-docs" to the URL of your jama instance. (ex.

    Chris Asakawa

  • 4.  RE: Velocity - loop through item types

    Posted 08-01-2018 14:25
    This the relevant snippet that I ended up with, thanks to help from Jama Software:
    #set($docDao = $applicationContext.getBean("documentDao"))
    #set($contourItemDao = $applicationContext.getBean("contourItemDao"))
    #set($documentTypeFieldDao = $applicationContext.getBean("documentTypeFieldDao"))
    #####Get the docs in the project #########
    #set($projectDocIds = $contourItemDao.getContourItemIdsForProject($mathTool.toInteger($
    #set($found = 'no')
    #set($docList = []) ##$docList will be an array of arrays
    ## cycle through all of the items in the project
    #foreach ($projectDocId in $projectDocIds)
      #set($baseDoc = $docDao.getDocument($mathTool.toInteger($projectDocId)))
      #foreach ($myDoc in $docList)
         ## Have the fields for this item type already been added to the $docList array?
            #if ($myDoc.get(0) == $
               ## This Item Type has already been seen so move onto the next item
               #set($found = 'yes')
       #end ##end of foreach
       #if($found == 'no')
          ## if the item type hasn't already been seen, add the field names to the $docFields array
          #set($docFields = [])
          #foreach ($d in $documentTypeFieldDao.getDocumentTypeFieldListByType($
             #if($docFields.add($ #end
          #if($docList.add($docFields)) #end  ##Add the $docFields array as an entry in the $docList array
       #set($found = 'no')
    ## For each item type included in $docList...
    #foreach($item in $docList)
       ## For each field defined for that item type....
       #foreach($fieldName in $item)
          <td>$fieldName</td>  ## print the name of that field

    Debra Shannon
    Sentek Global
    San Diego CA

  • 5.  RE: Velocity - loop through item types

    Posted 08-10-2018 17:29
    Thank you for sharing this with the community. You're amazing!

    Dana Medhaug
    Technical Support Engineer
    Jama Software