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!

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.
  • I am a Microsoft Business Applications MVP and a Senior Manager at EY. I am a technology enthusiast and problem solver. I work/speak/blog/Vlog on Microsoft technology, including Office 365, Power Apps, Power Automate, SharePoint, and Teams Etc. I am helping global clients on Power Platform adoption and empowering them with Power Platform possibilities, capabilities, and easiness. I am a leader of the Houston Power Platform User Group and Power Automate community superuser. I love traveling , exploring new places, and meeting people from different cultures.
  • Blog site: MCT | SharePoint, Microsoft 365 and Power Platform Consultant | Contributor on SharePoint StackExchange, Techcommunity
  • Encodian Owner / Founder - Ex Microsoft Consulting Services - Architect / Developer - 20 years in SharePoint - PowerPlatform Fan
  • I am the Owner/Principal Architect at Don't Pa..Panic Consulting. I've been working in the information technology industry for over 30 years, and have played key roles in several enterprise SharePoint architectural design review, Intranet deployment, application development, and migration projects. I've been a Microsoft Most Valuable Professional (MVP) 12 consecutive years and am also a Microsoft Certified SharePoint Masters (MCSM) since 2013.
  • Big fan of Power Platform technologies and implemented many solutions.
  • Passionate #Programmer #SharePoint #SPFx #M365 #Power Platform| Microsoft MVP | SharePoint StackOverflow, Github, PnP contributor
  • Web site – Youtube channel -