Showing results for 
Search instead for 
Did you mean: 

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. When I do know, I will add a comment to this blog post.

Copy attachments from CDS to SharePoint_A.pngImage A - The complete FlowCopy attachments from CDS to SharePoint_B.pngImage BCopy attachments from CDS to SharePoint_C.pngImage CCopy attachments from CDS to SharePoint_D.pngImage D

Thanks for reading!


All of my attachments are just coming out encoded how did you get around this problem?

Meet Our Blog Authors
  • Experienced Consultant with a demonstrated history of working in the information technology and services industry. Skilled in Office 365, Azure, SharePoint Online, PowerShell, Nintex, K2, SharePoint Designer workflow automation, PowerApps, Microsoft Flow, PowerShell, Active Directory, Operating Systems, Networking, and JavaScript. Strong consulting professional with a Bachelor of Engineering (B.E.) focused in Information Technology from Mumbai University.
  • Encodian Owner / Founder - Ex Microsoft Consulting Services - Architect / Developer - 20 years in SharePoint - PowerPlatform Fan
  • Cambridge UK Power Platform User Group Leader, Technical evangelist and speaker. Always says yes to coffee! #LetsGetCoffee
  • Passionate #Programmer #SharePoint #SPFx #Office365 #MSFlow | C-sharpCorner MVP | SharePoint StackOverflow, Github, PnP contributor
  • I am building business processes and applications that are easy for users' to stick to, so they can follow and understand them. In overall I transform processes to be more reliable and effortless. I am a proud co-organizer of SharePoint Saturday Warsaw and active community member, blogger and international speaker.