In this post, I will share both ways to create a recursive call of a Logic App.
Commun Parts
Let's assume that our goal is to crawl a folder structure. It could be in any file connector: DroxBox, OneDrive, Google Drive, etc. For this post, I will use Sharepoint Online connector.
First, let's create our Logic App. Use the Http Request-Response template.
- Our Logic App will receive the folder path, that it needs to process, as a parameter. This is easily done by defining a JSON schema in the Request trigger.
{ "$schema": "http://json-schema.org/draft-04/schema#", "properties": { "FolderName": { "type": "string" } }, "type": "object" }
- To list all the content of the current folder. Add an action List folder from the SharePoint Online connector. The File identifier should be the trigger parameter:
FolderName
.
Note: You need to edit the code behind for this action. I notice a strange behavior with space in the folder path.
The current code should look like this:
"path": "/datasets/@{encodeURIComponent(encodeURIComponent('https://mysharepoint.sharepoint.com/sites/FrankSiteTest'))}/folders/@{encodeURIComponent(triggerBody()?['FolderName'])}"
Change it by doubling theencodeURIComponent
"path": "/datasets/@{encodeURIComponent(encodeURIComponent('https://mysharepoint.sharepoint.com/sites/FrankSiteTest'))}/folders/@{encodeURIComponent(encodeURIComponent(triggerBody()?['FolderName']))}"
- For each element returned we need to check if it's a folder. One property of the returned object is
IsFolder
, we just need to use it in our condition:
@equals(bool(item()?['IsFolder']), bool('True'))
- If it's a folder, we need to do some recursive call passing the path of the current folder to
FolderName
.
- Otherwise, when it's a file, do some process.
- If it's a folder, we need to do some recursive call passing the path of the current folder to
Method 1: The quick and easy
Since we are forced to call a different Logic App, let's clone our Logic App. Close the editor and click the Clone button, name it FolderCrawler2.
Now we need to edit both Logic apps by adding a call to the other one. FolderCrawler is calling FolderCrawler2 and FolderCrawler2 calls FolderCrawler.
This method is really just a workaround, but it works perfectly. What I like about it is that it uses all the Intellisense at our disposal in the editor. Obviously, the big disadvantage is the code duplication.
Method 2: The Clean and light
The real way to do a recursive call is to use the URL from the Request in an HTTP POST call. Than in the body pass a JSON matching the schema containing the
Path
value. I hope you enjoy this little post. If you think of another way to do recursive call or if you have some questions, let me know!