cancel
Showing results for 
Search instead for 
Did you mean: 

How to programmatically create space?

amit_wmw
Champ in-the-making
Champ in-the-making
Hello,

Can anybody please suggest me how to programmatically (java) create space under companyHome? A sample code will be of great help!

Thanks,
Amit.
12 REPLIES 12

mwildam
Champ in-the-making
Champ in-the-making
java.lang.IllegalStateException: The org.alfresco.cache.globalConfigCache Cache is not alive.

Why? Any idea about the problem and the solution?
I have the same error trying to access Alfresco suddenly - but on that particular Alfresco installation I did not do anything programmatically, it is quite a default installation.

gkeuss
Champ on-the-rise
Champ on-the-rise
Try to get the node service this way :

FacesContext context = FacesContext.getCurrentInstance();
NodeService nodeService = Repository.getServiceRegistry(context).getNodeService();

ameenulla
Champ in-the-making
Champ in-the-making
Example code to create space


import org.alfresco.webservice.content.Content;
import org.alfresco.webservice.content.ContentServiceSoapBindingStub;
import org.alfresco.webservice.repository.UpdateResult;
import org.alfresco.webservice.types.CML;
import org.alfresco.webservice.types.CMLAddAspect;
import org.alfresco.webservice.types.CMLCreate;
import org.alfresco.webservice.types.ContentFormat;
import org.alfresco.webservice.types.NamedValue;
import org.alfresco.webservice.types.ParentReference;
import org.alfresco.webservice.types.Predicate;
import org.alfresco.webservice.types.Reference;
import org.alfresco.webservice.types.Store;
import org.alfresco.webservice.util.AuthenticationUtils;
import org.alfresco.webservice.util.Constants;
import org.alfresco.webservice.util.Utils;
import org.alfresco.webservice.util.WebServiceFactory;

public class Spaceunder {
   static Store STORE = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
   static String spaceName = "example";
   static String path = "/app:company_home/cm:testspace";
   private static final int BUFFER_SIZE = 1024;
   public static void main(String[] args) {

      try {
         WebServiceFactory.setEndpointAddress("http://localhost:8888/alfresco/api");
         AuthenticationUtils.startSession("admin", "admin");
         try{
         createSpace();
         }catch (Throwable e) {
            e.printStackTrace();
         }
      } catch (Exception e) {
          e.printStackTrace();
      } finally {
         try {
            AuthenticationUtils.endSession();
         } catch (Exception e1) {
             e1.printStackTrace();
         }
      }

   }

   private static void  createSpace(){
      try {
         Reference contentReference = new Reference(STORE, null, path+ "/cm:" + spaceName.replace(" ", "_x0020_"));
         WebServiceFactory.getRepositoryService().get(new Predicate(new Reference[] { contentReference },STORE, null));

      } catch (Exception e) {
         try {
            // create a folder with instance Id If it is already created it
            // will throw Exception
            ParentReference parentReference = new ParentReference(STORE, null, path, Constants.ASSOC_CONTAINS,Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL,spaceName));
            NamedValue[] properties = new NamedValue[] { Utils.createNamedValue(Constants.PROP_NAME, spaceName) };
            CMLCreate create = new CMLCreate("1", parentReference,null, null, null, Constants.TYPE_FOLDER, properties);

            CML cml = new CML();
            cml.setCreate(new CMLCreate[] { create });
            WebServiceFactory.getRepositoryService().update(cml);
         } catch (Exception e1) {
            e1.printStackTrace();
         }
      }
   }      
}