Share:|

Next up for listallthethings: Component Templates.  If I want to list all the Component Templates in a (static) Template group I can run:

blcli_execute TemplateGroup groupNameToId "/Workspace"

or for a Smart Template Group

blcli_execute SmartTemplateGroup groupNameToId "/Workspace/All Templates"

 

blcli_storelocal templateGroupId
blcli_execute Template listAllByGroup ${templateGroupId}

 

And that gives me the list of Templates in the group.  If you listed the contents of a static group and needed to get the DBKey of the template for some operation then you would just need to loop through the names, supply your group name and pass both to the Template.getDBKeyByGroupAndName command:

templateGroup="/Workspace"
blcli_execute TemplateGroup groupNameToId "${templateGroup}"
blcli_storelocal templateGroupId
blcli_execute Template listAllByGroup ${templateGroupId}
blcli_storelocal templateList
while read template
do
blcli_execute Template getDBKeyByGroupAndName "${templateGroup}" "${template}"
blcli_storelocal templateKey
echo "${templateGroup}/${template}:${templateKey}"
done <<< "$(awk 'NF' <<< "${templateList}")"

 

If we used the Smart Group then we can't use the Smart Group with the Template.getDBKeyByGroupAndName, so we need to take the approach of our other list exercises (List All The Depot Objects ,List All The Jobs , List All The Servers ) and look at the Template.listAllByGroup command and see if we can output the DBKey.  In the Unreleased blcli commands and documentation  we see this following command sequence for our command:

CommandInputReturn value stored name
Template.findAllByGroupId$groupid$-
Template.getNameno input-
Utility.setTargetObjectno input-
Utility.listPrintno input-

There doesn't seem to be a STemplateHeader namespace like we had for Jobs and DepotObjects.  So that's ok, but as we know from the other exercises, loading all the objects could result in some poor performance or memory issues with the blcli heap.  If the latter we can always add a blcli_setjvmoption -Xmx1024m or some other size prior to any other blcli command executions in the script to bump up the heap size (though this shouldn't be needed if running in a NSH Script Job and the Blcli Server is enabled).

 

There's a couple ways we could go w/ this; get the DBKey for all the templates and then iterate through that list to get the name and group or keep the list of templates in memory an iterate through that list.  This is similar to what we did in the previous posts.  If you just want the DBKey do the former.  I'll examples of each.

 

First the example where you get the DBKey for all the templates and loop through that list:

blcli_execute SmartTemplateGroup groupNameToId "${templateGroup}"
blcli_storelocal templateGroupId
blcli_execute Template findAllByGroupId ${templateGroupId}
blcli_execute Template getDBKey
blcli_execute Utility setTargetObject
blcli_execute Utility listPrint
blcli_storelocal templateKeys

while read templateKey
        do
        blcli_execute Template findByDBKey ${templateKey}
        blcli_execute Template getName
        blcli_storelocal templateName
        blcli_execute Template getGroupId
        blcli_storelocal templateGroupId
        blcli_execute Group getQualifiedGroupName 5008 ${templateGroupId}
        blcli_storeenv templateGroupPath
        echo "${templateGroupPath}/${templateName}"
done <<< "$(awk 'NF' <<< "${templateKeys}")"

 

Now the same thing, except loading each object and pulling out the same information:

blcli_execute SmartTemplateGroup groupNameToId "${templateGroup}"
blcli_storelocal templateGroupId
blcli_execute Template findAllByGroupId ${templateGroupId}
blcli_execute Utility storeTargetObject templates
blcli_execute Utility listLength
blcli_storelocal listLength
for i in {0..$((${listLength}-1))}
        do
        blcli_execute Utility setTargetObject templates
        blcli_execute Utility listItemSelect ${i}
        blcli_execute Utility setTargetObject
        blcli_execute Template getName
        blcli_storelocal templateName
        blcli_execute Template getGroupId
        blcli_storelocal templateGroupId
        blcli_execute Group getQualifiedGroupName 5008 ${templateGroupId}
        blcli_storelocal templateGroupPath
        echo "${templateGroupPath}/${templateName}"
done

 

These are similar patterns to what we do in the other listallthethings examples.  And just like the other examples I could be doing a set instead of a get with the DBKey or object in the loop.  If you'll noticed in the Group.getQualifiedGroupName I pass a number.  This is the object type id for a Static Template Group.  I pulled that number from my object type id reference.  I know the Template or Job or DepotObject can only ever exist in a static workspace group so I don't need to do any automagic derivation.  If you are scripting and trying to make a generic function you could do something like this instead:

blcli_execute Group findById ${groupId}
blcli_execute Group getType
blcli_storelocal groupTypeId
blcli_execute Group getQualifiedGroupName ${groupTypeId} ${groupId}

 

 

You can see some of the above logic at work in the Export Template Rules And Parts script where you can pass in a group name that contains some number of Component Templates and then the script will find all the templates in the group and dump out the desired information into each one.