Hi,
Try below code to create WTDocument using template.
- 1. First we have to write this class TemplateHelper.java
package ext.ptc;
import java.io.InputStream;
import com.ptc.windchill.enterprise.doc.server.DocumentUtility;
import com.ptc.windchill.enterprise.templates.TemplatesFactory;
import wt.admin.AdminDomainRef;
import wt.content.ApplicationData;
import wt.content.ContentServerHelper;
import wt.doc.DepartmentList;
import wt.doc.DocumentType;
import wt.doc.WTDocument;
import wt.enterprise.EnterpriseHelper;
import wt.enterprise.SequenceGenerator;
import wt.enterprise.TemplatesUtility;
import wt.inf.container.WTContainer;
import wt.inf.container.WTContainerHelper;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.util.WTProperties;
public class TemplateHelper {
/**
* return the WTDocument object basis the template name
* @param templateName
* @param orgName
* @throws Exception
*/
public static WTDocument getDocumentTemplate(String templateName, String orgName) throws Exception
{
QuerySpec qs = new QuerySpec(WTDocument.class);
SearchCondition templateNameSC = new SearchCondition(WTDocument.class,WTDocument.NAME,
SearchCondition.EQUAL,templateName);
qs.appendWhere(templateNameSC, new int[]{0,1});
WTContainer container = WTContainerHelper.service.getByPath("/wt.inf.container.OrgContainer="+orgName).getReferencedContainer();
System.out.println("Cont Name in getDocTemp ==>"+container.getName());
wt.fc.QueryResult qrTemplates = EnterpriseHelper.service.getTemplates(container,
qs, null, true, true, true);
if(qrTemplates.hasMoreElements())
return ((WTDocument)qrTemplates.nextElement());
else
return null;
}
}
2. Later we have to give these inputs pertaining to specific document subtype wherever it is getting created:
In my scenario, I was adhering document template of subtype ‘Commercial proposal document’ for organization ‘PTC’ where ‘Commercial Proposal Document’ Is the name of the Template.
- cmpDOC.setDomainRef(doc.getDomainRef());
WTDocument docTemplate = TemplateHelper.getDocumentTemplate("Commercial Proposal Template", "PTC");
System.out.println("Document Template Name : - "+docTemplate.getName());
doc=(WTDocument) VersionControlHelper.service.getLatestIteration(doc, true);
System.out.println("childDoc Name : - "+doc.getName());
PersistenceHelper.manager.save(cmpDOC);
wt.content.ContentHelper.service.copyContent((wt.content.ContentHolder)docTemplate, (wt.content.ContentHolder)cmpDOC);
Thanks,
Kaushik