Graph Pagination in Logic Apps

ぐ巨炮叔叔 提交于 2020-06-17 09:56:05

问题


I'm trying to fetch all users from a specific group via an HTTP connector, a registered app, and Microsoft Graph.

The registered app has Directory.Read.All permissions.

My idea is that I'm calling the nextLink as long as it's there while appending all of the fetched users' userPrincipalName to an array eventually filling the array with all users of the group.

My Logic App looks like this:

Unfortunately, I'm just 1 reputation short of posting images, please forgive. The 3 links should provide an overview of the structure of my app.

First, nextLink is initialized to the first Graph API endpoint. This variable is set to the current nextLink through each iteration of the until loop.

Second, For the purpose of this exercise, I only get the top 5. I know there are only 9 users:

Lastly, I call the union method on the "users" array that I initialized earlier and the "value" array from the HTTP get method, to get one single array consisting of all users:


The issue is that the HTTP action always returns the same top 5 users. I've checked that the nextLink provided in the first HTTP GET call to Graph, is correct by copying it from the Runs history and pasting it into Microsoft Graph Explorer and there the next 4 users are correctly returned.

I also made sure that, for each iteration in the until loop, I call the Graph API with the nextLink from the previous iteration as expected.

The nextLink returned inside of the Logic App is exactly the same when I test it in Graph Explorer, but the same nextLink returns 2 different results when called from Graph Explorer and inside my Logic App.

Why is the result always the same top 5 users and not the next 4 users as expected?


回答1:


This issue solved by OP self, this issue is due to queries in request URL , copy OP's comment as an answer :

After fiddling a bit more around with what each of you providing I found a solution. It seems that when the query arguments are passed to the HTTP GET outside of the endpoint itself (meaning in the "queries" field inside of the block) it seems to keep overriding the nextLink. When writing the endpoint URL out entirely with the odata parameters, it works as intended.




回答2:


If not sure about the reason why you will get this issue, but based on your requirement, I did a sample below:

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "GetGroupUrl",
                            "type": "string",
                            "value": "https://graph.microsoft.com/v1.0/groups/<your group id>/members?$select=userPrincipalName&$top=5"
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Initialize_variable_2": {
                "inputs": {
                    "variables": [
                        {
                            "name": "users",
                            "type": "array"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Until": {
                "actions": {
                    "Compose": {
                        "inputs": "@union(variables('users'),body('HTTP')['value'])",
                        "runAfter": {
                            "HTTP": [
                                "Succeeded"
                            ]
                        },
                        "type": "Compose"
                    },
                    "HTTP": {
                        "inputs": {
                            "authentication": {
                                "audience": "https://graph.microsoft.com",
                                "clientId": "<app id>",
                                "secret": "<app secret>",
                                "tenant": "<your secret>",
                                "type": "ActiveDirectoryOAuth"
                            },
                            "method": "GET",
                            "uri": "@variables('GetGroupUrl')"
                        },
                        "runAfter": {},
                        "type": "Http"
                    },
                    "Set_variable": {
                        "inputs": {
                            "name": "GetGroupUrl",
                            "value": "@{if(equals(body('HTTP')?['@odata.nextLink'], null),null,body('HTTP')['@odata.nextLink'])}"
                        },
                        "runAfter": {
                            "Compose": [
                                "Succeeded"
                            ]
                        },
                        "type": "SetVariable"
                    }
                },
                "expression": "@equals(variables('GetGroupUrl'), '')",
                "limit": {
                    "count": 60,
                    "timeout": "PT1H"
                },
                "runAfter": {
                    "Initialize_variable_2": [
                        "Succeeded"
                    ]
                },
                "type": "Until"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {
            "manual": {
                "inputs": {
                    "method": "GET",
                    "schema": {
                        "properties": {
                            "text": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    }
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

You can just replace the params with your own and paste it into your logic app code view and test it . It works for me, as you can see , each request results are different :

Hope it helps .



来源:https://stackoverflow.com/questions/58838128/graph-pagination-in-logic-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!