The Common Data Service (CDS) has the possibility to add attachments to a record of a entity when using an canvas app. You could have the use case that you want to copy such attachments to SharePoint Online too. This blog post shows you how to do this.
Let me explain the context first. There are 2 custom entities:
- Case
- Document
The entity ‘Document’ contains the attachments. It also has a field ‘Case ID’ containing the unique number of a case. When the status of a case changes to “Closed”, the attachments need to be copied to a certain document library. The entity “Document” contains extra fields for metadata too. SharePoint Online contains similar columns.
Below, the actions used are given and described. At the bottom of this blog post, 4 images are shown to support the text below.
- Action “When a record is updated” (Connector “Common Data Service”).
- Entity “Cases”.
- I did not yet add any attribute filters for the status change to “Closed”.
- Action “Condition”
- Status is equal to closed
- If no > Terminate
- Within “yes” branch of action #2 > Action “List records” (Common Data Service).
- Entity “Documents”.
- I use a filter query to filter the documents belonging to that case using the field “Case ID”.
- Image B.
- Within action #2 > Action “Apply to Each” #1
- Using the output of action #3.
- Within action #4 > Action “List records” (Connector “Common Data Service)”.
- Entity “Notes”. I found out that attachments are placed in the default entity “Notes”. The magic happens in the filter query:
- _objectid_value eq @{items(‘Apply_to_each’)?[‘ItemInternalId’]}
- With “@{items(‘Apply_to_each’)?[‘ItemInternalId’]}” being a dynamic expression.
- Image C.
- _objectid_value eq @{items(‘Apply_to_each’)?[‘ItemInternalId’]}
- Entity “Notes”. I found out that attachments are placed in the default entity “Notes”. The magic happens in the filter query:
- Within Action #4 > Action “Apply to Each” #2
- Using the output of action #5.
- Within Action #6 > Action “Condition”
- @items(‘Apply_to_each’)?[‘documentbody’] is not equal to null
- Both the first path and last (null) are dynamic expressions.
- When an attachment is deleted via a canvas app, the field “documentbody” is indeed emptied, but the record will remain. I therefor had to add this condition.
- @items(‘Apply_to_each’)?[‘documentbody’] is not equal to null
- Within “yes” branch of action #7 > Action “Create file” (Connector “SharePoint”)
- File Name > @{items(‘Apply_to_each’)?[‘filename’]}
- Using the “apply to each” of action #6.
- File Content > @{items(‘Apply_to_each’)?[‘documentbody’]}
- Using the “apply to each” of action #6.
- File Name > @{items(‘Apply_to_each’)?[‘filename’]}
- Within “yes” branch of action #7 > Action “Update file properties” (Connector “SharePoint”)
- Properties of the document (entity “Document”) are used.
- Image D.
The above mechanism works for Word and PDF documents. It does not work for TXT documents.
For my situation, the attachments in CDS need to be moved instead of copied. At this moment, I am not sure if directly deleting a record in the entity “Documents” is allowed.



