cancel
Showing results for 
Search instead for 
Did you mean: 

Check file or Folder is Exists or not

prakasht
Champ in-the-making
Champ in-the-making
Hi.
I am a new in Alfresco. I am using the 5.1 Alfresco community version.
I want to check the From Specific folder is Exist or not with there name Using JAVA API.


Thanks In Advance.
5 REPLIES 5

prakasht
Champ in-the-making
Champ in-the-making
thanks for reply but i want to check my folder is exits or not by foldername not by nodeRef.

angelborroy
Community Manager Community Manager
Community Manager
Then you can use SearchService and query by using "PATH" from Lucene syntax.
Hyland Developer Evangelist

adit_patel
Confirmed Champ
Confirmed Champ

You can use getChildByName function

NodeService (Alfresco 5.2-SNAPSHOT API) 

jpotts
World-Class Innovator
World-Class Innovator

Here's a runnable class that leverages OpenCMIS to check for the presence of a folder with a given name.

package com.someco.cmis.examples;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.ItemIterable;
import org.apache.chemistry.opencmis.client.api.QueryResult;
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.SessionParameter;
import org.apache.chemistry.opencmis.commons.enums.BindingType;

public class FolderChecker {
     private String serviceUrl = "http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/browser";
    private Session session = null;

    public static void main(String[] args) {
         if (args.length != 1) {
              System.out.println("USAGE: java FolderChecker [folder name]");
              System.exit(-1);
         }
         FolderChecker fc = new FolderChecker();
         System.out.println("Does folder: " + args[0] + " exist? " + fc.doesFolderExist(args[0]));
    }
   
    public boolean doesFolderExist(String folderName) {
          String queryString = "select cmis:objectId from cmis:folder where cmis:name = '" + folderName + "'";
         ItemIterable<QueryResult> results = getSession().query(queryString, false);
         if (results.getTotalNumItems() > 0) {
              return true;
         } else {
              return false;
         }
    }
   
     public Session getSession() {

          if (session == null) {
               // default factory implementation
               SessionFactory factory = SessionFactoryImpl.newInstance();
               Map<String, String> parameter = new HashMap<String, String>();
     
               // user credentials
               parameter.put(SessionParameter.USER, "admin"); // <-- Replace
               parameter.put(SessionParameter.PASSWORD, "admin"); // <-- Replace
     
               // connection settings
               parameter.put(SessionParameter.BROWSER_URL, this.serviceUrl); // Uncomment for Atom Pub binding
               parameter.put(SessionParameter.BINDING_TYPE, BindingType.BROWSER.value()); // Uncomment for Atom Pub binding

               List<Repository> repositories = factory.getRepositories(parameter);
     
               this.session = repositories.get(0).createSession();
          }
          return this.session;
     }
}