1. ptc\Windchill_12.0\Windchill\codebase\rest\custom\domain\DocMgmt\v5\import.js function action_updatePrimaryConentOfDocument(data, params) { var doc = Java.type('ext.sconce.solution.ODataHelper'); return doc.updatePrimaryConentOfDocument(data, params); } 2. ptc\Windchill_12.0\Windchill\codebase\rest\custom\domain\DocMgmt\v5\import.json "actions" : [ { "name": "updatePrimaryConentOfDocument", "importName": "updatePrimaryConentOfDocument", "description": "API to update update Primary Content Of WTDocument & remove existing content", "includeInServiceDocument": true, "parameters": [ { "name": "Number", "type": "String", "isNullable": false, "isCollection": false }, { "name": "fileLocation", "type": "String", "isNullable": false, "isCollection": false }, { "name": "fileName", "type": "String", "isNullable": false, "isCollection": false } ], "returnType": { "type": "Edm.String", "isCollection":false, "isNullable": true } } ] Input: fileLocation: D:\TestUploadFolder Number : 0000007608 fileName : Sample.pdf 3. Java file: package ext.sconce.solution; import java.beans.PropertyVetoException; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.logging.log4j.Logger; import org.apache.olingo.commons.api.data.Parameter; import org.apache.olingo.commons.api.http.HttpStatusCode; import org.apache.olingo.server.api.ODataApplicationException; import org.json.JSONObject; import com.flexnet.lm.binary.StringUtil; import com.ptc.odata.core.entity.action.ActionProcessorData; import com.ptc.odata.core.entity.property.EntityAttribute; import com.ptc.odata.core.entity.property.PropertyValueType; import wt.content.ApplicationData; import wt.content.ContentHelper; import wt.content.ContentHolder; import wt.content.ContentItem; import wt.content.ContentRoleType; import wt.content.ContentServerHelper; import wt.doc.WTDocument; import wt.fc.QueryResult; import wt.log4j.LogR; import wt.pds.StatementSpec; import wt.pom.Transaction; import wt.util.WTException; import wt.util.WTProperties; import wt.util.WTPropertyVetoException; public class ODataHelper { private static String CLASSNAME = ODataHelper.class.getName(); private static Logger logger = LogR.getLoggerInternal(CLASSNAME); public static EntityAttribute updatePrimaryConentOfDocument(ActionProcessorData data, HashMap params) { if (logger.isDebugEnabled()) logger.debug(CLASSNAME + " - updatePrimaryConentOfDocument Start"); Map responseMap = new HashMap(); String fileName = ""; String Number = ""; String fileLocation = ""; try { Number = getStringParameter(data, params, "Number"); fileLocation = getStringParameter(data, params, "fileLocation"); fileName = getStringParameter(data, params, "fileName"); logger.debug("Number:" + Number + "fileLocation" + fileLocation + "fileName " + fileName); updatePrimaryConentOfDocument(Number, fileLocation, fileName); responseMap.put("Message", "update Primary Conent Of Document successfully."); } catch (ODataApplicationException e) { e.printStackTrace(); responseMap.put("Error", e.getMessage()); } EntityAttribute result = new EntityAttribute(null, "values", PropertyValueType.PRIMITIVE, new JSONObject(responseMap)); return result; } public static String getStringParameter(ActionProcessorData data, Map params, String stringName) throws ODataApplicationException { StringUtil.isEmpty(""); Parameter param = (Parameter) params.get(stringName); if (org.springframework.util.StringUtils.isEmpty(param.getValue())) { String errorMsg = "Invalid Parameter - " + stringName + "not found in the request"; throw new ODataApplicationException(errorMsg, HttpStatusCode.BAD_REQUEST.getStatusCode(), data.getLocale()); } else { return (String) param.getValue(); } } public static void updatePrimaryConentOfDocument(String Number, String fileLocation, String fileName) { logger.debug("updatePrimaryConentOfDocument - replaceContent - BEGIN"); WTDocument wtdoc = getDocumentByNumber(Number); if (wtdoc != null) { Transaction trx = new Transaction(); try { trx.start(); ApplicationData wtdContent = ApplicationData.newApplicationData((ContentHolder) wtdoc); wtdContent.setFileName(fileName); wtdContent.setRole(ContentRoleType.PRIMARY); WTProperties wtproperties = wt.util.WTProperties.getLocalProperties(); String dirSeparator = wtproperties.getProperty("dir.sep"); logger.debug("Path : " + fileLocation + dirSeparator + fileName); File file = new File(fileLocation + dirSeparator + fileName); wtdoc = (WTDocument) wt.vc.VersionControlHelper.service.getLatestIteration(wtdoc, true); deletePrimaryContent(wtdoc); try (FileInputStream istream = new FileInputStream(file)) { ContentServerHelper.service.updatePrimary(wtdoc, wtdContent, istream); istream.close(); } ContentServerHelper.service.updateHolderFormat(wtdoc); logger.debug("Replaced content - wtdoc: " + wtdoc.getIdentity()); trx.commit(); trx = null; logger.debug("Completed updatePrimaryConentOfDocument END"); } catch (WTException | PropertyVetoException | IOException ex) { System.out.println( " Error while updating primary content = " + ex + "for WTDocument " + wtdoc.getIdentity()); ex.printStackTrace(); } finally { if (trx != null) { trx.rollback(); } } } } public static WTDocument deletePrimaryContent(WTDocument wtdoc) { try { QueryResult qrCI = ContentHelper.service.getContentsByRole((ContentHolder) wtdoc, ContentRoleType.PRIMARY); if (qrCI.size() > 0) { ContentItem ci = (ContentItem) qrCI.nextElement(); ContentServerHelper.service.deleteContent(wtdoc, ci); } } catch (WTException | WTPropertyVetoException ex) { ex.printStackTrace(); } return wtdoc; } public static WTDocument getDocumentByNumber(String docNumber) { WTDocument doc = null; try { wt.fc.QueryResult result = null; wt.query.QuerySpec query = new wt.query.QuerySpec(WTDocument.class); wt.query.WhereExpression condition1 = new wt.query.SearchCondition(WTDocument.class, WTDocument.NUMBER, wt.query.SearchCondition.EQUAL, docNumber); int fromIndices[] = { 0, 1 }; query.appendWhere(condition1, fromIndices); result = wt.fc.PersistenceHelper.manager.find((StatementSpec) query); while (result.hasMoreElements()) { doc = (WTDocument) result.nextElement(); } } catch (Exception e) { e.printStackTrace(); } return doc; } }