Dynamics 365 (D365) offers integration that stores documents into SharePoint and displays them in D365. This integration has many benefits and offers indexing of documents which will not happen if the documents are saved inside Dataverse which is technical database of D365. Saving documents from D365 to email entity in Dynamics 365 Sales out-of-the-box needs user to save the document into computer and then upload to the email in D365. This blog describes solution that allows user directly select documents inside SharePoint and adds them as attachments of the email.

I was facing this challenge and searched solution how to implement it. Originally I was thinking out-of-the-box Document view and some way to select items and move them into email attachment box. Since I could not add grid with dynamic filter in a new email entity, which did not have any data I asked around if someone had better solution. For my luck Antti Jääskeläinen my colleague had done POC about this topic and offered his solution to be developed further.

Solution consists of custom page, which opens above email entity and gets documents folder for the parent case of the email.

First create a custom page that calls a Power Automate flow which gets the parent case (or document under a case) GUID and then searches the document location table in D365 to figure out which folder contents to display. Add the custom page into your D365 Model-driven app but not in the navigation.

Custom page app OnStart function getting documents into collection
Custom page app OnStart function getting documents into collection

Then you need to create the Power Automate flow getting the documents. The flow gets the entity and entity id and with that it gets the document location, parses the SharePoint folder url and gets all nested items from SharePoint. It parses the items (files and folders) into JSON which it gives back to Power App (custom page), current folder path, library id and siteUrl.

Getting SharePoint library items for case in D365
Getting SharePoint library items for case in D365

Then you need to add ribbon button Tuo palvelupyynnöltä which first saves the email entity if it is not saved. This is done because we cannot save attachments if there is no existing email entity. Then it calls the custom page.

function openCustomPageDialog(primaryControl, firstSelectedItemId, selectedEntityTypeName)
{
    var itemId = "";
    if (firstSelectedItemId == null || firstSelectedItemId == "")
    {
        primaryControl.data.save().then(function() { openSharePointDocumentsDialog(selectedEntityTypeName, primaryControl.data.entity.getId(), primaryControl); }, errorCallback);
    } else {
        itemId = firstSelectedItemId;
        openSharePointDocumentsDialog(selectedEntityTypeName, itemId, primaryControl);
    }
}
function errorCallback(data)
{
    alert("error");
}
function openSharePointDocumentsDialog(selectedEntityTypeName, itemId, primaryControl)
{
     // Centered Dialog
     var pageInput = {
        pageType: "custom",
        name: "dg_sharepointdocuments_84437",
        entityName: selectedEntityTypeName, 
        recordId: itemId 
    };
    var navigationOptions = {
        target: 2,
        position: 1,
	height: {
	    value: 80,
	    unit: "%"
	},
	width: {
	    value: 50,
	    unit: "%"
	},
	title: "Palvelupyynnön dokumentit"
    };
    Xrm.Navigation.navigateTo(pageInput, navigationOptions).then(
        function () {
            // Refresh the main form when the dialog is closed
            primaryControl.data.refresh();
        }
    ).catch (
        function (error) {
            // Handle error
        }
    );
}

Then you need to implement another Power Automate flow which gets the file data from SharePoint and adds the files into email attachments as activity

Power Automate flow that gets the file bytes to D365 email entity attachments
Power Automate flow that gets the file bytes to D365 email entity attachments

Implement the adding functionality into the custom page and call the flow when pressing + sign in the user interface. You should also implement navigating inside folder structure in SharePoint data.

Adding files from SharePoint to D365
Adding files from SharePoint to D365

Finally you have files in your email entity attachments (liitteet) the same way as you were using the out-of-the-box functionality to upload them from your local computer.

But when this was installed into test environment, the custom page dialog did not show any files or took 5 minutes to open. Please read the next post telling how to fix this issue.