Attachments/Documents of Dynamics 365 CRM records can be stored in SharePoint sites on the basis of hierarchy. To achieve this, follow the steps given below:
Go to Advanced Settings --> Settings --> Document Management Settings.
Select Entities --> Enter the URL of respective SharePoint site.
Enable the Checkbox ‘Based on Entity’ --> Select either of the entities, Account or Contact --> Click on Next.
Click on Finish.
When hierarchy is enabled, the path structure will look as shown below.
Next, go to Opportunity --> Select an opportunity --> Click on Attach2Dynamics button --> Drag and Drop required file.
Next go to SharePoint site. Here, the respective file will be stored in the following manner: Account > Account Name_guid > Opportunity > Opportunity Name_guid.