How-to copy attachments from the CDS to SharePoint Online

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.

  1. Action “When a record is updated” (Connector “Common Data Service”).
    1. Entity “Cases”.
    2. I did not yet add any attribute filters for the status change to “Closed”.
  2. Action “Condition”
    1. Status is equal to closed
    2. If no > Terminate
  3. Within “yes” branch of action #2 > Action “List records” (Common Data Service).
    1. Entity “Documents”.
    2. I use a filter query to filter the documents belonging to that case using the field “Case ID”.
    3. Image B.
  4. Within action #2 > Action “Apply to Each” #1
    1. Using the output of action #3.
  5. Within action #4 > Action “List records” (Connector “Common Data Service)”.
    1. Entity “Notes”. I found out that attachments are placed in the default entity “Notes”. The magic happens in the filter query:
      1. _objectid_value eq @{items(‘Apply_to_each’)?[‘ItemInternalId’]}
        1. With “@{items(‘Apply_to_each’)?[‘ItemInternalId’]}” being a dynamic expression.
      2. Image C.
  6. Within Action #4 > Action “Apply to Each” #2
    1. Using the output of action #5.
  7. Within Action #6 > Action “Condition”
    1. @items(‘Apply_to_each’)?[‘documentbody’] is not equal to null
      1. Both the first path and last (null) are dynamic expressions.
    2. 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.
  8. Within “yes” branch of action #7 > Action “Create file” (Connector “SharePoint”)
    1. File Name > @{items(‘Apply_to_each’)?[‘filename’]}
      1. Using the “apply to each” of action #6.
    1. File Content > @{items(‘Apply_to_each’)?[‘documentbody’]}
      1. Using the “apply to each” of action #6.
  9. Within “yes” branch of action #7 > Action “Update file properties” (Connector “SharePoint”)
    1. Properties of the document (entity “Document”) are used.
    2. 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.

Image A – The complete Flow
Image B
Image C
Image D

Leave a Reply

Your email address will not be published. Required fields are marked *