Dynamically create SharePoint Document Location for D365 CRM entities using Power Automate Flow 

You can integrate SharePoint with your Dynamics 365 CRM Environment and enable document locations for each of your Dynamics 365 CRM entities.  
 
More on enabling SharePoint can be found in the linked blog by Microsoft MVP Priyesh Wagh – https://d365demystified.com/2021/11/30/enable-sharepoint-online-integration-dynamics-365-power-platform-admin-center/ 

Challenge – Now one challenge while using SharePoint with Dynamics 365 CRM Entities is a SharePoint Document location for any entities is created only when you manually open the Documents tab from the entity form as shown below. 

Now consider a scenario where you want a document structure created when an entity record is created or you want to store files in the entities document location without having to manually go and open the document tabs. 

In this blog I will show you how you can dynamically create document locations for D365 entities 

  1. Adding folders to these document locations. 
  2. Adding files to these document locations. 
  3. How to get existing document locations and add files and folders in these document locations. 

Concept of document locations – Lets first understand the concept of document locations in Dynamics 365 CRM. 

Level 1: At the first level there is a document location record named “Default Site” this  

Level 1 Site Document Location:  

At the first level there is a document location record named “Default Site” this default site holds the details of the SharePoint site. 

Level 2 Entity Document Location:  

At the second level there is a document location record named “Documents on Default Site 1” this is the Parent document location record for entities, this has the entities logical name in the Relative URL & the Default site from Level 1 in the Parent Site or Location. 

(Copy the GUID from the URL of this record we will be using this in our flow Later

Level 3 Entity Records Document Location: 

At the third level there is a document location record named “Documents on Default site 1” this is the document location record which is related to the entities records.  

  1. This has the Level 2 document location record in the Parent Site or Location. 
  2. The Relative URL contains the name of the folder in the entities SharePoint Library the by default the  name is {RecordName}_{RecordGUID} you can change this name if you want  
  3. The entity record for which the document location has being created is set as the regarding of this document location record. 

(Note: Once you enable SharePoint for an entity a Document Library is created within the SharePoint site and folders are created inside this document library for that entities records) 

Development Approach: We need Level 1 & Level 2 document library record created 

  1. Level 1 document library record is created when you enable SharePoint Integration for the entity. 
  2. For creating  Level 2 document library record you will have to manually open the document tab on an entity record after you have enabled the SharePoint for that entity. 
  3. Once done open advance find and use the below query. 

Open this record and copy the GUID from the URL as shown in the Level 2 Entity Document Location step. 

Now that we have Level 1 & Level 2 records we can create our Level 3 record which will be the SharePoint document location record for that entity record. 

Power Automate flow:  

Step 1: Create a “Compose” action and add the GUID of the Level 2 document library record which we have copied. (You will have to change this if you move the flow to different environment hence I am making you use a compose action so that updating becomes ease after migrating the flow) 

Step 2: Use this GUID to get the Document location record using “Get Row By ID” action. 

Step 3: Check if a Document Location record for already exists for that entity record using the below fetch XML Query. 

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"> 

  <entity name="sharepointdocumentlocation"> 

    <attribute name="name" /> 

    <attribute name="regardingobjectid" /> 

    <attribute name="parentsiteorlocation" /> 

    <attribute name="relativeurl" /> 

    <attribute name="absoluteurl" /> 

    <order attribute="name" descending="false" /> 

    <filter type="and"> 

      <condition attribute="locationtype" operator="eq" value="0" /> 

      <condition attribute="servicetype" operator="eq" value="0" /> 

    </filter> 

    <link-entity name="msdyn_expense" from="msdyn_expenseid" to="regardingobjectid" link-type="inner" alias="aj"> 

      <filter type="and"> 

        <condition attribute="msdyn_expenseid" operator="eq" uitype="msdyn_expense" value="GUID OF YOUR ENTITY RECORD" /> 

      </filter> 

    </link-entity> 

  </entity> 

</fetch> 

Step 4: Add an empty check. 

If the document location record doesn’t exists. 

  1. Create a relative URL I have given it {EntityName}_{EntityGUID}, you can give this name as per your preference. 
  2. Create SharePoint folder select your SharePoint Library, select the entities document library and add the relative URL which we created. 
  3. Now you can also create folders and sub folders by adding {/Name of the folder /Name of Sub Folder} 
  1. Create a document location record using the “Add Row” action –> in this use the OData id from Step 2 –> and Relative URL from Point 1. 
  1. Use relate rows to relate the newly created document library record and your entity record. 

You can create files in this document location using create file action. 

Full Path – I you will get from Create new Folder action which we used above. 

Rest all the variables depends up on from where you are taking the file.  

If the document location record exists. 

If the document location record exists you get the Relative URL combining the relative URL with the entities location name you get SharePoint Document Locations folders path for that entity record, you can then use this path to add files or create folders and sub folders. 

Hope this helps 😊! 

Leave a comment