Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Microsoft Power Automate/Sharepoint

Trinity99

New Coder
Hi,

I was trying to make a MS Power Automate for uploading attachments from a shared outlook inbox to Sharepoint.

The automation worked twice but the attachments wouldn't open- it gave this error when trying to open the PDF:

Error 1 opening file.

Could not open '21-608- RFI 078 (1).pdf' because it is either not a supported file type or the file has been corrupted. For example, it was sent as an email attachment and wasn't correctly decoded.

Then the automation started failing. Below is the code. Any ideas are much appreciated!

JSON:
{
  "type": "OpenApiConnection",
  "inputs": {
    "parameters": {
      "mailboxAddress": "[email protected]"
    },
    "host": {
      "apiId": "/providers/Microsoft.PowerApps/apis/shared_office365",
      "connection": "shared_office365",
      "operationId": "SharedMailboxOnNewEmailV2"
    }
  },
  "recurrence": {
    "frequency": "Minute",
    "interval": 5
  }
}

{
  "type": "Foreach",
  "foreach": "@triggerOutputs()?['body/value']",
  "actions": {
    "For_each_2": {
      "type": "Foreach",
      "foreach": "@triggerOutputs()?['body/value']",
      "actions": {
        "For_each_1": {
          "type": "Foreach",
          "foreach": "@item()?['attachments']",
          "actions": {
            "Create_file": {
              "type": "OpenApiConnection",
              "inputs": {
                "parameters": {
                  "dataset": "https://harmonconst.sharepoint.com/sites/PM",
                  "folderPath": "/Shared Documents/005 HCI Accounting Attachments",
                  "name": "@item()?['name']",
                  "body": "@item()?['contentBytes']"
                },
                "host": {
                  "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline",
                  "connection": "shared_sharepointonline-2",
                  "operationId": "CreateFile"
                }
              }
            }
          }
        }
      }
    }
  },
  "runAfter": {}
}

{
  "type": "Foreach",
  "foreach": "@triggerOutputs()?['body/value']",
  "actions": {
    "For_each_1": {
      "type": "Foreach",
      "foreach": "@item()?['attachments']",
      "actions": {
        "Create_file": {
          "type": "OpenApiConnection",
          "inputs": {
            "parameters": {
              "dataset": "https://harmonconst.sharepoint.com/sites/PM",
              "folderPath": "/Shared Documents/005 HCI Accounting Attachments",
              "name": "@item()?['name']",
              "body": "@item()?['contentBytes']"
            },
            "host": {
              "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline",
              "connection": "shared_sharepointonline-2",
              "operationId": "CreateFile"
            }
          }
        }
      }
    }
  }
}

{
  "type": "Foreach",
  "foreach": "@item()?['attachments']",
  "actions": {
    "Create_file": {
      "type": "OpenApiConnection",
      "inputs": {
        "parameters": {
          "dataset": "https://harmonconst.sharepoint.com/sites/PM",
          "folderPath": "/Shared Documents/005 HCI Accounting Attachments",
          "name": "@item()?['name']",
          "body": "@item()?['contentBytes']"
        },
        "host": {
          "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline",
          "connection": "shared_sharepointonline-2",
          "operationId": "CreateFile"
        }
      }
    }
  }
}

{
  "type": "OpenApiConnection",
  "inputs": {
    "parameters": {
      "dataset": "https://harmonconst.sharepoint.com/sites/PM",
      "folderPath": "/Shared Documents/005 HCI Accounting Attachments",
      "name": "@item()?['name']",
      "body": "@item()?['contentBytes']"
    },
    "host": {
      "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline",
      "connection": "shared_sharepointonline-2",
      "operationId": "CreateFile"
    }
  }
}
 

Attachments

  • MS Power Automate Question Snippetjpg.jpg
    MS Power Automate Question Snippetjpg.jpg
    129.5 KB · Views: 2
Last edited by a moderator:
Hi,

I was trying to make a MS Power Automate for uploading attachments from a shared outlook inbox to Sharepoint.

The automation worked twice but the attachments wouldn't open- it gave this error when trying to open the PDF:

Error 1 opening file.

Could not open '21-608- RFI 078 (1).pdf' because it is either not a supported file type or the file has been corrupted. For example, it was sent as an email attachment and wasn't correctly decoded.

Then the automation started failing. Below is the code. Any ideas are much appreciated!

JSON:
{
  "type": "OpenApiConnection",
  "inputs": {
    "parameters": {
      "mailboxAddress": "[email protected]"
    },
    "host": {
      "apiId": "/providers/Microsoft.PowerApps/apis/shared_office365",
      "connection": "shared_office365",
      "operationId": "SharedMailboxOnNewEmailV2"
    }
  },
  "recurrence": {
    "frequency": "Minute",
    "interval": 5
  }
}

{
  "type": "Foreach",
  "foreach": "@triggerOutputs()?['body/value']",
  "actions": {
    "For_each_2": {
      "type": "Foreach",
      "foreach": "@triggerOutputs()?['body/value']",
      "actions": {
        "For_each_1": {
          "type": "Foreach",
          "foreach": "@item()?['attachments']",
          "actions": {
            "Create_file": {
              "type": "OpenApiConnection",
              "inputs": {
                "parameters": {
                  "dataset": "https://harmonconst.sharepoint.com/sites/PM",
                  "folderPath": "/Shared Documents/005 HCI Accounting Attachments",
                  "name": "@item()?['name']",
                  "body": "@item()?['contentBytes']"
                },
                "host": {
                  "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline",
                  "connection": "shared_sharepointonline-2",
                  "operationId": "CreateFile"
                }
              }
            }
          }
        }
      }
    }
  },
  "runAfter": {}
}

{
  "type": "Foreach",
  "foreach": "@triggerOutputs()?['body/value']",
  "actions": {
    "For_each_1": {
      "type": "Foreach",
      "foreach": "@item()?['attachments']",
      "actions": {
        "Create_file": {
          "type": "OpenApiConnection",
          "inputs": {
            "parameters": {
              "dataset": "https://harmonconst.sharepoint.com/sites/PM",
              "folderPath": "/Shared Documents/005 HCI Accounting Attachments",
              "name": "@item()?['name']",
              "body": "@item()?['contentBytes']"
            },
            "host": {
              "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline",
              "connection": "shared_sharepointonline-2",
              "operationId": "CreateFile"
            }
          }
        }
      }
    }
  }
}

{
  "type": "Foreach",
  "foreach": "@item()?['attachments']",
  "actions": {
    "Create_file": {
      "type": "OpenApiConnection",
      "inputs": {
        "parameters": {
          "dataset": "https://harmonconst.sharepoint.com/sites/PM",
          "folderPath": "/Shared Documents/005 HCI Accounting Attachments",
          "name": "@item()?['name']",
          "body": "@item()?['contentBytes']"
        },
        "host": {
          "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline",
          "connection": "shared_sharepointonline-2",
          "operationId": "CreateFile"
        }
      }
    }
  }
}

{
  "type": "OpenApiConnection",
  "inputs": {
    "parameters": {
      "dataset": "https://harmonconst.sharepoint.com/sites/PM",
      "folderPath": "/Shared Documents/005 HCI Accounting Attachments",
      "name": "@item()?['name']",
      "body": "@item()?['contentBytes']"
    },
    "host": {
      "apiId": "/providers/Microsoft.PowerApps/apis/shared_sharepointonline",
      "connection": "shared_sharepointonline-2",
      "operationId": "CreateFile"
    }
  }
}
  1. Encoding Issues: Ensure that the encoding of the attachment is correctly handled. The `contentByte`s property may need decoding before being used to create the file. You can use the base64ToString() function to decode the content bytes.

    JSON:
    "body": "@base64ToString(item()?['contentBytes'])"
  2. File Type Check: Verify that the attachments being processed are indeed PDF files. You can add a condition before creating the file to check the file type.

    JSON:
    "condition": {
    "expression": "@equals(item()?['contentType'], 'application/pdf')",
    "ifTrue": {
    // Your existing Create_file action
    },
    "ifFalse": {
    // Log or handle other file types
    }
    }
  3. Log Information: Add logging actions at various steps to gather information about the attachments, their names, and content before and after processing. This can help in debugging and identifying any issues.
  4. PDF Version Compatibility: Check if the PDF files being uploaded are using a version of the PDF standard that is supported by SharePoint.
  5. SharePoint Permissions: Ensure that the account used in the SharePoint connection has the necessary permissions to create files in the specified SharePoint folder.
  6. Retry Mechanism: Implement a retry mechanism in case of intermittent issues. Sometimes, transient errors can occur.
  7. Update SharePoint Connection: Ensure that your SharePoint connection is up to date. Sometimes, issues can arise if the connection is outdated.
Make these adjustments in your Power Automate flow and test again to see if the issue persists. Additionally, review the SharePoint logs or error details for more specific information on why the files may not be opening correctly.
 

New Threads

Buy us a coffee!

Back
Top Bottom