user1063287
user1063287

Reputation: 10879

How to get File Upload field properties from Microsoft Form in Power Automate?

Desired Behaviour

I am trying to get the file properties of a file uploaded via a Microsoft Form in Power Automate.

Research

I've tried numerous variations of suggestions from sources such as:

I am a fairly experienced developer and I am familiar with variable types (String, Array, Object etc) and how to reference items in an object with dot or bracket notation and accessing array items by index.

I'm also familiar with:

JSON.parse()

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.

JSON.stringify()

The JSON.stringify() method converts a JavaScript object or value to a JSON string

And have read about Power Automate's Parse JSON action

To reference or access properties in JavaScript Object Notation (JSON) content, you can create user-friendly fields or tokens for those properties by using the Parse JSON action. That way, you can select those properties from the dynamic content list when you specify inputs for your logic app. For this action, you can either provide a JSON schema or generate a JSON schema from your sample JSON content or payload

But I am still having a lot of difficulty getting the values I need.

What I've Tried

01) Use Parse JSON to access properties of the Reponse body:

enter image description here

02) Run the flow and look at the Raw Outputs of Parse JSON:

{
    "body": {
        "responder": "[email protected]",
        "submitDate": "7/5/2021 7:03:26 AM",
        "letters-and-numbers-here-1": "some text here",
        "letters-and-numbers-here-2": "[{\"name\":\"File 01 Name.docx\",\"link\":\"https://tenant.sharepoint.com/sites/MySiteName/_layouts/15/Doc.aspx?sourcedoc=%7Bletters-and-numbers%7D&file=File%2001%20Name%20_Uploader%20Name.docx&action=default&mobileredirect=true\",\"id\":\"id-is-here\",\"type\":null,\"size\":20411,\"referenceId\":\"reference-id-is-here\",\"driveId\":\"drive-id-is-here\",\"status\":1,\"uploadSessionUrl\":null}]",
        "letters-and-numbers-here-3": "[{\"name\":\"File 02 Name.docx\",\"link\":\"https://tenant.sharepoint.com/sites/MySiteName/_layouts/15/Doc.aspx?sourcedoc=%7Bletters-and-numbers%7D&file=File%2002%20Name%20_Uploader%20Name.docx&action=default&mobileredirect=true\",\"id\":\"id-is-here\",\"type\":null,\"size\":20411,\"referenceId\":\"reference-id-is-here\",\"driveId\":\"drive-id-is-here\",\"status\":1,\"uploadSessionUrl\":null}]",
        "letters-and-numbers-here-4": "some other text here"
    }
}

03. Try and get the name value of the first File Upload field.

I had assumed that the first Parse JSON would have recursively set all values as JSON Objects, however it looks like the value of the File Upload field is still a string. So I figure I have to do another Parse JSON action on the File Upload field.

enter image description here

Content:

body('Parse_JSON')?['letters-and-numbers-here-2']

Sample JSON Payload:

{
    "letters-and-numbers-here-2": "[{\"name\":\"File 01 Name.docx\",\"link\":\"https://tenant.sharepoint.com/sites/MySiteName/_layouts/15/Doc.aspx?sourcedoc=%7Bletters-and-numbers%7D&file=File%2001%20Name%20_Uploader%20Name.docx&action=default&mobileredirect=true\",\"id\":\"id-is-here\",\"type\":null,\"size\":20411,\"referenceId\":\"reference-id-is-here\",\"driveId\":\"drive-id-is-here\",\"status\":1,\"uploadSessionUrl\":null}]"
}

Which produces the Error:

[
  {
    "message": "Invalid type. Expected Object but got Array.",
    "lineNumber": 0,
    "linePosition": 0,
    "path": "",
    "schemaId": "#",
    "errorType": "type",
    "childErrors": []
  }
]

So I tried to target the first Object within the File Upload field Array, and I try the following values as the Content of Parse JSON 2:

body('Parse_JSON')?['letters-and-numbers-here-2']?[0]

body('Parse_JSON')['letters-and-numbers-here-2'][0] 

Both produce the error:

Unable to process template language expressions in action 'Parse_JSON_2' inputs at line '1' and column '9206': 'The template language expression 'body('Parse_JSON')['letters-and-numbers-here-2'][0]' cannot be evaluated because property '0' cannot be selected. Property selection is not supported on values of type 'String'. Please see https://aka.ms/logicexpressions for usage details.'

Question

How do I access the File Upload field properties?

For reference, the Microsoft Form has 4 fields of type:

The connectors used are:

The body returned by Get response details, in Raw outputs, is:

"body": {
    "responder": "[email protected]",
    "submitDate": "7/5/2021 3:17:56 AM",
    "lots-of-letters-and-numbers-1":  "text string here",
    "lots-of-letters-and-numbers-2":  [{.....}],
    "lots-of-letters-and-numbers-3":  [{.....}],
    "lots-of-letters-and-numbers-4":  "text string here"
}

Upvotes: 1

Views: 4065

Answers (1)

user1063287
user1063287

Reputation: 10879

I tried a different approach, without the Parse JSON action, and could access the file name:

enter image description here

The expression values were:

`Compose` > `Inputs`:  json(body('Get_response_details')?['lots-of-letters-and-numbers-2'])
`Initialize Variable` > `Value`:  outputs('Compose')[0]['name']

Or, even simpler, just using Compose:

enter image description here

Or Initialize variable:

enter image description here

Using this expression with json() and body():

json(body('Get_response_details')?['lots-of-letters-and-numbers-2'])[0]['name']

I'm not sure if this is the best approach or if it comes with any 'gotchas'.

Upvotes: 1

Related Questions