Search code examples
node.jssharepointmicrosoft-teamschatbot

Microsoft teams chatbot searching in sharepoint library


I have developed a basic Teams chatbot using the Hello World Tutorial from Microsoft (with Node.js). What I would like to make this bot able to search through the Sharepoint documents library. Users could ask questions like "the contract for product B" to the bot in teams and the bot could reply with the doc url or, better, the doc itself. So :

  1. Is it possible ?
  2. If yes would you know a tutorial that could help me ?

Thanks


Solution

  • Yes, this is possible. You need to be aware that a bot is basically just a web service with a special REST endpoint, so it can do anything any other web service can do, which includes for example calling to SharePoint. The important thing for you to be aware of is that the bot / web service has no access rights by itself into SharePoint, so you need to think about authentication to the SharePoint library. In short:

    1. You should use Microsoft Graph to connect to SharePoint, something like this: https://learn.microsoft.com/en-us/graph/api/resources/onedrive?view=graph-rest-1.0 or more specifically this: https://learn.microsoft.com/en-us/graph/api/driveitem-list-children?view=graph-rest-1.0&tabs=http

    2. If you look at the Permissions section in the above link, notice that your service (i.e. bot) needs to have either 'Application' permissions, which means a standard permission applied to the bot itself, and granted by the tenant admin, to retrieve any files, or 'delegated' which means it can only fetch items the user has access to, and the bot operates on the user's behalf. The 2nd option requires you to implement SSO in your bot, to get the user's access token, exchange it for a Graph token (something called an "OBO" or "On Behalf of" exchange), and then calling to SharePoint.

    I've tried to keep the answer focused on your question. If you want more details, especially on the 2nd part which is more detailed and might not be familiar to you, let me know, but it should be a separate question here on this site in that case.