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

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

How to add attachment to Problem Report via API ?

vuchekar
9-Granite

How to add attachment to Problem Report via API ?

Hi All,

i want to add document as an attachment to a Problem report.

it can be easily done by UI.

my requirement is to add this attachment via API.

Please let me know how i can do this ? how i can edit/create new Problem report via API.

Thanks,

Vivek

2 REPLIES 2

Vivek,

you can try below code for adding Attachments for PR

Transaction tx = new Transaction();

  if(ProblemReport != null){

  try{

  tx.start();

  ApplicationData theContent = ApplicationData.newApplicationData(ProblemReport);

  File theFile=new File(FilePath);

  theContent.setFileName(FileName);

  theContent.setUploadedFromPath(FilePath);

  theContent.setRole(ContentRoleType.toContentRoleType("SECONDARY")); //if it’s secondary, use PRIMARY

  theContent.setFileSize(theFile.length());

  FileInputStream fis = new FileInputStream (theFile);

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

  ContentServerHelper.service.updateHolderFormat(ProblemReport);

     PersistenceHelper.manager.refresh((Persistable)ProblemReport, true, true);

  fis.close();

  tx.commit();

  tx= null;

  }

  catch(Exception e){

  e.printStackTrace();

  }finally{

  if(tx!=null)

  tx.rollback();

  }

Hope it helps!!!

Thanks

shreyas

Hi Sheryas,

Thanks for your help.

my requirement to create WTDoc first and then attach same document to problem report.

i am faing one issue while creating WTDoc via API,

i am using below code to create WTDocument,

=======

package ext.wtdoc;

import java.beans.PropertyVetoException;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.lang.reflect.InvocationTargetException;

import wt.content.ApplicationData;

import wt.content.ContentRoleType;

import wt.content.ContentServerHelper;

import wt.doc.DocumentType;

import wt.doc.WTDocument;

import wt.fc.ObjectNoLongerExistsException;

import wt.fc.Persistable;

import wt.fc.PersistenceHelper;

import wt.httpgw.GatewayAuthenticator;

import wt.method.RemoteAccess;

import wt.method.RemoteMethodServer;

import wt.util.WTException;

import wt.util.WTPropertyVetoException;

public class CreateWTDoc11 implements RemoteAccess {

  /**

  * @param args

  * @throws WTException

  * @throws ObjectNoLongerExistsException

  * @throws IOException

  * @throws PropertyVetoException

  * @throws InvocationTargetException

  */

  public static void main(String[] args) throws ObjectNoLongerExistsException, WTException, PropertyVetoException, IOException, InvocationTargetException {

  RemoteMethodServer rms = RemoteMethodServer.getDefault();

  GatewayAuthenticator auth = new GatewayAuthenticator();

  auth.setRemoteUser("wcadmin");

  rms.setAuthenticator(auth);

  System.out.println("Auth is done..");

  rms.invoke("createWTDoc1", CreateWTDoc11.class.getName(), null, new Class[]{}, new Object[]{});

  }

  public static void createWTDoc1() throws WTException, PropertyVetoException, IOException{

  String name="TESTWTDOCAPI";

  String number="TESTWTDOCAPI";

  WTDocument doc= WTDocument.newWTDocument(name, number, DocumentType.getDocumentTypeDefault());

  ApplicationData theContent = ApplicationData.newApplicationData(doc);

  String path="C:\\test.txt";

  String filename = "test.txt";

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

  theContent.setFileName(filename);

  theContent.setUploadedFromPath(path);

  theContent.setRole(ContentRoleType.toContentRoleType("PRIMARY")); //if it’s secondary, use “SECONDARY”

  theContent.setFileSize(theFile.length());

  FileInputStream fis = new FileInputStream (theFile);

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

  System.out.println("22222");

  ContentServerHelper.service.updateHolderFormat(doc);

  System.out.println("33333");

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

  System.out.println("444444444444");

  fis.close();

  }

}

=======

when i execute this code, i am getting below exception,

Exception in thread "main" wt.util.WTRemoteException: Unable to invoke remote me

thod; nested exception is:

        java.rmi.ServerRuntimeException: Server exception; nested exception is:

        wt.util.WTRuntimeException: Failed to initialize administrative domain.

        at wt.method.RemoteMethodServer.invoke(RemoteMethodServer.java:793)

        at ext.wtdoc.CreateWTDoc11.main(CreateWTDoc11.java:46)

Caused by: wt.util.WTRuntimeException: Failed to initialize administrative domai

n.

        ... 2 more

Please let me know how i can fix this.

Thanks,

Vivek

Top Tags