function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sbbsbb 

Publishing a document into Content workspace using API

I have been trying to upload a file to the 'Content' area using API (from my Java program) -- but it is ending up in the Personal workspace instead of where I want it to go (another workspace I have created for this purpose). No matter what I try, it appears to have no effect! So I am wondering if it is really possible to publish to a workspace using API? If it is, could someone please tell me what I am doing wrong (obviously, I am a newbie -- so please help!)?

 

Thanks!

 

Here is my code snippet:

 

 

  ContentVersion cv = new ContentVersion();

  File file = new File("C:/tests/test.txt");

  byte[] myFileByteArray = null;

 

  ContentWorkspaceDoc wsDoc = new ContentWorkspaceDoc();

  ContentWorkspace ws = null;

 

  try{

  QueryResult qr = binding.query("SELECT id from ContentWorkspace WHERE name = 'Email Attachments' limit 1");

  SObject[] records = qr.getRecords();

 

  if (qr.getSize() > 0){

  System.out.println("Workspace found");

  ws = (ContentWorkspace) records[0];

  }

 

  myFileByteArray = getBytesFromFile(file);

 

  cv.setVersionData(myFileByteArray);

         cv.setTitle("Attachment via API");

  cv.setPathOnClient(file.getPath());

 

  wsDoc.setContentWorkspaceId(ws.getId());

        System.out.println("Workspace set");

 

          cv.setContentDocumentId(wsDoc.getContentDocumentId());

       

 

 

  SaveResult sr = binding.create(new com.sforce.soap.enterprise.sobject.SObject[] {cv})[0];

  if (sr.isSuccess()) {

         System.out.println("Successfully added attachment.");

        System.out.println("Status = " + cv.getPublishStatus());

    } else {

        System.out.println("Error adding attachment: " +sr.getErrors(0).getMessage());

    }

 

 }catch (Exception e){

  e.printStackTrace();

}

 

Best Answer chosen by Admin (Salesforce Developers) 
sbbsbb

Obviously, I was doing it wrong. All I needed to do was set the 'FirstPublishLocationId' property on ContentVersion to the ID of the workspace where I wanted the document to appear. Problem solved!