cancel
Showing results for 
Search instead for 
Did you mean: 

Setting custom properties(single and multiValue)

anujcb
Confirmed Champ
Confirmed Champ
Hi All,

   Newbie to alfresco here. I am trying to set custom properties on a custom type and the custom properties are not being set. Please help

Here is the code I am using….

         String urlString = "http://'+ipaddress+':8080/alfresco/service/api/upload?alf_ticket="
               + authTicket;
         System.out.println("The upload url:::" + urlString);
         HttpClient client = new HttpClient();

         PostMethod mPost = new PostMethod(urlString);
         // File f1 =fileobj;
         Part[] parts = {
               new FilePart("filedata", filename, fileobj, filetype, null),
               new StringPart("filename", filename),
               new StringPart("contenttype", "rs:invoiceDoc" ),
               new StringPart("description", description),
               new StringPart("rs:itemName","Item1,Item2"),
               new StringPart("rs:vendor","sample vendor"),

               // modify this according to where you wanna put your content
               new StringPart("siteid", siteid),
               new StringPart("containerid", "documentLibrary"),
         // new StringPart("uploaddirectory", "/Company Home")
         };
         mPost.setRequestEntity(new MultipartRequestEntity(parts, mPost
               .getParams()));
         int statusCode1 = client.executeMethod(mPost);


The code actually creates the document of the right content type, rs:invoiceDoc. But the property values rs:itemName and rs:vendor (custom properties of rs:invoiceDoc) are not getting set.  What am I missing?

Also, rs:itemName is a multi valued property, is this the right way to set multiple values to a multi valued property?
new StringPart("rs:itemName","Item1,Item2")

13 REPLIES 13

Thank you, is there an example you can point me to using open CMS for setting custom single and repeating proprties when uploading a document.

kaynezhang
World-Class Innovator
World-Class Innovator
Following is sample code you can use to create a document with single-value and multi value properties.

String url = "http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom";
String userName = "kaynezhang";
String password = "kaynezhang";
SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
Map<String, String> params = new HashMap<String, String>();
params.put(SessionParameter.USER, userName);
params.put(SessionParameter.PASSWORD, password);
params.put(SessionParameter.ATOMPUB_URL, url);
params.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
params.put(SessionParameter.OBJECT_FACTORY_CLASS,
      "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");
List<Repository> repos = sessionFactory.getRepositories(params);
if (repos.isEmpty()) {
   throw new RuntimeException("Server has no repositories!");
}
      
Session session =  repos.get(0).createSession();

//get site document library container
CmisObject folder = (AlfrescoFolder) session.getObjectByPath("/sites/{siteshotname}/documentLibrary");

//upload document

Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
properties.put(PropertyIds.SECONDARY_OBJECT_TYPE_IDS, "cm:titled");

// single-value property
properties.put("xx:Xxx", xxx);
properties.put(PropertyIds.NAME, "xxx");

//multi-value property
List<String> xxList = new ArrayList<String>();
xxList.add("xx");
xxList.add("xx");
xxList.add("xx");
properties.put("my:xx", xx); // multi-value property

//perform upload
File file = new File("d:\\xx.pdf");
InputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fis);
byte[] bytes = new byte[(int) file.length()];
dis.readFully(bytes);
ContentStream contentStream = new ContentStreamImpl(file
      .getAbsolutePath(), BigInteger.valueOf(bytes.length), "application/pdf",
      new ByteArrayInputStream(bytes));

AlfrescoDocument newDocument = (AlfrescoDocument) folder
      .createDocument(properties, contentStream, vs);

anujcb
Confirmed Champ
Confirmed Champ
Finally working with minor updates

Used following libraries.
slf4j1.7.12
apache chemistry opencms 0.10.0
alfresco opencms extension 0.7
alfresco 5.0.c server

NOTE: to use your custom object type prefix the object type name with 'D:' like below
properties.put(PropertyIds.OBJECT_TYPE_ID, "D:rs:receiptsDoc");


import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;


import java.io.InputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alfresco.cmis.client.AlfrescoDocument;
import org.alfresco.cmis.client.AlfrescoFolder;
import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Repository;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.api.SessionFactory;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.PropertyIds;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.enums.BindingType;
import org.apache.chemistry.opencmis.commons.enums.VersioningState;
import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;




public class FileUploadCMIS {

   public void uploadDocument(File fileobj,
         String filename, String filetype, String description,
         String destination, String ipaddress, String pass, String siteid) throws IOException{
      
      String url = "http://'+ipaddress+':8080/alfresco/cmisatom";

      
      try {

         

         SessionFactory sessionFactory = SessionFactoryImpl.newInstance();

         Map<String, String> params = new HashMap<String, String>();

         params.put(SessionParameter.USER, "admin");

         params.put(SessionParameter.PASSWORD, pass);

         params.put(SessionParameter.ATOMPUB_URL, url);

         params.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());

         params.put(SessionParameter.OBJECT_FACTORY_CLASS,

               "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");

         List<Repository> repos = sessionFactory.getRepositories(params);

         if (repos.isEmpty()) {

            throw new RuntimeException("Server has no repositories!");

         }

         

         Session session =  repos.get(0).createSession();

         

         //get site document library container

         CmisObject folder = (AlfrescoFolder) session.getObjectByPath("/sites/"+siteid+"/documentLibrary");

         

         //upload document

         

         Map<String, Object> properties = new HashMap<String, Object>();

         properties.put(PropertyIds.OBJECT_TYPE_ID, "D:rs:receiptsDoc");

         //properties.put(PropertyIds.SECONDARY_OBJECT_TYPE_IDS, "rs:receiptsDoc");

         

         // single-value property

         properties.put("rs:vendor", "some vendor");

         properties.put(PropertyIds.NAME, "some name");

         

         //multi-value property

         List<String> xxList = new ArrayList<String>();

         xxList.add("item 1");

         xxList.add("item 2");

         xxList.add("item 3");

         properties.put("rs:itemName", xxList); // multi-value property

         

         //perform upload

         File file = new File(fileobj.getAbsolutePath());

         InputStream fis = new FileInputStream(file);

         DataInputStream dis = new DataInputStream(fis);

         byte[] bytes = new byte[(int) file.length()];

         dis.readFully(bytes);

         ContentStream contentStream = new ContentStreamImpl(file

               .getAbsolutePath(), BigInteger.valueOf(bytes.length), "application/pdf",

               new ByteArrayInputStream(bytes));

         
         VersioningState vs = null;
         
         AlfrescoDocument newDocument = (AlfrescoDocument)((AlfrescoFolder)folder).createDocument(properties, contentStream, vs);


      } catch (Exception e) {
         e.printStackTrace();
      }
   }

}

ankit
Champ in-the-making
Champ in-the-making
please have look about setting metadata using api