In the previous part of the article, we have completed the initial setup. In this part of the article, we will implement chat bot which will access data from SharePoint.
Highlights of the article series
Prerequisite
In this diagram, I have mentioned sequence numbers to identify the flow.
SharePointController.cs
I have updated Post( ) method to invoke SharePointDialog with parameter activity.
SharePointDialog.cs
I have modified GreetWelcome method, which will greet user and provide url for authentication. I have kept this url in web.config as ‘SHAREPOINT_LOGIN_URI’ key. As we are working on development environment, I put localhost web application value. This url is targeted to action ‘LoginWithSharePoint’ of ‘Home’ controller. We will pass username of current user as query string parameter to save token against it after login.
web.config entries (bot application)
3. When user clicks on link, control will go to LoginWithSharePoint action method in Home controller of our MVC web app. We will get user name from query string parameter. We will save it to session for future use.
4. We will form O365 authentication url using SharePoint site url, client id of app and redirect uri. Navigate the user to login page. I have provided redirect URI as action method LoggedinToSharePoint of Home controller.
web.config entries (AuthenticationWebApp)
5. After successful login, ACS will return control to Redirect URI i.e. in LoggedinToSharePoint action method. ACS will make post call to reditect URI and send ContextToken as Form parameter named SPAppToken.
6. Our application will save ContextToken against user name in MongoDB and ask user to continue with chat by returning View.
Contracts.cs
Mongo.cs
You can see context tokens are saved in MongoDB using RoboMongo (client app for MongoDB)
7. After successful login user comes back to chat window and starts a conversation with bot. Let’s say, user will search for Amit. LUIS will identify intent and control will go to SearchPeople method of SharePointDialog. Here it will extract the entity as Amit and invoke FindUserByName method of SharePoint repository.
8. In FindUsersByName( ) method, Token for respective user is retrieved from MongoDB. And it will be used to create clientcontext. I have PeopleDetails list in SharePoint site with following details. Then CAML CONTAINS query will be executed against PeopleDetails list for Title column.
9. Chat bot will return html containing list of users and their contact numbers. Keep trying different authentication services. Happy Chatting! :)
Pro WPF: Windows Presentation Foundation in .NET 3.0