Copy files - SharePoint data to Azure Blob using Logic Apps

61 Views Asked by At

I have a scenerio where i have multiple Parent folders in sharepoint, these folders contain multiple nested or sub folders and then have a file. How can i copy those files to blob by maintaining the same folder structure what we had in Sharepoint using Logic app.

Folder Structure- Under Document folder i have 2 folders Result and Test .

Result -> Result1 -> Result2 -> test.xlsx

test -> Folder1 -> Folder2 -> Folder3 -> file1.xlsx,file2.xlsx,file3.xlsx

Flow which i tried-

enter image description here

enter image description here

1

There are 1 best solutions below

2
Ikhtesam Afrin On BEST ANSWER

I have files in Test/demo/demo1 folder in SharePoint.

enter image description here

I am using the below workflow to copy these files to blob storage by keeping the folder structure intact.

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Code-

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_each": {
                "actions": {
                    "Create_blob_(V2)": {
                        "inputs": {
                            "body": "@body('Get_file_content_using_path')",
                            "headers": {
                                "ReadFileMetadataFromServer": true
                            },
                            "host": {
                                "connection": {
                                    "referenceName": "azureblob"
                                }
                            },
                            "method": "post",
                            "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files",
                            "queries": {
                                "folderPath": "/samples-workitems",
                                "name": "@items('For_each')?['Path']",
                                "queryParametersSingleEncoded": true
                            }
                        },
                        "runAfter": {
                            "Get_file_content_using_path": [
                                "SUCCEEDED"
                            ]
                        },
                        "runtimeConfiguration": {
                            "contentTransfer": {
                                "transferMode": "Chunked"
                            }
                        },
                        "type": "ApiConnection"
                    },
                    "Get_file_content_using_path": {
                        "inputs": {
                            "host": {
                                "connection": {
                                    "referenceName": "sharepointonline"
                                }
                            },
                            "method": "get",
                            "path": "/datasets/@{encodeURIComponent(encodeURIComponent('**********'))}/GetFileContentByPath",
                            "queries": {
                                "inferContentType": true,
                                "path": "@items('For_each')?['Path']",
                                "queryParametersSingleEncoded": true
                            }
                        },
                        "type": "ApiConnection"
                    }
                },
                "foreach": "@body('List_folder')",
                "runAfter": {
                    "List_folder": [
                        "SUCCEEDED"
                    ]
                },
                "type": "Foreach"
            },
            "List_folder": {
                "inputs": {
                    "host": {
                        "connection": {
                            "referenceName": "sharepointonline"
                        }
                    },
                    "method": "get",
                    "path": "/datasets/@{encodeURIComponent(encodeURIComponent('*********'))}/folders/@{encodeURIComponent('%252fTest%252fdemo%252fdemo1')}"
                },
                "metadata": {
                    "%252fTest": "/Test",
                    "%252fTest%252fdemo%252fdemo1": "/Test/demo/demo1"
                },
                "runAfter": {},
                "type": "ApiConnection"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "triggers": {
            "When_a_HTTP_request_is_received": {
                "kind": "Http",
                "runtimeConfiguration": {
                    "concurrency": {
                        "runs": 1
                    }
                },
                "type": "Request"
            }
        }
    },
    "kind": "Stateful"
}

Output-

enter image description here

enter image description here

Update-

I have two below folder structure-

Test -> Folder1 -> Folder2 -> Folder3 -> All the files
Test -> Result -> Result1 -> Result2 -> All the files

My workflow looks like to below-

enter image description here

enter image description here

enter image description here

enter image description here

Code-

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_each": {
                "actions": {
                    "Condition": {
                        "actions": {
                            "Create_blob_(V2)": {
                                "inputs": {
                                    "body": "@body('Get_file_content_using_path')",
                                    "headers": {
                                        "ReadFileMetadataFromServer": true
                                    },
                                    "host": {
                                        "connection": {
                                            "referenceName": "azureblob"
                                        }
                                    },
                                    "method": "post",
                                    "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files",
                                    "queries": {
                                        "folderPath": "/samples-workitems",
                                        "name": "@items('For_each')?['{FullPath}']",
                                        "queryParametersSingleEncoded": true
                                    }
                                },
                                "runAfter": {
                                    "Get_file_content_using_path": [
                                        "SUCCEEDED"
                                    ]
                                },
                                "runtimeConfiguration": {
                                    "contentTransfer": {
                                        "transferMode": "Chunked"
                                    }
                                },
                                "type": "ApiConnection"
                            },
                            "Get_file_content_using_path": {
                                "inputs": {
                                    "host": {
                                        "connection": {
                                            "referenceName": "sharepointonline"
                                        }
                                    },
                                    "method": "get",
                                    "path": "/datasets/@{encodeURIComponent(encodeURIComponent('**************'))}/GetFileContentByPath",
                                    "queries": {
                                        "inferContentType": true,
                                        "path": "@items('For_each')?['{FullPath}']",
                                        "queryParametersSingleEncoded": true
                                    }
                                },
                                "type": "ApiConnection"
                            }
                        },
                        "else": {
                            "actions": {}
                        },
                        "expression": {
                            "or": [
                                {
                                    "equals": [
                                        "@items('For_each')?['{Path}']",
                                        "Test/Folder/Folder1/Folder2/Folder3/"
                                    ]
                                },
                                {
                                    "equals": [
                                        "@items('For_each')?['{Path}']",
                                        "Test/Result/Result1/Result2/"
                                    ]
                                }
                            ]
                        },
                        "type": "If"
                    }
                },
                "foreach": "@body('Get_files_(properties_only)')?['value']",
                "runAfter": {
                    "Get_files_(properties_only)": [
                        "SUCCEEDED"
                    ]
                },
                "type": "foreach"
            },
            "Get_files_(properties_only)": {
                "inputs": {
                    "host": {
                        "connection": {
                            "referenceName": "sharepointonline"
                        }
                    },
                    "method": "get",
                    "path": "/datasets/@{encodeURIComponent(encodeURIComponent('*********'))}/tables/@{encodeURIComponent(encodeURIComponent('ea140ea6'))}/getfileitems",
                    "queries": {
                        "viewScopeOption": "RecursiveAll"
                    }
                },
                "runAfter": {},
                "type": "ApiConnection"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "triggers": {
            "When_a_HTTP_request_is_received": {
                "kind": "Http",
                "runtimeConfiguration": {
                    "concurrency": {
                        "runs": 1
                    }
                },
                "type": "Request"
            }
        }
    },
    "kind": "Stateful"
}

Output:-

enter image description here

enter image description here