Custom SharePoint Integration for Dynamics 365 Online


Microsoft SharePoint and Dynamics 365 Integration 


Microsoft SharePoint is a document management and collaboration tool that helps in maintaining storage space [1]. 

It can also be further integrated with Microsoft Dynamics 365 (D365) to give you extended document management and collaboration capabilities attached to it. 

With this integration, D365 customer data can easily be sourced from SharePoint and customer data documents can be automatically attached to the customer record [2]. 

In addition to this, this integration comes with many more advantages as well [3].

  1. Added performance
  2. Improved security
  3. Easy access
  4. Out of the box customizations
  5. Inter-entity linking

SharePoint Integration Customization


Out of the Box SharePoint refers to the core components of MS SharePoint. When integrating with D365, it is good enough to use the Out of the Box (OOB) SharePoint - Dynamics CRM Integration as the core functionality [4]. 

However, sometimes you may need to extend this to meet certain clients’ expectations.

Using SharePoint Client Object Model (CSOM) for Customizations


To develop this kind of third party solution or customization, the SharePoint Client Object Model (CSOM) can be used [5].

There are two methods to develop this kind of solution for D365 Online.

  1. Use the SharePoint Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll and use the standard methods [6]. 

    Please refer this link for more details.

    However, in Dynamics 365 Online, there is a limitation that only one dll can be resisted and it won't allow a user to register other dlls for referencing.

    In this case, you need to merge your custom solution dll with Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll using a dll merging technique [7].

  2. Create a “custom solution” by calling the SharePoint REST API with web requests.

    The following is a sample code for creating a folder in the SharePoint document library using REST API. Similarly, you can create, delete, update records in SharePoint as well.


  1. public class SharePointService
    {
     private string _username;
     private string _password;
     private string _siteUrl;
     private SpoAuthUtility _spo;
     public SharePointService(string username, string password)
     {
    _username = username;
    _password = password;
     }
     /// <summary>
     /// For creating folder in SharePoint
     /// </summary>
     /// <param name="siteUrl">The site URL.</param>
     /// <param name="relativePath">The relative path.</param>
     public string CreateFolder(string siteUrl, string relativePath)
     {
    if (siteUrl != _siteUrl)
    {
    _siteUrl = siteUrl;
    Uri spSite = new Uri(siteUrl);
    _spo = SpoAuthUtility.Create(spSite, _username, WebUtility.HtmlEncode(_password), false);
    }
    string odataQuery = "_api/web/folders";
    byte[] content = ASCIIEncoding.ASCII.GetBytes(@"{ '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '" + relativePath + "'}");
    string digest = _spo.GetRequestDigest();
    Uri url = new Uri(String.Format("{0}/{1}", _spo.SiteUrl.ToString().TrimEnd('/'), odataQuery));
    var webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
    .Headers.Add("X-RequestDigest", digest);
    // Send a json odata request to SPO rest services to fetch all list items for the list.
    ,
    "POST", // reading data from SP through the rest api usually uses the GET verb 
    content,
    webRequest,
    _spo // pass in the helper object that allows us to make authenticated calls to SPO rest services
    );
    string response = Encoding.UTF8.GetString(result, 0, result.Length);
    return response;
     }
    }

There are certain helper classes which are needed in order to execute the above sample methods. These can be found here.


References


  1. Microsoft SharePoint : www.docs.microsoft.com/en-us/sharepoint

  2. How Dynamics 365 and Office 365 work together: https://www.sherweb.com/blog/dynamics-365/dynamics-365-office-365-work-together/

  3. Benefits of integrating SharePoint with Microsoft Dynamics CRM: https://opencirrus.org/benefits-integrating-sharepoint-microsoft-dynamics-crm/

  4. Setup Customer Engagement Apps to use SharePoint online: www.community.dynamics.com/crm/b/magnetismsolutionscrmblog/archive
    /2017/12/19/setting-up-server-based-sharepoint-integration-for-dynamics-365-online

  5. Get Started using the client object model with external data in SharePoint: www.docs.microsoft.com/en-us/sharepoint/dev/general-development/get-started-using-the-client-object-model-with-external-data-in-sharepoint

  6. www.docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ee537013(v%3doffice.14)

  7. www.manuelmeyer.net/2016/01/net-power-tip-10-merging-assemblies/

  8. www.drive.google.com/file/d/1SSepbcz77k57vWNP8cAT2Lzj5_gMqRNi/view?usp=sharing



Thusitha Dissanayake

Technical Lead