Overview:
This article provides you knowledge to synchronize O365 SharePoint list data with Azure SQL database table by making use of latest technologies/features like Azure Function app and Microsoft flows.
On every SharePoint list item, insert/update our custom Microsoft Flow will get triggered and calls Azure function app to do sync job operation with Azure SQL database table. Our custom Azure function app has all the functionality written to get SP list item information from list and then add/update in Azure SQL DB table. All operations performed by Azure function app will be logged to “Log Analytics” which is again a feature of Azure to maintain logs of any application for further reference. The content flow will be as mentioned below, between SP list and Azure DB table:
O365 SP List à Microsoft Flow à Azure Function app à Azure SQL DB
Same way, the synchronization flow from Azure SQL database table to SP list is as given below:
Microsoft Flow (Scheduled Job) à Azure Function app à Azure SQL DB à O365 SP List
Our solution is completely generic and can be used for multiple lists vs DB table synchronization just by changing configuration.
Components used in this article:
- Azure: Function App, SQL DB, Log Analytics
- O365: SharePoint List, Microsoft Flow
Now, let’s go through the detailed discussion required to implement the sync job between list and DB table. Below mentioned are the steps that we will follow in this article:
- Create SharePoint list
- Create Azure SQL DB and table
- Generate Client ID and Secret & grant access at site level
- Create Azure function app
- Create Log Analytics
- Create Microsoft Flow for SP to Azure SQL Sync (Single item)
- Create Microsoft Flow for Azure SQL to SP Sync (Multiple items)
- Add Azure Function App Configuration (Key & Value Pair)
- Test it by adding/updating items in SP list and DB table
Step 1: Create SharePoint List
- Login to O365 SharePoint Site (Ex: https://contoso.SharePoint.com)
- Create Custom List with Employee title
- Add below-mentioned columns to the Employee list
- FirstName
- LastName
- Designation
- EmployeeID
Step 2: Create Azure SQL DB and table
- Login to Azure portal (https://portal.azure.com)
- Create Azure SQL database with ContosoOrgDB (if already not created) (Instruction to create Azure SQL DB)
- Click SQL Databases option available in Left navigation à Select the ContosoOrgDB database à Select Overview option à Copy Server name present in the right window à Click Set Server Firewall à Click Add Client IP link present at top of the window à Click Save button present at top-left corner of the window. With this, your system will be added to the trusted locations to work with Azure database.
- Go to SSMS in your local machine à Click Connect symbol à Paste the server name which you copied in the last step à Provide your DB username and password à click Connect.
- Create table with Employee title and add the below mentioned columns to it. (The column names should be as same as SP list column names to perform Sync without issues).
- FirstName
- LastName
- Designation
- EmployeeID
Note: The account which you are using to login to Azure must have Azure subscription and enough access to create Azure “Function app” and “SQL Database”
Step 3: Generate Client ID and Secret & grant access at site level
Register App:
- Navigate to O365 SharePoint App registration page (Ex: <site collection url>/_layouts/15/AppRegNew.aspx)
- Generate Client ID and Client Secret by clicking Generate button
- Fill Title field with EmployeeSync
- Fill App Domain field with localhost.com
- Fill Redirect URI field with https://www.localhost.com
- Now, click the Create button to register the app
- Once created, it will show the details on the display page. Please copy it to your notepad.
Grant Access to the App:
- Navigate to <site collection url>/_layouts/15/AppRegNew.aspx page to grant access to the app
- Now, enter the Client ID which we generated in app registration page and then click Lookup
- It should fetch the app details once you click lookup
- Fill Permission Request XML field with below XML <AppPermissionRequests><AppPermissionRequest Scope=”http://sharepoint/content/sitecollection” Right=”FullControl” /></AppPermissionRequests>
- Now, click Create button
- Click Trust button to grant access to the app
- Now your app is registered to the site and granted with Full Access
Reference: https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/register-sharepoint-add-ins
Note: The account which you are using to login to O365 SharePoint site must have Site Collection access to register the app and to create Microsoft flows.
Step 4: Create Azure Function App
- Create Azure Function App Visual Studio solution by following this article. Let the solution name be EmployeeSyncApp
- Add HTTP trigger function to the solution by following this article. Let the function name be SPtoSQLSingleItemSync. Test the application as mentioned in the article to confirm the solution/function is working as expected.
- Write C# code in the above created HTTP trigger function. The code should include functionality to Get SharePoint List item details and Add/update item in the Azure SQL database table.
- Add one more HTTP Trigger function and the name be SQLtoSPMultipleItemSync. Test the application as mentioned in the article to confirm the solution/function is working as expected.
- Write C# code in the above created HTTP trigger function. The code should include functionality to Get all Azure SQL DB table records and Add/update item in the SP List.
- Navigate to https://portal.azure.com and create Azure function app by following below mentioned steps.
- Click Create a resource present at top left corner of the page
- Select Compute and then select Function App
- Provide App name as EmployeeSyncApp (Name should be unique)
- Select new resource group and provide AzureTestGroup value to it.
- Click Create
- Now go to All Services à Function apps à Select EmployeeSyncApp function app
- Click Get Publish Profile link to download.
- Now go to Visual Studio solution
- Right click on project and select Publish
- Select Existing radio button and upload downloaded Publish profile
- Click Publish button to publish azure function.
Step 5: Create Log Analytics
- Navigate to https://portal.azure.com
- Click Create Resource present at top left corner of the page
- Select Management Tools and then select Log Analytics
- Provide OMS Workspace name as EmployeeSyncAppLogs
- Select Use existing option for Resource Group and select AzureTestGroup
- Now click OK button
- Now, the log analytics is ready for use in our function app.
Step 6: Create Microsoft Flow for SP to Azure SQL Sync (Single item)
- Navigate to https://flow.microsoft.com
- Click My flows link present at top left corner of the page
- Click Create from blank link
- Again, click Create from blank button
- Select SharePoint connector
- Select When an item is created or modified trigger
- Provide SharePoint Site address (Site URL) where we created our Employee list (Ex: https://contoso.SharePoint.com)
- Select SharePoint List Name on which our flow must be triggered on item add/update operation. In our case, it’s Employee
- Click + New Step and select HTTP connector
- Select HTTP – HTTP trigger
- Select Method as Get
- The URI is nothing but the URL of the Azure function to which it must call
- Navigate to https://portal.azure.com
- Go to Azure Functions à Select EmployeeSyncApp function app à select SPtoSQLSingleItemSync function under Functions section
- Click on Get function URL link present at right side window and copy the URL.
- Go back to Flow and paste copied URL in URI field
- Add below mentioned headers
- Key=ReferenceColumn
- Key=ReferenceIds
- Click Save present at top right corner
- Provide Flow Name as SPtoSQLSingleItemSyncFlow
Step 7: Create Microsoft Flow for Azure SQL to SP Sync (Multiple items)
- Navigate to https://flow.microsoft.com
- Click My flows link present at top left corner of the page
- Click Create from blank link
- Again, click on Create from blank button
- Select Built-in tab at top and select Schedule connector
- Select Recurrence trigger
- Enter 1 in the Interval field and select Day
- Click + New Step and select HTTP connector
- Select HTTP – HTTP trigger
- Select Method as Get
- The URI is nothing but the URL of the Azure function to which it must call
- Navigate to https://portal.azure.com
- Go to Azure Functions à Select EmployeeSyncApp function app à select SQLtoSPMultipleItemSync function under Functions section
- Click on Get function URL link present at right side window and copy the URL.
- Go back to Flow and paste copied URL in URI field
- Add below mentioned headers
- Key=ReferenceColumn
- Click on Save present at top right corner
- Provide Flow Name as SQLtoSPMultipleItemSyncFlow
Step 8: Add Azure function app configuration
- Navigate to https://portal.azure.com
- Now go to All Services à Function apps à Select EmployeeSyncApp function app
- On the right side window, click Application Settings link
- Under application settings section, add all the configurations which are required for your sync job/function.
- In my scenario, I added below mentioned configuration settings:
- ClientId: This is the Client ID which we generated in step 3.
- ClientSecret: This is the Client secret which we generated in step 3.
- SPToDBSyncUrl: This is “SPtoSQLSingleItemSync” function URL.
- DestDBName: This is database where you table is present
- DestSQLConString: This is connection string for DB access
- DestTableName: This is DB table name
- LogName: Here, you must provide name of log analytics
- SharedKey: Log Analytics “Primary Key”
- SPListName: SharePoint list name
- SPSiteUrl: SharePoint site URL where your list present
- TimeDuration: Request timeout duration
Step 9: Test it by adding/updating items in SP list and DB table
- Now go to SharePoint list
- Add/update an item in the list
- Wait for a while and check Azure SQL DB table.
- You should find the record synced to SQL database table.
If you don’t find the record in the table, then please cross-verify the above steps and configuration in the Azure function app.