cancel
Showing results for 
Search instead for 
Did you mean: 

Restrict Search Results using rest API

sergiobaiao
Champ in-the-making
Champ in-the-making

Hi there,

I'm building a PHP client to perform an Alfresco search using the API for "public" access to some alfresco documents. I've created a site within Alfresco and have a set of folders there. I can already display both folders and files from Alfresco, but there are some folders on this site that i don't want to appear. All of those "restricted" folders start with an underscore, like _docs, for example.

Is there a way i can restrict these folders starting with "_" to not appear on the search results, including any subfolder and/or documents within it?

2 REPLIES 2

fedorow
Elite Collaborator
Elite Collaborator

I would not use underline. Alfresco is a content managment. Create custom type for the public folders or for restricted folders. If custom types is superfluous, use tags. Tag all restricted folders as 'restricted' and add it to your queryies. For example:

{
  "query": {
    "query": "<your query> AND -TAG:\"restricted\"",
    "language": "afts"
  }
}

Nicholas002
Champ in-the-making
Champ in-the-making

To restrict search results from including folders and documents with names starting with an underscore (_) in Alfresco using the REST API, you can leverage the search query capabilities provided by Alfresco. Alfresco supports powerful search queries using the Alfresco Query Language (AQL) or the Lucene query language.

Here's a general approach to achieve this:

Constructing the Search Query: Build a search query that excludes folders and documents whose names start with an underscore. You can use the Alfresco Query Language (AQL) or Lucene query language to formulate this query.

Performing the Search: Execute the constructed search query against Alfresco's repository using the Alfresco REST API.

Processing Search Results: Process the search results returned by the API call and display the desired folders and documents, excluding those that match the criteria for exclusion (i.e., names starting with an underscore).

Below is an example of how you can construct and execute a search query using the Alfresco REST API in PHP:

<?php

// Define Alfresco API endpoint and credentials
$apiUrl = "https://your-alfresco-instance.com/alfresco/api/-default-/public/search/versions/1/search";
$username = "your_username";
$password = "your_password";

// Construct the search query to exclude folders and documents with names starting with an underscore
$searchQuery = 'ASPECT:"cm:folderish" AND NOT (NAME:"_/")';

// Prepare cURL request
$ch = curl_init($apiUrl . "?q=" . urlencode($searchQuery));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

// Execute the cURL request
$response = curl_exec($ch);

// Check for errors
if(curl_errno($ch)){
echo 'Error: ' . curl_error($ch);
}

// Close cURL session
curl_close($ch);

// Decode the JSON response
$searchResults = json_decode($response, true);

// Process and display search results
if(isset($searchResults['list']['entries'])) {
foreach ($searchResults['list']['entries'] as $entry) {
// Process each search result entry (folder or document)
// Display or use the relevant information as needed
echo "Name: " . $entry['entry']['name'] . "<br>";
echo "Type: " . $entry['entry']['entryType'] . "<br>";
echo "<br>";
}
} else {
echo "No search results found.";
}

?>

In this example, the search query ASPECT:"cm:folderish" AND NOT (NAME:"_/") is used to exclude folders and documents with names starting with an underscore (_) from the search results. Adjust the query as needed based on your specific requirements and the structure of your Alfresco repository.

Make sure to replace "BallSportsGear", "your_username", and "your_password" with your actual Alfresco API endpoint URL and credentials.

By using this approach, you can restrict search results from including folders and documents with names starting with an underscore (_) in your Alfresco repository via the REST API.