Redirecting document citation URLs in Microsoft Copilot Studio Code Editor

Copper Contributor

Hi all, 

 

I've created a genAI boosted Copilot in Microsoft Copilot Studio, using uploaded documents as the sources. When I message the test Copilot in the console, the citation links just lead to a pop-up with the relevant section of text. Instead, I would like to redirect the citation URLs so they lead to the respective original document which is stored on SharePoint. For various reasons, I can't boost the Copilot with the Sharepoint page directly, hence saving it as a document and uploading. 

 

In the code editor, I have created a record that maps the name of the document to the url I want it to link to, like so:

 

    - kind: SetVariable
      id: setVariable_C9TaRa
      variable: Topic.source_mapping
      value: "{     \"doc1.docx\": \"https://sharepoint.com/sites/team/doc1.aspx\",     \"doc2.docx\": \"https://sharepoint.com/sites/team/doc2.aspx\"}"

    - kind: ParseValue
      id: OSXhWy
      variable: Topic.source_mapping_record
      valueType:
        kind: Record
        properties:
          doc1.docx: String
          doc2.docx: String

      value: =Topic.source_mapping

 

 

By default, the citation part of the code looks like this:

 

                    type: "Container",
                    items: ForAll(Topic.Answer.Text.CitationSources,
                      {
                        type: "TextBlock",
                        text: "[" & Id &"]: [" & Name & "]("& Url &")",
                        wrap: true,
                        size: "Small",
                        weight: "Lighter"
                      }
                    )

 

 

I changed the URL part (as the URL variable was empty as they're documents) so it instead calls my record with the source name to insert the URL. When I hard-code in the source name, it works perfectly and provides the link to the SharePoint, i.e, instead of having ("& Url &"), having:

 

("&Topic.source_mapping_record.'doc1.docx'&")

 

 

However, when I try to use the name variable instead of hard coding the name, like so:

 

("& "Topic.source_mapping_record." & "'"&Name&"'" &")

 

 It will just print (Topic.source_mapping_record.‘doc1.docx’) in the response citation. 

 

I basically need to know the correct syntax for embedding the variable 'Name' in the record call, so that it doesn't read the entire thing as a string. 

 

Thanks!

 

 

2 Replies

Hello @hc452 

Have you managed to solve this?

Regards

@hc452 I also want to do this.  Can you share  more info about the steps you use to get to the code editor?

 

I pasted your entire post into ChatGPT, and this is what she said:

 

To correctly embed the variable `Name` into your record call within the citation URL, you need to adjust the syntax so that it dynamically accesses the record's key using the `Name` variable. In your current setup, it treats the entire expression as a string, which is why it's returning the literal text.

Here’s the correct approach:

Instead of this:

("&Topic.source_mapping_record.'doc1.docx'&")

 

You should use a dynamic reference, ensuring the key is interpreted as a variable, like this:

"(" & Topic.source_mapping_record[Name] & ")"

Here’s how the modified section of your code should look:

type: "Container",
items: ForAll(Topic.Answer.Text.CitationSources,
  {
    type: "TextBlock",
    text: "[" & Id & "]: [" & Name & "](" & Topic.source_mapping_record[Name] & ")",
    wrap: true,
    size: "Small",
    weight: "Lighter"
  }
)

### Explanation:
- `Topic.source_mapping_record[Name]` dynamically retrieves the value from your record based on the current `Name` variable.
- `Name` is used as the key to access the corresponding SharePoint URL from `source_mapping_record`.

This way, the URLs will point to the correct SharePoint document based on the document's name dynamically, rather than being hardcoded.