Dynamics 365 and SharePoint Online Integration – Web Api Development
Posted on 2/28/2018 7:55:00 AM by Levin, AricBusiness Solutions Cloud Platform Development Dynamics 365 Dynamics CRM Online Services SharePoint
Now that we have configured our SharePoint and Azure environments to allow us to create an API that will be called from CRM and process SharePoint requests, we will dive into the actual API.
If you haven’t developed any API projects with Visual Studio, I would suggest familiarizing yourself with Web API, and take a look at the following Microsoft Documentation: Getting Started with ASP.NET Web API 2 (C#).
The Web Api projects contains the following components which we will get into further details in this post:
- References to Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime
- Reference to CAML Query Builder. This is a modified version of the CAML Query builder that is available on github: https://github.com/noormahdi/CAML-Query-Builder
- Controller classes for DocumentLibrary and LookupItems
- Model classes for LookupItem, SharePointDocument and SharePointFolder
- Helper classes for dealing with File Upload, Data, Encryption, and SharePoint
As the API library contains a lot of different functionality, I will go through the process of retrieving the documents API to show in the subgrid. The full source code and instruction on how to use will be available soon on github, so that you can implement this in your own environment. We will start by taking a look at one of the HttpGet methods in our Api Library. This is the starting point that the user will execute against the Api and get a response of the list of documents that are available on SharePoint.
The function starts by Retrieving the Authorization Credentials. This is either passed by the Web Resource from CRM as an encoded string as part of the request, or in our case stored in the application setting. The function checks if the Request.Authorization.Header contains a value. If it does it retrieves the string from the Authorization header, or if not it creates the Authorization string from the Application Settings Username and Password.
We then call the Set SharePoint Credentials function passing the Authorization string. This is using a Helper Class that handles with connectivity to SharePoint and executing SharePoint Client Commands against SharePoint.
The GetDocumentsById connects to SharePoint (if not already connected), creates a CAML query with a where clause to search by the Master Id and returns a ListItemCollection.
We then loop through the items of the collection, and convert each ListItem to a SharePointDocument. SharePointDocument is a Model class that contains the fields that are returned from SharePoint, and will be returned to the web resource as a Json string. The function below shows the ListItemToSharePointDocument function which handles the conversion.
Finally, we create the response passing a Status of OK (regardless whether or not files were returned), and serialize the files object as a Json string and return the response.
The list of functions that are included as part of the Api are:
The next and final articles in this blog series will cover the creation of the web resource.
Navigate to original post to enter comments
Popular Blog Categories
- Business Solutions
- Cloud Platform
- Common Data Service
- Dynamics 365
- Dynamics CRM
- IT Solutions
- Office 365
- Online Services
- Power BI
- Power Platform
- PowerApps Component Framework (PCF)
- Skype for Business
- Small Business Solutions
- Unified Interface