Pass trough all SharePoint items from the “Get Items” action in Azure Logic Apps

3 minute read

When constructing a Logic Apps that gets information from a SharePoint list you will use the SharePoint “Get Items” action. This action allows you to get items from a SharePoint list you connect the action to. You can then process the items by using for example an Azure Function. Constructing a flow this will give you the following Logic App.

image

By default Logic Apps will construct a foreach loop after the “Get Items” action that you cannot see in the designer view. By opening the code view you will see it (It will only appear if you added a value of the “Get Items” action to for example the input of the Azure Function).

{
    "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2015-08-01-preview/workflowdefinition.json#",
    "actions": {
        "Get_items": {
            "conditions": [],
            "inputs": {
                "host": {
                    "api": {
                        "runtimeUrl": "https://logic-apis-westeurope.azure-apim.net/apim/sharepointonline"
                    },
                    "connection": {
                        "name": "@parameters('$connections')['sharepointonline_1']['connectionId']"
                    }
                },
                "method": "get",
                "path": ""
            },
            "type": "apiconnection"
        },
        "SharePointProcessing": {
            "conditions": [
                {
                    "dependsOn": "Get_items"
                }
            ],
            "foreach": "@body('Get_items')['value']",
            "inputs": {
                "body": "@item()['Status']",
                "function": {
                    "id": ""
                }
            },
            "type": "Function"
        }
    },
    "contentVersion": "1.0.0.0",
    "outputs": {},
    "parameters": {
        "$connections": {
            "defaultValue": {},
            "type": "Object"
        }
    },
    "triggers": {
        "recurrence": {
            "recurrence": {
                "frequency": "Hour",
                "interval": 1
            },
            "type": "Recurrence"
        }
    }
}

This means that the Azure Function will be called for every item that is retrieved by the “Get Items” action meaning that if the action retrieves 100 items the function will be started 100 times.

In a Logic App a colleague (Andre de Ruiter) of mine was constructing he wanted to process all the items at ones in a Azure Function. By default this isn’t possible because the Logic Apps designer only gives you the option to add specific fields into the input value.

image

As you can see in the above screenshots the action retrieved all the fields available on the SharePoint item and does not give a option to add the complete body of the “Get Items” action.

If you want to send the complete body trough you will have to use de code view. The easiest way is to add one output from the “Get Items” activity into the input of your action and then click on “Code View”.

Within this view search for the Azure Function or the other action that is being called. You will notice that this action contains the foreach object.

"SharePointProcessing": {
    "conditions": [
        {
            "dependsOn": "Get_items"
        }
    ],
    "foreach": "@body('Get_items')['value']",
    "inputs": {
        "body": "@item()['Deleted']",
        "function": {
            "id": ""
        }
    },
    "type": "Function"
}

Remove the line with the foreach and in the “inputs” object change the body value to: “@body(‘Get_items’)”. Your action will know look as followed.

"SharePointProcessing": {
    "conditions": [
        {
            "dependsOn": "Get_items"
        }
    ],
    "inputs": {
        "body": "@body('Get_items')",
        "function": {
            "id": ""
        }
    },
    "type": "Function"
}

Open the “Designer” again and notice that the Action now contains the “body” element of the “Get Items” action.

image