cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

We are happy to announce the new Windchill Customization board! Learn more.

How do I upload a file to WTDocument via code?

VladiSlav
17-Peridot

How do I upload a file to WTDocument via code?

Hello everyone, please tell me how to upload a file to WTDocument using Info*Engine?
Or how to use java to create a document and upload a file to it?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Cassie
14-Alexandrite
(To:VladiSlav)

Hello, 

 

Here is a existing article https://www.ptc.com/en/support/article/CS18364.

Please check if it is works for you.

 

Thanks

Cassie

View solution in original post

10 REPLIES 10
Cassie
14-Alexandrite
(To:VladiSlav)

Hello, 

 

Here is a existing article https://www.ptc.com/en/support/article/CS18364.

Please check if it is works for you.

 

Thanks

Cassie

VladiSlav
17-Peridot
(To:Cassie)

cyang-5, thank you very much for your response, this is what you need.

Please tell me, do You have an example of how to get files from WTDocument, which is located in Windchill?

hlafkir
13-Aquamarine
(To:VladiSlav)

HI Vadsilav,

 

Please take a look in this article :

https://www.ptc.com/en/support/article?n=CS18364

Where you will be to create a document a upload a primary content.

Note that you may have to upload first content into server. 

Note also that it is a server side API. it is only working by invoking methodServer like described in this article :

 

https://www.ptc.com/en/support/article/CS18364

 

Hope this will help

 

Hicham

 

 

 

Using SOAP you 

VladiSlav
17-Peridot
(To:hlafkir)

hlafkir,thank you very much for your help, I had a problem, the document is being created, but the file is not attached, an error is issued:

(wt.pom.pomResource/0) wt.pom.POMInitException: A persistence error occurred. System message follows:
Nested exception is: wt.method.MethodServerException: No active method context
at wt.method.MethodContext.getContext(MethodContext.java:1835)
at wt.pom.PersistentObjectManager.getPom(PersistentObjectManager.java:268)
at wt.pom.Transaction._start(Transaction.java:690)
at wt.pom.Transaction.start(Transaction.java:654)
at com.acme.test.excel.TxtToExcel.createAndUpload(TxtToExcel.java:65)
at com.acme.test.excel.TxtToExcel.main(TxtToExcel.java:43)

Process finished with exit code 0

hlafkir
13-Aquamarine
(To:VladiSlav)

Hi Vadislav,

 

Did you implement the rmi invocation ? How did you do that ? and where is located your primary content ?

 

something like this : 

 

public class CreateDoc implements RemoteAccess {

public static void main(String[] args)
{
// Retrieve a MethodServer remote reference
// from the ServerManager
RemoteMethodServer rms = RemoteMethodServer.getDefault();

// Authenticate using the default
// HTTPLogin Authenticator
rms.setUserName("wcadmin");
rms.setPassword("ts");

try
{
rms.invoke("CreateDocument",
"ext.CreateDoc", null, null, null);
System.out.println("Should have worked, check the MS log");
}
catch (Exception e)
{
e.printStackTrace();
}
}


public static void CreateDocument() throws WTException, SQLException, PropertyVetoException, IOException {

....

....

 

Hicham

VladiSlav
17-Peridot
(To:hlafkir)

 

hlafkir, here is the code I use: 
package com.acme.test.excel;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import wt.content.ApplicationData;
import wt.content.ContentRoleType;
import wt.content.ContentServerHelper;
import wt.doc.DocumentType;
import wt.doc.WTDocument;
import wt.fc.Persistable;
import wt.fc.PersistenceHelper;
import wt.folder.Folder;
import wt.folder.FolderEntry;
import wt.folder.FolderHelper;
import wt.inf.container.WTContainer;
import wt.inf.container.WTContainerHelper;
import wt.inf.container.WTContainerRef;
import wt.method.RemoteAccess;
import wt.pom.Transaction;
import wt.util.WTException;
import wt.util.WTInvalidParameterException;

import java.beans.PropertyVetoException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class TxtToExcel implements RemoteAccess {
    public static void main(String[] args) throws IOException, WTException, PropertyVetoException {
        Workbook wb = new HSSFWorkbook();
        Sheet sheet0 = wb.createSheet("Tester");
        Sheet sheet1 = wb.createSheet("911");
        Sheet sheet2 = wb.createSheet("Tester911");

        FileOutputStream fos = new FileOutputStream("Excel/123.xls");
        wb.write(fos);
        fos.close();

        String container_path = "/wt.inf.container.OrgContainer=TESTER911/wt.pdmlink.PDMLinkProduct=Тестовое изделие";
        WTContainerRef containerRef = WTContainerHelper.service.getByPath(container_path);
        Folder folder = FolderHelper.service.getFolder("/Default/"+"Excel", containerRef);
        createAndUpload("excelmvm", "excelmvm", containerRef, folder);
    }

    static void createAndUpload(String name, String number, WTContainerRef product, Folder folder)
            throws WTInvalidParameterException, WTException, PropertyVetoException, IOException {
        Transaction tx = new Transaction();
        try{
            WTDocument doc = WTDocument.newWTDocument(name, number, DocumentType.getDocumentTypeDefault());
            doc.setContainerReference(product);
            doc.setDomainRef(((WTContainer) product.getObject()).getDefaultDomainReference());
            FolderHelper.assignLocation((FolderEntry)doc, (Folder)folder);
            // WTDoc needs to be stored before content may be added
            doc = (WTDocument)PersistenceHelper.manager.store(doc);

            ApplicationData theContent = ApplicationData.newApplicationData(doc);
            String filename = "test.txt";
            File theFile = new File("D:\\ptc\\Windchill_11.1\\eclipse\\Excel\\123.txt");
            theContent.setFileName(filename);
            theContent.setRole(ContentRoleType.toContentRoleType("PRIMARY"));
            theContent.setFileSize(theFile.length());
            FileInputStream fis = new FileInputStream(theFile);

            tx.start();
            theContent = ContentServerHelper.service.updateContent(doc, theContent, fis);
            ContentServerHelper.service.updateHolderFormat(doc);
            tx.commit();

            doc = (WTDocument) PersistenceHelper.manager.refresh((Persistable) doc, true, true);
            fis.close();
        }catch(Exception e) {
            e.printStackTrace();
            tx.rollback();
        }
    }
}
​
hlafkir
13-Aquamarine
(To:VladiSlav)

HI Vadislav, 

 

In my first post, I provided two same article. Sorry for that.

 

The RMI implemntation is here : 

https://www.ptc.com/en/support/article/CS24105

 

without that, you will always get the error you're facing..

 

As said earlier, pleas eimplement your code like this : 

 

public class CreateDoc implements RemoteAccess {

public static void main(String[] args)
{
// Retrieve a MethodServer remote reference
// from the ServerManager
RemoteMethodServer rms = RemoteMethodServer.getDefault();

// Authenticate using the default
// HTTPLogin Authenticator
rms.setUserName("wcadmin");
rms.setPassword("ts");

try
{
rms.invoke("CreateDocument",
"ext.CreateDoc", null, null, null);
System.out.println("Should have worked, check the MS log");
}
catch (Exception e)
{
e.printStackTrace();
}
}


public static void CreateDocument() throws WTException, SQLException, PropertyVetoException, IOException {

....

....

 

 

VladiSlav
17-Peridot
(To:hlafkir)

hlafkir,I repeated the example https://www.ptc.com/en/support/article/CS24105 , but I get errors:

wt.util.WTRemoteException: Unable to invoke remote method; nested exception is:
java.lang.ClassNotFoundException: com.acme.test.excel.ServerClass
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at wt.method.MethodServerImpl.getTargetClass(MethodServerImpl.java:451)
at wt.method.MethodServerImpl.invoke(MethodServerImpl.java:374)

package com.acme.test.excel;

import wt.method.RemoteMethodServer;

public class TxtToExcel {
    public static void main(String[] args) {
        RemoteMethodServer rms = RemoteMethodServer.getDefault();
        rms.setUserName("wcadmin");
        rms.setPassword("wcadmin");
        
        try {
            rms.invoke("doSomethingInMS",
                    "com.acme.test.excel.ServerClass", null,
                    new Class[]{String.class},
                    new Object[]{"TxtToExcel"});
            System.out.println("Should have worked, check the MS log");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


package com.acme.test.excel;

import wt.method.RemoteAccess;

public class ServerClass implements RemoteAccess {
    public static void doSomethingInMS(String msg) 
    {
        System.out.println("Hello World from "+ msg +" in MS !!");
    }
}
hlafkir
13-Aquamarine
(To:VladiSlav)

Please try this simple invocation : 

 

package ext;

import java.beans.PropertyVetoException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;

import wt.content.ApplicationData;
import wt.content.ContentRoleType;
import wt.content.ContentServerHelper;
import wt.doc.DocumentType;
import wt.doc.WTDocument;
import wt.fc.Persistable;
import wt.fc.PersistenceHelper;
import wt.folder.Folder;
import wt.folder.FolderEntry;
import wt.folder.FolderHelper;
import wt.inf.container.WTContainerHelper;
import wt.inf.container.WTContainerRef;
import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;
import wt.pom.Transaction;
import wt.util.WTException;

public class CreateDoc implements RemoteAccess {
	
	public static void main(String[] args)
	  {
	     // Retrieve a MethodServer remote reference
	     // from the ServerManager
	     RemoteMethodServer rms = RemoteMethodServer.getDefault();

	     // Authenticate using the default
	     // HTTPLogin Authenticator
	     rms.setUserName("wcadmin");
	     rms.setPassword("ts");

	     try
	     {
	         rms.invoke("CreateDocument",
	              "ext.CreateDoc", null, null, null);
	         System.out.println("Should have worked, check the MS log");
	     }
	     catch (Exception e)
	     {
	         e.printStackTrace();
	     }
	   }
	
	
public static void CreateDocument() throws WTException, SQLException, PropertyVetoException, IOException {
		
	
	Transaction tx = new Transaction();
	try {
	tx.start();

	    
	WTDocument epm = WTDocument.newWTDocument("MyName1", "MyNumber1", DocumentType.getDocumentTypeDefault());




	    WTContainerRef container_ref = WTContainerHelper.service.getByPath("/wt.inf.container.OrgContainer=Demo Organization/wt.pdmlink.PDMLinkProduct=Drive System");
	    Folder folder = FolderHelper.service.getFolder("/Default",container_ref);
	 

	    WTContainerHelper.setContainer(epm, container_ref);

	    FolderHelper.assignLocation((FolderEntry) epm, folder);

	    PersistenceHelper.manager.save(epm);



	    ApplicationData theContent = ApplicationData.newApplicationData(epm);

	    String filename = "test.txt";

	    File theFile=new File("C:\\test.txt");

	    theContent.setFileName(filename);

	    theContent.setUploadedFromPath(theFile.getPath());

	    theContent.setRole(ContentRoleType.toContentRoleType("PRIMARY"));

	    theContent.setFileSize(theFile.length());
	    
	    



	    FileInputStream fis = new FileInputStream (theFile);

	    theContent = ContentServerHelper.service.updateContent(epm, theContent, fis);



	    ContentServerHelper.service.updateHolderFormat(epm);

	    epm = (WTDocument) PersistenceHelper.manager.refresh((Persistable)epm, true, true);

	    fis.close();
	    
	    
	    tx.commit();
	    tx = null;
	    }
	    catch (Exception e)
	    {
	    e.printStackTrace();
	    }
	    finally
	    {
	    if(tx!=null)
	    tx.rollback();
	    }
	}

}
Top Tags