I know that this is not the hot thing to be discussed in these days where we have fluid integration between latest versions of CRM i.e. Dynamics 365 and SharePoint or D365 with other document management vendors but still this might come handy when your client has the on-premise to on-premise integration between Dynamics CRM and SharePoint.

Note: This is a comprehensive post, within couple of hours, it helps to get the custom grid build for Dynamics CRM – SharePoint integration and handle the document CRUD operations from CRM.

Targeted audience are Developers and Techhnical Consultants. First segment is for Functional Consultants as well.

SharePoint with Dynamics CRM

SharePoint is the obvious choice considering storing the documents and associating it with the Dynamics CRM since CRM isn’t designed to manage the documents. On the other hand, maintaining the documents in SharePoint have advantages like,

Collaboration for non-CRM users to access documents.

Leverage SharePoint document manager features like checking out/check in and version history.

Provides the interface to support OneNote integration with CRM

SharePoint can sync to your Desktop Library and Outlook and other many more.

When we talk about SharePoint integration there are two ways of integration,

CRM List Component – only integration method available for on-premise SharePoint environments as they do not support server-based integration

Server-based SharePoint Integration – Dynamics CRM Server to SharePoint Server (server-to-server) authentication so there is no need to install any additional components.

But why custom grid?

As you know about Chrome’s famous modal-dialog issue which doesn’t allow uploading a new document using the OOB Sharepoint grid in CRM. To give some background, Google Chrome version 35 has deprecated the JavaScript method showModalDialog() and this is a core method used in dialog-return scenarios in Microsoft Dynamics CRM. Not all the client’s are happy with the registry change workaround which will the fix. Refer here for the details.

In this case, where the registry changes can’t be done/not allowed on client’s machine, we can go ahead building a custom page (aspx) and embedded in the iFrame on the form or in the navigation pane.

How it looks?

Once you built, it looks like this (of course, depends on the style you use).

Using jQuery block UI on load to show a custom message with loader for better user adoption. Note: You can have your custom styles as well instead of block UI and have used the same .gif that CRM uses.

2. View list of documents under a tab:

3. On deleting the selected documents, you can have the custom messages and on confirmation delete the file and route to the same page with the valid query string parameters.

4. Upload page with just a file upload control and a couple of buttons. On Submit, the document is uploaded to the SharePoint, shows a confirmation alert and the popup and window are closed.

Steps to build it:

Design the Home page (aspx) with grid and buttons to Add, Delete and Edit if needed.

In the code behind, the grid view is populated by the data retrieved from the SharePoint. To retrieve the files from the SharePoint, document location details (available within CRM) of that specific record needs to retrieve. To understand how SharePoint Document Locations work, refer to Debajit’s blog.

3. SharePoint Interfaces

This segment has the key logic for the CRUD operations for the SharePoint documents.

Upload a new document to SharePoint:

SharePoint’s elevated credentials is used for impersonation to do the operations but the actual user’s name should be seen in the grid’s Uploaded By and Modified By colums respectively. To achieve that, the current CRM user’s name is updated back to the SharePoint’s Author and Editor fields immediately after a successful upload.

Once you got the aspx page built, just call it from the CRM form, bingo!

4. JavaScript (web resource) to get the required data and set it to the iFrame (pass it to the aspx page)

The web resource is added in the Form and triggered by the expansion of the Tab. The script will retrieve the relevant data, in my case, it was the entity’s schema name and guid and pass it to the iFrame.

The OOB SharePoint grid in CRM created a SharePoint folder when you navigate to the Documents area from the from navigation but in our case, since we are using a custom page, it is our responsibility to create a SharePoint folder. We are creating a SharePoint folder when the record is created in the CRM in Plugin.

5. Plugin – to create a folder in SharePoint on the creation of a record

Create SharePoint document location in CRM if not available

if (_spDocLocId == Guid.Empty)
{
//There is no location so create one
_spDocLocId = CreateSharePointDocLocation(orgService, FolderName, entityID, entity.LogicalName, entityDisplayName);
//Get the abslouteURL from the doc location just created
string absouteUrl = GetAbslouteUrl(orgService, _spDocLocId);
// We still need to create a SharePoint folder
CreateSharePointFolder(absouteUrl, entityDisplayName);
}