Power Automate – Move attachments from Dataverse to One Drive

In my previous blog, I shared how we can transfer SharePoint list data & its attachments to Dataverse. In today’s blog, I will show how we can move the attachments from Dataverse to One drive using Power Automate. Again, this is one of the ways of data migration & related entities could be any.

When to use – It can be used for archiving your data which is old in Dataverse. It would also help you keep your Dataverse capacity under the limit.

For testing purposes, I have used the Manual trigger. It could be a scheduled trigger (like run every Saturday for data older than 2-3 years maybe or trigger via Power Apps)

Let’s briefly discuss the strategy I used, again one of the ways. I wanted to get all the contacts & their attachments stored in dataverse. Once I have the attachment, download it & move it to one drive. There are a few points I noticed while testing, I will share them later in the blog.

So let us get the list of all contacts. Make sure you use ‘Expand Query’ for retrieving related entities. I used Annotation for attachments. Also, mention the column names you want to retrieve from the related entity.

Contact_Annotation($select=annotationid,filename, documentbody)

Now I have used the Parse JSON action to get the attachment data. I generated JSON from the sample and used the output from List rows to get the attachment. I am sharing the sample JSON that was generated.

 {
    "type": "object",
    "properties": {
        "@@odata.context": {
            "type": "string"
        },
        "value": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    contactid": {
                        "type": "string"
                    },
                    "Contact_Annotation@odata.type": {
                        "type": "string"
                    },
                    "Contact_Annotation": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "annotationid": {
                                    "type": "string"
                                },
                                "filename": {
                                    "type": "string"
                                },
                                "documentbody": {
                                    "type": "string"
                                }
                            },
                            "required": [
                                "annotationid",
                                "filename",
                                "documentbody"
                            ]
                        }
                    }
                },
                "required": [
                    "contactid",
                    "Contact_Annotation@odata.type",
                    "Contact_Annotation"
                ]
            }
        }
    }
}

Since there could be multiple attachments attached to contact, as soon as you use the id of the attachment (annotationid), apply to each loop will apply automatically. I love this feature of Power Automate where it works smartly and applies the loop mechanism.

Once we have the associated attachment record, the rest is easy. We need to download the attachments. Notice that, there is no place or location for the same, so let’s create the file on one drive.

Note – I have typed the column name here instead of using it from dynamics values.

The file was giving an error when opened, so it is better to convert the data. And below is the formula for that. Test this flow and you will see the attachments at the location path.

base64ToBinary(outputs('Download_a_file_or_an_image')?['body'])

Note – Choose your ‘Folder Path’ I have saved the attachments at the root folder directly.

Read the official documentation here

  • While testing this flow, I noticed that I did not filter contact records where there were no attachments, you would need to do that if you just focussing on attachments.
  • Also, if there were the same attachments to different contact records, it was overwriting & saving the latest ones. So, a folder structure would be good.

In my next blog, I will share another way to get data for your related entities.

Hope this helps!

Keep reading, keep enjoying, keep exploring!

One thought on “Power Automate – Move attachments from Dataverse to One Drive

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s