Passing multiple URL parameter values to PnP Search

The deeper I dig into PnP Search the more I learn! I often use URL parameters to send search terms to custom search pages. This is usually to allow a user to click a link in a SharePoint library to get a dynamic result e.g. from a custom file, use PnP Search to show a page with related content.

This is cool, but what if the requirement is to find items that match one of a list of values. For example, I have a List of Business Owners with a text column containing one or more CompanyID values separated by a space. The Business Owner can own more than one Company. I want one search result to show items for each of the CompanyID’s.

In my “Companies” Document Library I have documents tagged with the CompanyID e.g. company registration, constitution, annual returns etc.

Their is a small challenge. When you send the CompanyID’s string to PnP Search it will work if their is only one CompanyID but fails if their are multiple. The reason is the unless you specify the “OR” between each value, PnP Search uses an implicit “AND”, resulting in the values not matching and no results.

To solve this problem, I started with the JSON formatted column in my SharePoint List. I used this formatting on the column containing the CompanyID values (a space separated list, other delimiters can be used by editing the “=split” code below). The URL is the link to the custom Search page with a URL parameter “Params”.

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "a",
  "attributes": {
    "href": {
      "operator": "+",
      "operands": [
        "https://xxxxxxx.sharepoint.com/sites/TestSite/SitePages/SearchArrayTest.aspx?Params=",
        {
          "operator": "join",
          "operands": [
            "=split([$CompanyID], ' ')",
            " OR "
          ]
        }
      ]
    },
    "target": "_blank"
  },
  "style": {
    "color": "blue",
    "text-decoration": "underline"
  },
  "txtContent": "View related documents"
}

In the JSON code you can see the URL including parameter, column name (CompanyID) and the “OR” I am adding as a delimiter between values in the URL.

Next I needed to do some configuration in PnP Search. I created a new page and added a Search Result webpart (no need for a Search Box). I edit the Search Result Webpart and changed the Query template to use my URL parameter (Params).

To test the Search page I manually entered a parameter. The URL below is an example, where I have two parameters C100 and C210 separated by an OR. Note the %20 is a space and is automatically added, you don’t need to format that yourself.

https://xxxxxx.sharepoint.com/sites/testsite/searchpage.aspx?Params=C100%20OR%20C210

The Query Template can include additional parameters such as using a managed property, specifying the site or library path, filtering by content type etc.


Discover more from SharePoint Moments

Subscribe to get the latest posts sent to your email.

Leave a comment