Image Search with PnP Modern Search v4

One of our clients had a file share with thousands of photos that they wanted to move to SharePoint. They used Adobe Bridge to tag the photos with keywords, and asked us if it was possible to bring in the keywords and allow users to filter the images by keyword. Since SharePoint’s out of the box search does not currently allow for that type of filtering (while typing a keyword would work as well, users would have to know what to type initially), we thought this would be a great use case for the PnP community’s Modern Search web part! And the best part is that this doesn’t take any code, just a few configurations and settings!

Screenshot from Adobe Bridge
This screenshot from Adobe Bridge shows the keywords which have been added as file metadata.

Building the image library

To keep the Modern Search web part’s search results limited to the photos, create a new Document Library to hold all the images. In Site Contents, click on New > Document library, type a name for the new library, and click Create.

Create document library panel

The next step is very important to be able to easily bring in the keywords that were added with Adobe Bridge. Click on the gear, go to Library settings, click on Enterprise Metadata and Keywords Settings, check the checkbox on the page, then click OK.

Screenshot of Enterprise Metadata and Keywords settings

Back on the document library page, click on the Add column button and add the Enterprise keywords to the view. (Don’t forget to hit “Apply” at the top!) This isn’t a required step, but this will help you to see the keywords associated with each photo.

Screenshot of Edit view columns

The image library is now ready for you to import your images, so go ahead and drag and drop them into the SharePoint library. As SharePoint uploads the images, it will pull the metadata keywords and automatically map them to the Enterprise Keywords column! (If you are trying to work with an existing library, you will unfortunately have to reimport the images.)

Configure search web parts

Now you’re ready to use the PnP Modern Search v4 web part to create your search result page! You’ll need to first install the latest version of the PnP Modern Search v4 to your app catalog.

Create a new page in your site and add a one-third right section to your page.

Add one-third right section

Add the PnP Search Box web part and PnP Search Results web part to the left column, and add the PnP Search Filters web part to the right column.

Add web parts to page

Let’s set up the Search Results web part first. Click the Configure button, then go through the panels with these settings:

First, select SharePoint Search. The panel will then show all the other properties that can be set.

In the query template box, add the path to your image library and specify the image file types you want to show:

{searchTerms} Path:https://yourtenant.sharepoint.com/sites/YourSite/YourLibrary* (FileType:jpg OR FileType:png OR FileType:tif)

Click Next to advance to the third panel. Click the toggle to turn on “Use input query text.” Select “Dynamic value,” then in the dropdown, choose the “PnP – Search Box” web part. Under PnP – Search Box’s properties, select “Search query.”

Also, click the toggle to turn on “Connect to a filters Web Part.” In the dropdown, select the Search Filters Web Part.

Now, set up the Search Filters web part. Click the blue configure button.

Under Use data from these Web Parts, select the Search Results Web Part.

Click the Edit button under Customize filters. For the Display name, type “Keywords.” For the Filter field, type “owstaxidmetadataalltagsinfo.” Select Checkbox under template, and also check the Multi values checkbox. You can also check the “Expand by default” checkbox if you want the filter to be shown by default.

At this point, if you save your page, you should see the images from your library appearing in the search results and your keywords showing up as filters in the right panel!

Search results showing - in progress

Fix the tags

However, you may see the tags on the cards as shown above, with a lot of confusing numbers and letters before the tag name. I’ll show you the official way to resolve it first:

There are two quick steps to take to get the tags to render properly. Edit the search results web part, and in the first panel, click the “Customize” button under Layout slots. Look for the Tags slot, and you’ll see that the Slot field is “owstaxidmetadataalltagsinfo.” Add the word “Auto” before that, like this:

Add "Auto" before "owstaxidmetadataalltagsinfo"

(Side note – I could not find any information on what the “Auto” prefix does for this field and other ones such as “AutoPreviewImageUrl” and “AutoPreviewUrl.” If anyone knows more about this, please share in the comments below!)

Scroll down and click “Save” to commit the slot changes.

Back in the main search results properties pane, scroll down and toggle “Enable localization” to be on.

Enable localization

This allows the label to show as you’d expect:

Tags show properly

However – clicking the tag doesn’t actually seem to DO anything. We’ll have to modify the link to add in our own functionality. These next steps will also demonstrate HOW COOL the Search v4 web part is and how much flexibility and functionality is part of the project, allowing us to make some powerful configurations within the web part settings, without having to rebuild any solutions!

Edit the search results web part again and go to the second panel of the web part properties pane. Scroll down to Layout options and click the “Managed card fields” button. This panel allows you to tweak the placeholders within the individual card layout. Click the edit button in the Tags row:

Edit managed card fields

This opens a Handlebars editing panel. Notice the logic that allows the splitting of multiple tags (we aren’t changing anything there, just pointing it out!) as well as the HTML and Handlebars expression used for the URL of the link.

Handlebars expression

We’re going to modify the href to provide a query string. Then, we’ll update the web parts to use that query string.

Edit the code for the href to look like this:

href="?q=owstaxidmetadataalltagsinfo:'{{trim tag}}'"

Save the page. Now we’ll update the search box web part to use the query parameter, but you have to do it in a kind of wonky way.

In the browser URL bar, type ?q= after the page URL. Then, click the “Edit” button to edit your page. The page will reload with ?q=&Mode=Edit after the page URL. We need that “q” showing so that the search box web part can pull it in.

Edit the Search Box web part and advance to the third panel in the web part properties pane. Turn on the toggle for “Use dynamic data source as default input.” Under Connect to source, select “Page environment.” Under Page environment’s properties, choose “Query string.” Under Query string, select “Query parameters.” Finally, under queryParameters’s properties, you should be able to select “q.”

Set up search box query parameters

After saving the page, clicking a tag will now filter the search results by that tag!

(Notice that this solution does not result in the Filters web part being “checked” with the filter that is used. I believe there isn’t a code-free way to do this, but if you know of one, please comment below!)

The more we use the PnP Modern Search v4 solution, the more impressed we are. If you want to find out more about how you can do even more with it as a developer, be sure to check out Eric’s article and webinar about Modern Search v4!

Leave a Comment

Your email address will not be published. Required fields are marked *

Enter Code *

Filed Under

Related

Are you preparing to launch a new #SharePoint intranet?

Don’t spring it on employees out of the blue. First, talk to as many of them as you can.

Planning is just phase one. For all of our tips, watch our recent webinar!

http://ow.ly/dsQM50GqPI5

It's that time again! Join #Microsoft MVP & Regional Director, @EricOverfield on Thursday, October 28th, at 11:00 AM PST as he walks through #MicrosoftViva’s current state & offerings and what they mean to your #digitalworkspace right now.

http://ow.ly/3QFK50Gnx9f

It's the final countdown! Join @EricOverfield at 11am PST to learn about common pitfalls and paths to success when moving to #SharePointOnline. You'll walk away with practical tips to use in your organization today!

http://ow.ly/fdf150GjyJT

In today's episode of the #PixelMillWebinars, you'll learn from the experience of a global organization’s path to digital teamwork victory and get to see first-hand how to successfully plan a smooth migration for your team.

Join us at 11am PST!

https://pixelmill.com/webinar-9-takeaways-from-a-global-organizations-migration-to-sharepoint-online/

It's the day of the show y'all! Join #Microsoft RD +MVP @EricOverfield at 11am PST as we walk you through a multi-national enterprise software company’s migration journey to #SharePointOnline. Learn about common pitfalls and paths to success.

http://ow.ly/cN9250GjtGk

Subscribe to PixelMill's
E-news

* indicates required

Let's Talk Digital
Workspaces Today

Get In Touch