Chat Bot with SharePoint Authentication - Part One

 
Highlights of the article series
  • How to register app in SharePoint
  • How to access SharePoint data in Chat bot

Prerequisite

  • To create LUIS app - refer this article.
  • To be ready with bot framework dev environment - refer to this article.
  • SharePoint environment with Apps configurations.
  • Install and configure MongoDB (I am using MongoDB to store tokens but you can use any db)
Code file is attached with article; you can pull code from GitHub also.

Initial Setup

Setup 1 Create Web app for authentication

Add new project with ASP.NET Web Application template in your bot application solution and select MVC option.

Make this web application "SSL Enabled" so that the site can be used to register an app to SharePoint.

Select web application project and press F4. It will display properties window.
 
Change "SSL Enabled" property to true. Make note of SSL URL; it will be required while registering the app.

Add the following methods to HomeController.cs.
  1. public ActionResult LoginWithSharePoint(string userName) {...}  
  2. public ActionResult LoggedinToSharePoint() {...}  
And, add LoggedinToSharePoint.cshtml under Views folder for Home controller.
  1. <!DOCTYPE html>      
  2. <html>      
  3.    <head><meta name="viewport" content="width=device-width" /><title>SharePoint Login</title></head>    
  4.    <body><div>You are logged in with SharePoint successfully.<br>Go back and enjoy chatting!</div></body>    
  5. </html>   
Setup 2 Register an app to SharePoint

As we are going to access SharePoint data in our application as an app, we need to register our app to SharePoint environment and get Client Id and Client Secret.

To register an app, go to url <<sharepoint_site_url>>/_layouts/appregnew.aspx. It will show option to generate Client Id and Client Secret.
 
Just click generate buttons, and it will populate respective values.
 
Put the title you want.
 
App domain will be the domain of your web application and Redirect URI will be the URL of the default page of web application where you want users to get redirected after authentication with SharePoint.
 
For development purpose, I put App domain as ‘localhost:44331’ and Redirect URI as ‘https://localhost:44331/’. After filling all the information, click on "Create" button.
 
Copy Client Id and Client Secret values for future use.


After registration, we need to grant permissions required for app on SharePoint environment. To grant permission, go to <<sharepoint_site_url>>/_layouts/appinv.aspx.
 
It will show textbox to provide App Id. Put Client Id you have copied above and click on Lookup button.
 
It will populate all information of app you have entered in the above step. Provide app permission XML in App Permission Request XML textbox.
 
Click on Save. After this, you need to Trust the app on next page.
  1. <AppPermissionRequests AllowAppOnlyPolicy="true">      
  2.    <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl" />      
  3. </AppPermissionRequests>   
The above XML depicts that we have provided ‘FullControl’ permission on site collection level for our app, and authorization on data will be done by APP permissions only not by user permissions.



App registration is completed. You can view registered apps under <<sharepoint_site_url>>/_layouts/appprincipals.aspx?Scope=Web.

We are ready with Initial setup. Now, we will start with actual implementation, which I will describe in the next article.

Read more on Microsoft Bot Framework

Up Next
    Ebook Download
    View all
    Learn
    View all