cancel
Showing results for 
Search instead for 
Did you mean: 

How to programmatically create content?

beliasnkyas
Champ in-the-making
Champ in-the-making
Hello,

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

Thanks,
1 REPLY 1

mrogers
Star Contributor
Star Contributor
The following code is extracted from one of alfresco's unit tests.

It creates a folder in company_home/guest_home and creates a node in that folder.    You may also want to use the FileFolderService instead, its a bit "higher level" than the node service.

               String guestHomeQuery = "/app:company_home/app:guest_home";
               ResultSet guestHomeResult = searchService.query(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, SearchService.LANGUAGE_XPATH, guestHomeQuery);
               assertEquals("", 1, guestHomeResult.length());
               NodeRef guestHome = guestHomeResult.getNodeRef(0);
      
               /**
                * Create a test node that we will read and write
                */
               String guid = GUID.generate();
              
               ChildAssociationRef child;
              
               child = nodeService.createNode(guestHome, ContentModel.ASSOC_CONTAINS, QName.createQName(guid), ContentModel.TYPE_FOLDER);
               NodeRef testRootNode = child.getChildRef();
               nodeService.setProperty(testRootNode , ContentModel.PROP_TITLE, guid);  
               nodeService.setProperty(testRootNode , ContentModel.PROP_NAME, guid);
                         
               child = nodeService.createNode(testRootNode, ContentModel.ASSOC_CONTAINS, QName.createQName("testNodeAC"), ContentModel.TYPE_CONTENT);
               NodeRef nodeAC = child.getChildRef();
               nodeService.setProperty(nodeAC , ContentModel.PROP_TITLE, CONTENT_TITLE + "AC");  
                nodeService.setProperty(nodeAC , ContentModel.PROP_NAME, "DemoNodeAC");
           
                {
                    ContentWriter writer = contentService.getWriter(nodeAC , ContentModel.PROP_CONTENT, true);
                    writer.setLocale(CONTENT_LOCALE);
                    writer.putContent(CONTENT_STRING);
                    nodes.add(nodeAC);
                }