Generate Custom Upload to SharePoint in Dynamics CRM

Miguel Nepomuceno, 20 September 2018

While Microsoft Dynamics CRM / 365 offers integration options and features available with SharePoint On-Premise/ Online, there are some client cases where doing the SharePoint Integration is not worth setting up. We encountered this situation with one of our clients who wanted to integrate Dynamics CRM On-Premise to SharePoint Online. They did not want to setup the out-of-the-box integration because of their limited infrastructure setup, and mainly to them not wanting to face a big data migration cost as they are hoping to upgrade to Dynamics 365 (Online) in the coming future.

With this premise in mind, we were tasked with implementing custom SharePoint logic in their CRM, such as creating a SharePoint Folder for a CRM record using the SharePoint REST API. This blog will cover one of the tasks required, which was generating a link to upload a Document to a SharePoint Document Library from CRM.

image

For this blog demonstration, the Document Structure follows the format: MagnetismRootSite > Entity > Record.

image

Default Upload Button in Library vs. Upload Form

On every Document Library in SharePoint, there is an Upload button available which essentially uploads a Document under that Library.

image

We had a button that opens the CRM record (e.g. Account record “Magnetism Solutions Ltd) under its Entity folder (e.g. Account entity) in SharePoint.

image

While this directs the user, on click, to the Document Library so that they CAN upload a document under this Folder, we wanted a more user-friendly way to direct the user to upload a document straight from CRM.

To do this, we needed to generate a link to upload a SharePoint Document to a Document Library from Dynamics CRM. This is essentially an in-built Upload form that is linked to a specific Document Library and allows the user to select the File they want to add under the Library. While this doesn’t save the user any clicks compared to opening the Document Library in SharePoint and clicking the ‘Upload’ button (screenshot above), we want to explicitly guide the users on adding a Document and direct them straight from CRM.

Making the Button

First we need to construct the custom Ribbon button, which I’ve labeled ‘Upload Documents’. I’ve constructed this using Ribbon Workbench. This button then calls a JavaScript Web Resource that will open the Upload URL for that specific CRM record.

image

Constructing the Upload Form URL

In the Web Resource, what we are essential doing is constructing a URL. For the above example, it looks like this:

https://magnetismtest.sharepoint.com/sites/MagnetismRootSite/_layouts/15/Upload.aspx?List={D0068158-2706-49D5-88CA-2645CD1449BC}&RootFolder=/sites/MagnetismRootSite/Shared Documents/Account/Magnetism SolutionsLtd&Source=https://magnetismtest.sharepoint.com/sites/MagnetismRootSite/Shared Documents/Account/Magnetism Solutions Ltd&IsDlg=1

We can break this URL to the following basic format in our Web Resource:

http://yourServer/sites/yourSite/_layouts/15/Upload.aspx?List={Site GUID}&RootFolder={Relative URL}&Source={URL to Navigate}&IsDlg=1 

The link has the following information which you need to set in your Web Resource when constructing a working URL.
• The  number /15/ is the version of SharePoint (15 is for SharePoint 2016).
List = GUID for Document Library. This is essentially the GUID for the MagnetismRootSite for my example.

In order for me to obtain the GUID for MagnetismRootSite, I did the following:
      1. Navigate to Site Settings. To do this, go to:
          https://magnetismtest.sharepoint.com/sites/MagnetismRootSite/_layouts/15/settings.aspx
      2. In Site Settings, under ‘Site Administration’, go to ‘Site libraries and lists’.

image

     3. In “Site Libraries and Lists”, go to “Customize “Documents””.
     4. Step 3 will take you to the Site’s Document Settings page. Obtain the GUID for the Site in the URL:

image

RootFolder = the Folder in the Library. This is optional.

Since we want to add the Document for the ‘Magnetism Solutions Ltd’ Account record under the Account Folder, I’ve set this as the SharePoint Relative URL for the CRM record.

&RootFolder=/sites/MagnetismRootSite/Shared Documents/Account/Magnetism Solutions Ltd

Note: Don’t worry about spaces when constructing your link in JavaScript. The spaces get replaced with %2F when it is opened in the browser.

Source = URL to navigate to after the upload is complete. This is optional.

This depends on your requirements. You can set this to the upload URL such that the user can recursively upload another Document after an upload. I’ve set my URL to open the Document Library of the Account record after an upload is finished such that the user can see their files under the folder.

&Source=https://magnetismtest.sharepoint.com/sites/MagnetismTest/Shared Documents/Account/Magnetism Solutions Ltd

• IsDlg = controls if the upload form is displayed as a popup or a full page. Set this to 1 for a popup, or leave this off (i.e. do not include this) in your URL for a full page.

&IsDlg=1

Once you’ve obtained all this relevant information, you now just need to construct your URL in the JavaScript Web Resource. 

image

In the screenshot above, for demonstration purposes, I hardcoded the values.

It is not recommended for you to hardcode values in your JavaScript code. Recommended ways you are able to dynamically store these in CRM and obtain these values in JavaScript are either through:
• A custom Settings entity. These include values which are used commonly by all my URLs i.e. the SharePoint Base URL, Site Relative URL, and the Site GUID.
• The specific CRM record itself. These are values which are specific to the CRM record such as the record relative URL (to be placed after &RootFolder=). I was able to store these in fields for the Account record since I am using the URLs for my other custom “Open SharePoint” button:

image