Freeing storage for Dataverse for File Storage

 I’m Running Out of Storage Space! Now What?

These are approaches to decrease the amount of storage space used by removing or deleting various kinds of data customer engagement apps (Dynamics 365 Sales, Dynamics 365 Customer Service, Dynamics 365 Field Service, Dynamics 365 Marketing, and Dynamics 365 Project Service Automation) But there is no "undo" to restore your data once it has been deleted. So, we always need to be careful while deleting records. 

Most of the storage is utilized by email attachment's OR notes attachments on dynamics 365.  And we can not always delete the attachment's, as it is useful information so we need to find out another way where we store our attachments also we can use them via dynamics. SharePoint has a distinct advantage over Dynamics 365. With SharePoint, you not only get more storage space, but if you ever need more, storage space is much cheaper to purchase. Using Power automateany files attached to emails or notes are automatically moved to a SharePoint document library you specify in the configuration.

Using Power Automate, we can create Scheduled flow. So, here in this blog we will demonstrate you how we will move all the attachments from dynamics to  SharePoint and also link them with dynamics entity record, so we can refer them via dynamics only. 

Step 1:

Make a Power Automate stream as Scheduled stream, triggers each mid-night OR at non-working hours for your time region. 

First step, will take all the emails list having attachments. For that we can not directly get it via "Get Records" step so we need to write a fetch xml which will add link entity for attachment and it results all email records having attachments. 

Fetch XML:
<fetch distinct="true">
  <entity name="email">
    <attribute name="subject" />
    <attribute name="regardingobjectid" />
    <attribute name="from" />
    <attribute name="to" />
    <attribute name="prioritycode" />
    <attribute name="statuscode" />
    <attribute name="activityid" />
    <attribute name="attachmentopencount" />
    <order attribute="subject" descending="false" />
    <link-entity name="activitymimeattachment" from="objectid" to="activityid" link-type="inner" alias="ab" />
  </entity>
</fetch>

Step 2:
Next, will loop all the email records, and get list of all the attachments regarding that email. 


Step 3:
Now, we need to first configure the SharePoint with Dynamics. If I describe the steps in short then,
1. We create one Public/Private Site in SharePoint
2. We go to Settings -> Document Management -> SharePoint Configuration
3. Add the related site URL, follow the prescribed steps, select the entities for which we need to configure the SharePoint Documents. 
Here, do not forgot to select the "Email" entity.
4. Once we configure the Dynamics with SharePoint site, it will automatically create the Entity folders to SharePoint Site. We need to go to Site Contents, where you will find all the folders created for all the entities. 
5. Now, we need to create Document Location for this folders in Dynamics. 
So, for now we will Go to Settings -> Document Management -> Document Locations
Create new Location for Email Folder. 
Step 4:
Now, we are ready with Dynamics & SharePoint Integration. Both side we have folders to store our attachments. 
Next, we will continue our MS flow, will loop all the attachments list for a single email & Create the file in SharePoint.

Add Body of email attachment As File Context in "Create File in SharePoint" step.
Here, add the folder path as '/email/@{items('Apply_to_each')?['activityid']}' . Which will create a folder as Email/Email Id. And then store the file in newly created email related folder. 

Once we create file in SharePoint, we will delete the attachment from the Dynamics. Which will fulfil our main goal to Free up the space.



Step 5:
Now we next we need to create a document location at Dynamics side, which will link our Email Record & related SharePoint folder. For that, first we need to get the "Parent Document Location" - that we created in Step3. 


Once we get the Parent Document Location, we will create new document location for the newly create folder related to Email in SharePoint. 

To create new document location we need to add below details in steps,
  • Table Name: Document Locations
  • Name: Email Message [@{items('Apply_to_each')?['activityid']}]
  • Service Type: SharePoint
  • Parent Site or Location: [sharepointdocumentlocations(@{items('Apply_to_each_3')?['sharepointdocumentlocationid']})]
       As shown in below image, here if we directly put the parent document location id from the above step, then it will not work 
       So we need to add it like sharepointdocumentlocations(ID)
  • Relative URL : Here, just add reference of Email - @{items('Apply_to_each')?['activityid']} 
  • Regarding (Email Message): here also we need to add like emails(ID) - [emails(@{items('Apply_to_each')?['activityid']})]


Now, please have a look at below image as whole flow.
Same way we can implement for the Notes records also. For now we implemented for Email messages & its attachments. Same way we can create scheduled flow for the Notes & its attachments. 
 




Congratulations!
You have effectively set up a automate Microsoft Teams notification utilizing Power Automate streams.  Once you Run this flow, you can see the direct effect on storage chart of Dynamics storage Usage. And if you did not see the direct effect, then please wait for 24hr sometimes charts get reflected after some time. So, no need to worry about storage space now. We are almost there to free up the most occupied space in dynamics via very effective way. 

There are few more things which we can implement to free up space but all are like deleting the records which I would not suggest every time as emails, attachments, notes are the important things on our dynamics. Like, Evaluate and delete suspended workflows , Delete audit logs.


References :
https://docs.microsoft.com/en-us/power-platform/admin/free-storage-space



Comments