Chatbot: Accessing SharePoint Documents via Power Virtual Agent — Part 2

Emmanuel Adegor
8 min readSep 22, 2023

In case you overlooked part 1 of this article/tutorial, you can catch up by clicking here.

Now, let’s proceed to the integration of our PVA with Power Automate and SharePoint Online.

Since this article serves an educational purpose, I will configure a workflow specifically for a single policy category. If you need me to extend this tutorial by showing you how to configure a dynamic workflow for an unlimited number of policy categories, just let me know on the comment section. 😉

Ref: https://google.com

Steps:

  1. Click on the “+ ” icon under the “Health” condition card
  2. Click on “Call an action”
  3. Click on “Create a flow”

This will automatically launch and redirect you to your “Power Automate” environment as you see below:

4. Click on “Add an input”

5. Click on “Text”

6. Within the “Input” text field, enter a variable name of your choice to store the value of your PVA’s “PolicyCategory” variable.

To jog your memory, in the previous section (part 1), we introduced a choice variable within our category condition cards, which we labeled as “PolicyCategory,” as illustrated below:

7. Within the “Please enter your input” text field, enter the name of your PVA “PolicyCategory” variable.

The purpose of the “PolicyCategory” variable is to retain the user’s selection. For instance, the bot might inquire, “Which Policy document do you require?” and offer four options. When the user selects, let’s say, “Health,” this choice is recorded within the variable. Subsequently, we can access this stored value within Power Automate for further processing.

8. Click the “+” icon under the “Power Virtual Agents” trigger card

9. Click “Add an action”

10. Search and click on the “Get files (Properties only)” action

11. Select your site address and library name

12. Include a filter query to refine the search based on the PVA category chosen by the user in the chatbot window.

N.B: You have to create multiple workflows for your other categories

Let’s add a variable action card to our workflow, that will store the document links

13. Click on “+” above the “Get files (Properties only)” card

14. Click “Add an action”, then search and select the “Initialize variable” action.

15. Give the variable a name for example “Document Link”

If you have a programming background, the temptation to eliminate the spaces within the variable name might arise. However, it’s important to note that the presence of white spaces doesn’t affect its functionality; it will still operate as intended.

16. Select “String” as the type

17. Under the “Get files (Properties only)” action card, click “+”

18. Add an action called “Append to string variable”

19. In the name field, select your variable name “Document Links”

20. In the value field, add a dynamic value “Link to item”

Once you’ve added the “Link to item” dynamic value, an “Apply to each” loop will be generated automatically. This occurs because there might be multiple document links returned from the SharePoint document library.

BONUS: Include a comma after “Link to item” in the “Append to string variable” card, as illustrated below:

This will separate the links with a comma

21. In the “Return value(s) to Power Virtual Agents” card, click “Add an output”

22. Click Text (just like you did in step 4)

23. Within the “Input” text field, enter a variable name to store the links returned to the variable “Document Links”

This variable, which I’ll label as “policyLinks” holds significance because our PVA lacks direct access to the “Document Links” Power Automate variable but can retrieve its value indirectly through this intermediary variable.

24. Within the “Please enter your input” text field, select “Document Links”

25. Click “Save” so we can continue our PVA development

Return to PVA, and within the dialog box that reads, “You’ve opened a new tab to create a new Flow,” select the “Done” option to update the Flow list with your newly created Flow.

26. Repeat steps 1–2

27. Select the “Power Virtual Agents Flow Template” option if, like me, you happened to forget to rename your Power Automate workflow. Alternatively, you can go back to Power Automate, rename the workflow, and then save it once more.

28. Next, on the PVA page, save your changes and refresh the page to ensure that your workflow is accurately reflected.

29. Once you select the workflow, it will create your “Action” card as you see below:

You’ll notice that the “policyLinks” variable from Power Automate has been automatically chosen under the “Outputs” section.

30. Within the “Enter or select a value” card, select your PVA “PolicyCategory” choice variable.

31. Under the Actions card, click on “+”

32. Click “Send a message”

33. As soon as the “Message” card appears, you may notice an error on the “Policy Category” Action card, indicated by the message “There is an error ‘BindingIncorrectTypeError’.”

This error arises due to the difference in variable types: “policyLinks” from Power Automate is a string variable (Text), while “PolicyCategory” in PVA is a “choice” variable. The field PVA “Power Automate Inputs” expects a string. To resolve this error, we need to encapsulate “PolicyCategory” within a Text() function. This conversion will transform the “choice” value into text format. If you’re familiar with Power FX queries in Power Apps, you’ll recognize this formula.

34. Click on the arrow icon within the “PolicyCategory” field

35. Select formula

36. Within the “fx” field, type “Text” then select the “Text” function.

37. Next add parenthesis to the “Text” function, and within the parenthesis, type “Topic.PolicyCategory”, where “PolicyCategory” is your variable name.

38. Click insert

39. Your action card will now resemble the screenshot below, effectively eliminating the error:

40. Update the “Send a message” card that you added in step 32.

41. In the “Enter a message” field, input a response to convey the document links to the user. Additionally, include the variable for the links by selecting {x}, as demonstrated below:

Now let’s test:

42. Click Save and click “Test bot”

During the testing phase, you’ll observe that the bot combines multiple document links into a single link, without including line breaks to separate them. Consequently, clicking on the link will only redirect you to the first document link returned. To address this issue, we can resolve it by utilizing “space” and “compose” features within Power Automate.

To fix this,

  1. Go to power automate
  2. Locate the PVA workflow
  3. Click Edit

4. Add a new initialize variable card (I’ll name the variable card “New Line”)

5. select a type of “String”

6. In the value field, hit your “enter” or “return” key on your computer

Hitting the enter or return key in the value field will create a new line

7. Under the “Apply to each” card, search and add a “Compose” card

8. Click the Compose “Inputs” field

9. In the fx field within the “Expression” section, insert a “replace” formula

10. replace(<<Document Links variable>>, ‘,’ <<New Line variable>>)

This will appear as: replace(variables(‘Document Links’), ‘,’,variables(‘New Line’))

This replaces the comma that we added in the “Bonus” step between steps 20 and 21

11. Click Ok

12. Next edit the “Return value(s) to Power Virtual Agents” card and replace the “Document Links” with the compose “Outputs”.

13. Then click save

Now “policyLinks” will reference the “Outputs” from “Compose”

14. Refresh PVA and test the bot again

Now the links are well spaced out.

In this tutorial, you’ve acquired the knowledge of accessing SharePoint Documents through Power Automate within the Power Virtual Agent Chatbot.

Ref: https://google.com

--

--

Emmanuel Adegor

SharePoint Developer | React JS | SPFX | SharePoint Administrator