0

I am trying to create zip from a sharepoint folder which has multiple files. I am able to get the content of each file into an array ( as per diagram below) . But I am having challenges to pull the content from the array and create one zip file for all the files ("Create file" step).

enter image description here

Have anyone done something similar to this before?

regards, Alan

Alan B
  • 2,219
  • 4
  • 32
  • 62
  • An Azure function will do it for you, have you looked into using that approach? – Skin Sep 14 '22 at 03:52
  • The client only uses logic app, so I have to go with logic app – Alan B Sep 14 '22 at 04:03
  • Azure functions are built into the Azure landscape, you extend your LogicApp using a function. If they don't want to use functions then you're going to be limited at times. Have you asked them? – Skin Sep 14 '22 at 04:50
  • Yeah ..we are going with azure function option. Thanks – Alan B Sep 14 '22 at 23:16

1 Answers1

1

I could able to achieve your requirement using a 3rd party connector called Encodian which has an action called Add to Archive (ZIP). Below is the flow that worked for me.

enter image description hereenter image description here

Firstly, I tried to retrieve the properties and the contents of each and every file from the folder that I'm trying to zip. Then I stored its details in below format into an array which will be passed through Add to Archive (ZIP) action and finally saved the results by Create file action of SharePoint connector.

{
  "fileContent": @{base64(body('Get_file_content'))},
  "fileName": @{body('Get_file_properties')?['{FilenameWithExtension}']}
}

Here is the structure of my files in SharePoint

enter image description here

Before Compression

enter image description here

After Compression

enter image description here

you can reproduce the same in your logic app using below code view

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Add_to_Archive_(ZIP)": {
                "inputs": {
                    "body": {
                        "FinalOperation": true,
                        "documents": "@variables('Files')",
                        "encoding": "UTF8",
                        "encryption": "ZipCrypto",
                        "outputFilename": "ZipFolder.zip"
                    },
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['encodiandocumentmanager']['connectionId']"
                        }
                    },
                    "method": "post",
                    "path": "/api/v1/Core/AddToZip"
                },
                "runAfter": {
                    "For_each": [
                        "Succeeded"
                    ]
                },
                "type": "ApiConnection"
            },
            "Create_file": {
                "inputs": {
                    "body": "@base64ToBinary(body('Add_to_Archive_(ZIP)')?['FileContent'])",
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['sharepointonline']['connectionId']"
                        }
                    },
                    "method": "post",
                    "path": "/datasets/@{encodeURIComponent(encodeURIComponent('h<YOUR-SITE-URL>'))}/files",
                    "queries": {
                        "folderPath": "/Shared Documents",
                        "name": "@body('Add_to_Archive_(ZIP)')?['Filename']",
                        "queryParametersSingleEncoded": true
                    }
                },
                "runAfter": {
                    "Add_to_Archive_(ZIP)": [
                        "Succeeded"
                    ]
                },
                "runtimeConfiguration": {
                    "contentTransfer": {
                        "transferMode": "Chunked"
                    }
                },
                "type": "ApiConnection"
            },
            "For_each": {
                "actions": {
                    "Condition": {
                        "actions": {
                            "Append_to_array_variable": {
                                "inputs": {
                                    "name": "Files",
                                    "value": {
                                        "fileContent": "@base64(body('Get_file_content'))",
                                        "fileName": "@body('Get_file_properties')?['{FilenameWithExtension}']"
                                    }
                                },
                                "runAfter": {
                                    "Get_file_content": [
                                        "Succeeded"
                                    ]
                                },
                                "type": "AppendToArrayVariable"
                            },
                            "Get_file_content": {
                                "inputs": {
                                    "host": {
                                        "connection": {
                                            "name": "@parameters('$connections')['sharepointonline']['connectionId']"
                                        }
                                    },
                                    "method": "get",
                                    "path": "/datasets/@{encodeURIComponent(encodeURIComponent('<YOUR-SITE-URL>'))}/files/@{encodeURIComponent(body('Get_file_properties')?['{Identifier}'])}/content"
                                },
                                "runAfter": {},
                                "type": "ApiConnection"
                            }
                        },
                        "expression": {
                            "and": [
                                {
                                    "equals": [
                                        "@body('Get_file_properties')?['{IsFolder}']",
                                        false
                                    ]
                                }
                            ]
                        },
                        "runAfter": {
                            "Get_file_properties": [
                                "Succeeded"
                            ]
                        },
                        "type": "If"
                    },
                    "Get_file_properties": {
                        "inputs": {
                            "host": {
                                "connection": {
                                    "name": "@parameters('$connections')['sharepointonline']['connectionId']"
                                }
                            },
                            "method": "get",
                            "path": "/datasets/@{encodeURIComponent(encodeURIComponent('<YOUR-SITE-URL>'))}/tables/@{encodeURIComponent(encodeURIComponent('<->'))}/items/@{encodeURIComponent(items('For_each')?['ID'])}/getfileitem",
                            "queries": {
                                "view": "c5febd41-6227-4240-b023-0be5688aab0d"
                            }
                        },
                        "runAfter": {},
                        "type": "ApiConnection"
                    }
                },
                "foreach": "@body('Get_files_(properties_only)')?['value']",
                "runAfter": {
                    "Get_files_(properties_only)": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Get_files_(properties_only)": {
                "inputs": {
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['sharepointonline']['connectionId']"
                        }
                    },
                    "method": "get",
                    "path": "/datasets/@{encodeURIComponent(encodeURIComponent('<YOUR-SITE-URL>'))}/tables/@{encodeURIComponent(encodeURIComponent('<->'))}/getfileitems"
                },
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "ApiConnection"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Files",
                            "type": "array"
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {
        "$connections": {
            "value": {
                "encodiandocumentmanager": {
                    "connectionId": "/subscriptions/<SUB - ID>/resourceGroups/<RG>/providers/Microsoft.Web/connections/encodiandocumentmanager",
                    "connectionName": "encodiandocumentmanager",
                    "id": "/subscriptions/<SUB - ID>/providers/Microsoft.Web/locations/centralus/managedApis/encodiandocumentmanager"
                },
                "sharepointonline": {
                    "connectionId": "/subscriptions/<SUB - ID>/resourceGroups/<RG>/providers/Microsoft.Web/connections/sharepointonline",
                    "connectionName": "sharepointonline",
                    "id": "/subscriptions/<SUB - ID>/providers/Microsoft.Web/locations/centralus/managedApis/sharepointonline"
                }
            }
        }
    }
}
SwethaKandikonda
  • 7,513
  • 2
  • 4
  • 18