Populate List of Shared Files

cancel
Showing results for 
Search instead for 
Did you mean: 
eswbitto
Active Member

Populate List of Shared Files

Hello,

 

I'm using Alfresco API exporer to run a query to list all files that have been "shared" with a public link. I need to be able to go through and remove that public link. What I have been doing is using this.

https://mydomain/alfresco/api/-default-/public/alfresco/versions/1/shared-links?skipCount=0&maxItems=1000

 

I get results of everything, but I need the actual path to where the file is and possibly from which site.

3 Replies
cesarista
Customer

Re: Populate List of Shared Files

Hi:

You can do this task with JS Console, or even with a custom webscript. You can search all public documents with an alfresco fts query:

'ASPECT:"qshare:shared"

And for the list of obtained noderefs just remove the aspect.

Kind regards.

--C. 

eswbitto
Active Member

Re: Populate List of Shared Files

I am not familiar with using a fts query. Where and how would I go about it?

abhinavmishra14
Advanced

Re: Populate List of Shared Files

You can use below search api to get list of all shared files:

 

POST: http://localhost:8080/alfresco/api/-default-/public/search/versions/1/search

Payload:

{
 "query": {
    "query": "+TYPE:\"cm:content\" AND +ASPECT:\"qshare:shared\"",
    "language": "afts"
  }
}

This will return list of files, but you are looking for display path which may not be possible to get using the v1 search api.

You would have to write a custom java/js webscript that will execute fts query and return the list of nodes. Using the returned nodes you can extract the displaypath.

You seem to be unware of fts queries and stuff. I would recommend to go through these documentations:

https://docs.alfresco.com/search-enterprise/concepts/searchsyntax-intro.html

https://docs.alfresco.com/6.2/concepts/dev-api-by-language-alf-rest-finding-content-by-search-query....

https://docs.alfresco.com/6.2/tasks/ws-tutorials.html

Here is a sample javascript webscript, you can register it dynamically by uploading the file in "/Data Dictionary/Web Scripts Extensions/" folder in repository and refresh the webscripts.

get-shared-files.get.desc.xml

<webscript>
     <shortname>Get shared files information</shortname>
     <url>/search/get-shared-files?skipCount={skipCount}&amp;maxCount={maxCount}</url>
     <authentication>admin</authentication>
</webscript>

get-shared-files.get.js

main();

function main() {
    var skipCount = (args["skipCount"] == null || args["skipCount"] == undefined) ? 0 : args["skipCount"];
    var maxCount = (args["maxCount"] == null || args["maxCount"] == undefined) ? 10000 : args["maxCount"];

    var successNodes = [];
    var failedNodes = [];
    var totalNodes;
     
    var query = "+TYPE:\"cm:content\" AND +ASPECT:\"qshare:shared\"";
    var page = {
        skipCount: parseInt(skipCount),
        maxItems: parseInt(maxCount)
    };

    var searchQuery = {
        query: query,
        language: "fts-alfresco",
        page: page
    };

    logger.log("Executing SearchQuery: " + query)
    var nodes = search.query(searchQuery);
    totalNodes = nodes.length;
    logger.log("Total Nodes: " + totalNodes)

    for each(node in nodes) {
        var sharedFileDetails = [];
        var fileName = node.name;
        var nodeReference = node.nodeRef;
        try {
            logger.log("Fetching details for file: " + fileName);
            sharedFileDetails["fileName"] = fileName;
            sharedFileDetails["NodeRef"] = nodeReference;
			var fullDP = node.displayPath+"/"+fileName;
            sharedFileDetails["DisplayPath"] = fullDP;
			logger.log("DisplayPath: " + fullDP);
            successNodes[nodeReference] = sharedFileDetails;
        } catch (ex) {
            logger.log("Exception occurred: " + ex.message);
            failedNodes[nodeReference] = fileName + " - " + ex.message;
        }
    }
     
    model.successNodes = successNodes;
    model.failedNodes = failedNodes;
    model.totalNodes = totalNodes;
}

get-shared-files.get.html.ftl

<html>
<head>
<title>Shared File Information</title>
</head>
<body>
     <h3>Total Folders: <#if totalNodes??>${totalNodes}<#else>-</#if></h3>
     <#if successNodes??>
         <table border="1">
              <tr>
                   <th align="center">FileName</th>
                   <th align="center">Type And NodeRef</th>
                   <th align="center">DisplayPath</th>
              </tr>
           
          <#list successNodes?keys as nodeReference>
               <tr>
                <td align="center"> ${successNodes[nodeReference].fileName}</td>
                <td align="center"> ${successNodes[nodeReference].NodeRef}</td>
                <td align="center"> ${successNodes[nodeReference].DisplayPath}</td>
               </tr>
        </#list>
            </table>
     </#if>
     
     <#if failedNodes?is_hash_ex>
         <table border="1">
              <tr>
                   <th align="center">NodeRef</th>
                   <th align="center">ErrorMsg</th>
              </tr>
              <#assign keys = failedNodes ?keys>
           <#list keys as key>
             <tr>
                     <td align="center">${key}</td>
                     <td align="center">${failedNodes [key]}</td>
                </tr>
           </#list>
            </table>
     </#if>
</body>
</html>

Once the webscript is registred and refreshed, use this url to get the response:

http://127.0.0.1:8080/alfresco/service/search/get-shared-files?skipCount=0&maxCount=1000

Your result will similar to this one:

FileName Type and NodeRef  Display Path
Test.pdf Node Type: cm:content NodeRef: workspace://SpacesStore/4e28d5fd-f596-4403-b61b-2501ae910e0d  /Company Home/Sites/test/documentLibrary/Test.pdf

 

You can update the same webscript to remove the "qshare:shared" aspect, e.g.:

for each(node in nodes) {
        var sharedFileDetails = [];
        var fileName = node.name;
        var nodeReference = node.nodeRef;
        try {
            logger.log("Fetching details for file: " + fileName);
            sharedFileDetails["fileName"] = fileName;
            sharedFileDetails["NodeRef"] = nodeReference;
			var fullDP = node.displayPath+"/"+fileName;
            sharedFileDetails["DisplayPath"] = fullDP;
			logger.log("DisplayPath: " + fullDP);
            if(node.hasAspect("qshare:shared")) {
               node.removeAspect("qshare:shared");
            }
            successNodes[nodeReference] = sharedFileDetails;
        } catch (ex) {
            logger.log("Exception occurred: " + ex.message);
            failedNodes[nodeReference] = fileName + " - " + ex.message;
        }
    }

This will remove the shared aspect and files will no more be publicly accesible. 

 

 

 

~Abhinav
(ACSCE, AWS SAA, Azure Admin)