SharePoint-and-D365-Document-Embedding

Microsoft Dynamics 365 uniquely delivers a modern approach to business applications helping you unify data and relationships, build intelligence into your decision making, and achieve better results. In this blog post Matthew Foy will discuss a way of embedding SharePoint Documents directly on a form in Dynamics 365.

The Situation

When Dynamics 365 and SharePoint integration is configured with server-side integration, documents can be stored in SharePoint and related to (and managed from) CRM records.

For example, let’s say your Organization tracks scanned purchase orders, or tracks C.V.s, or some other form of document related to specific records in your CRM. The Dynamics 365 and SharePoint server-side synchronization integration is a great way to leverage both platforms’ strength: SharePoint is great at managing documents, and Dynamics 365 is great at relating entities together.

The Problem

However, sometimes it is desirable to have an “at a glance” view of documents associated with a record. In order to view the documents in SharePoint that are related to a record, the user must navigate to a related entities area outside of the form. (Similar to Connection, or other related entities)

For most related entities, a sub-grid can be used in the form to display the relationships immediately on the form, but there is no simple workaround for the Documents.

The Solution

If you navigate to the Documents area on a record with Document Management enabled, a quick inspection of the DOM will reveal that it is simply an IFrame.

Reconstructing the IFrame source used to be more challenging in previous versions of Dynamics CRM, when using the List Component integration. But with Server-Side Synchronization in Dynamics 365, the solution is much simpler.

By using variables available through the JavaScript Xrm API, we can reconstruct the source URL of the IFrame for any record and populate our own IFrame in a Web Resource in the form proper:

<html><head>
<style type="text/css">
 body { padding: 5px; margin: 0px; }
 iframe { border:none; margin: 0px; padding: 0px; width: 100%; height: 100%; }
</style>
<script type="text/javascript">
 function checkForm() {
 if (parent.Xrm.Page.ui.getFormType() === 1) {
 // Show an error message
 document.getElementById("spDocsFrame").src =
 'data:text/html;charset=utf-8,' 
 + escape('<html><head></head><body><div style="border: 1px dashed red; padding: 50px; margin: 10px; text-align: center;">Please create the record to enable this content.</div></body></html>');
 
 // Wait half a second and try again.
 setTimeout(checkForm, 500);
 } else { 
 document.getElementById("spDocsFrame").src =
 parent.Xrm.Page.context.getClientUrl()
 + "/userdefined/areas.aspx"
 + "?formid=ab44efca-df12-432e-a74a-83de61c3f3e9"
 + "&inlineEdit=1"
 + "&navItemName=Documents"
 + "&oId=%7b" + parent.Xrm.Page.data.entity.getId().slice(1, -1) + "%7d"
 + "&oType=" + parent.Xrm.Page.context.getQueryStringParameters().etc
 + "&pagemode=iframe"
 + "&rof=true"
 + "&security=852023"
 + "&tabSet=areaSPDocuments"
 + "&theme=Outlook15White";
 }
 }
</script>
</head>
<body onfocusout="parent.setEmailRange();" style="word-wrap: break-word;" onLoad="checkForm()">
<iframe id='spDocsFrame' src=''></iframe>
</body></html>

For more information about Dynamics 365 or if you would like an in-depth view of the services and solutions CRGroup has to offer, contact us at 1.800.576.6215.


About the Author:

Matthew Foy is a .NET developer working on CRGroup’s SharePoint and CRM consultancy team. He has a passion for robust solutions and a keen interest in solving problems.