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

cancel
Showing results for 
Search instead for 
Did you mean: 
abhinavmishra14
Moderator
Moderator

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

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-C02, GAIQ)
Sanjana_17
Active Member

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

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)