cancel
Showing results for 
Search instead for 
Did you mean: 

How to find how many folders and documents are present in a site?

madhu_sudireddy
Champ on-the-rise
Champ on-the-rise

How to find how many folders and documents are present in a site?

Can someone please help with this?

11 REPLIES 11

sanjaybandhaniya
Elite Collaborator
Elite Collaborator

If alfresco has a web script for getting number of folders in a site, can you please share?

With this web script, i'm only getting count of folders in document library not their sub folders, I need to get the sub-folders count also included, sorry din't mention it before.

For that type of structure,you have to create your own script.

this is only giving the folders in document library not sub folders , can you provide me the script for sub folders count also.

How can one use this api in java code, I have similar kind of requirement and I tried calling this api through the code but could not achieve this due to authentication issue(401 error).

Create a javascript of java backed webscript. Write search query that will take siteshortname and object type (your custom content type, custom folder type, cm:content, cm:folder etc.) and performs a search and returns the count. 

There is a high level code which you can implement in your java backed webscript:


   Query would be something like: 	  
   
   String countQuery = 'PATH:"/app:company_home/st:sites/cm:' + search.ISO9075Encode(siteShortName) + '//*" AND TYPE:"'+type+'"';
   Where siteShortName and type you will pass for getting the related results.
   
   You will below sample method to get the count and use it whereever you want:
   
   long countResult = getObjectCount(countQuery);

	public long getObjectCount(String query) {
		SearchParameters searchParameters = new SearchParameters();
		searchParameters.setQuery(query);
        searchParameters.setLanguage(SearchService.LANGUAGE_LUCENE);
        searchParameters.addStore(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE);
        searchParameters.setMaxItems(Integer.MAX_VALUE);
        searchParameters.setMaxPermissionChecks(Integer.MAX_VALUE);
		ResultSet results = null;
		long objectCount = 0;
		try {
			results = searchService.query(searchParameters);
			if (null != results) {
				objectCount = results.getNumberFound();
			}
		} catch (AlfrescoRuntimeException alfException) {
			LOGGER.error(alfException.getMessage(), alfException);
		} finally {
			if (results != null) {
				results.close();
			}
		}
		return objectCount;
	}

For javascript you can refer below sample and customize as per your need:

<webscript>
<shortname>Count Files/folder</shortname>
<description>
Counts Objects of given type

Parameter name : {type}
Example : custom:contentType, cm:content, cm:folder etc.

Parameter name : {siteShortName}
Example : test-site
</description>
<url>/countObjects?type={type}&amp;siteShortName={siteShortName}&amp;skipCount={skipCount}&amp;maxCount={maxCount}</url>
<format>any</format>
</webscript>
function main(){

	var siteCount = [];
	
	var type = args["type"];
	var siteShortName = args["siteShortName"];
  
	var skipCount = (args["skipCount"]==null || args["skipCount"]==undefined)?0:args["skipCount"];
	var maxCount = (args["maxCount"]==null || args["maxCount"]==undefined)?100000:args["maxCount"];
   
      var countQuery = 'PATH:"/app:company_home/st:sites/cm:' + search.ISO9075Encode(siteShortName) + '//*" AND TYPE:"'+type+'"';
      logger.log("CountQuery: "+countQuery);
		
      var page = {
			skipCount : parseInt(skipCount),
			maxItems : parseInt(maxCount)
	  };
      
      var searchQuery = {
		  query : countQuery,
		  language : "lucene",
		  page : page
	  };
      
	  var nodes = search.query(searchQuery);      
      var nodeCount = nodes.length;
	  siteCount[siteShortName] = nodeCount;	
  
      model.siteCount=siteCount;
}

main();
 
<html>
<head>
<title>Count of a given type</title>
</head>
<body>
<h4>Count of the given objects</h4>
<b>Count Query:</b> <#if query??>${query}<#else>-</#if><br/>
<hr/>
<#if siteCount?is_hash_ex>
<table border="1">
<tr>
<th align="center">Site Short Name</th>
<th align="center">Count</th>
</tr>
<#assign keys = siteCount ?keys>
<#list keys as key>
<tr>
<td align="center">${key}</td>
<td align="center">${siteCount [key]}</td>
</tr>
</#list>
</table>
</#if>
</body>
</html>
~Abhinav
(ACSCE, AWS SAA, Azure Admin)

Thank you for the revert.

I was able to count records in a datalist with this approach(Java backed code). 

I also wanted to calculate total size of the datalist in terms of MB/GB(based on amount of records present). What are the possible methods which will return this result. Also, for document library how can I caculate the total number of files and folder count alongwith total size of documentlibrary present using java code. (required for a timer job)