Поделиться через


Stage 9: Configure the query in a Content Search Web Part on a category page

This is a blog post in the series “How to set up a product-centric website in SharePoint Server 2013.”  In this series, I'll use data from a fictitious company called "Contoso" to show you how to use search features to set up a website based on product catalog data.
Note: Most of the features described in this series are not available in SharePoint 2013 Online.

For an overview of the blog posts in this series, go to How to set up a product-centric website in SharePoint Server 2013.

 

Quick overview

In previous blog posts, I showed you how to create a category page and a catalog item page. I also showed you how to assign these two pages to terms within the Site Navigation term set.  When we browsed to the "Audio" category, we couldn't see any content. This was because when we created the category page, we didn't add any Web Parts.

 

In this stage we will start to merge different pieces of what we have done in previous steps. In this blog post we'll learn:

 

Start stage 9

To display content on our Contoso website, we'll use the Content Search Web Part.

 

About the Content Search Web Part

The Content Search Web Part (CSWP) uses, as its name implies, search technology.

Most of us use search technology on a daily basis. Think about how many times a day you enter query terms in a search box, for example on bing.com; how after pressing Enter, you scan search results that are almost immediately displayed on a search results page (by the way, how did you find this blog post?).

When visitors browse to a page that contains a CSWP, they're probably not aware of this, but they're actually issuing a query. However, the thing that differs with CSWPs is that instead of entering query terms in a search box, the query is contained within the Web Part itself. This means that when a visitor browses to a page that contains a CSWP, this query is automatically issued.

Another thing that is different from the bing.com search scenario is that search results aren't displayed on a separate search results page, but within the CSWP. In most cases, visitors won't even know that search technology is being used to display the content they're viewing. To them, it'll look and feel like any other webpage.

 

How to add a Content Search Web Part to a page

Browse to the page where you want to add the CSWP. In our scenario, let's browse to "Audio".

  1. Click the Settings menu, and then click Edit Page.
  2. In the Web Part Zone where you want to add the Web Part, click Add a Web Part.
  3. In the Categories list, click Content Rollup.
  4. In the Parts list, click Content Search, and then click Add.

In our scenario, we'll add a CSWP to Zone 3.

CSWP added

The CSWP contains a default query, so it already displays some content (Audio, Cameras and Computers) -- but not the content we want to display. To make the Web Part display Contoso catalog content , we'll need to configure the query in the Web Part.

 

How to configure a query in a Content Search Web Part on a category page

  1. In the Web Part, click the Web Part Menu , and then click Edit Web Part.

Edit Web Part

  1. In the Web Part tool pane, click Change query. This will open a dialog box.

Change query

In the dialog box, notice that "Audio" is shown in the top left corner. This is the category which we navigated to and selected to edit the page from. Also notice, that in the RelevantResults section, the top three results, Audio, Cameras and Computers, are listed. These are the same three results that were shown in the Web Part when we added it.

CSWP default query

  1. From the Select a query list, select your catalog result source. In our scenario, it's catalog - Products Results.

CSWP result source

A result source narrows the scope from which search results can be retrieved. When we connected our publishing site to our catalog, SharePoint Server 2013 automatically created a result source for our catalog. In our scenario, this result source is named catalog - Products Results. By selecting this result source, only search results from our catalog are retrieved.

For more information about result sources, see Plan result source and query rules.

When we selected this result source, the number of RelevantResults changed from 864 to 775. 775 happens to be the number of items we have in our catalog. Therefore, selecting this result source confirms that we're on the right path to configuring the query.

  1. In the Restrict by tag section, select Restrict by current and child navigation terms.

CSWP restrict by tag

A key phrase in this selection is navigation terms. This refers to the category in the site navigation the visitor is browsing. In this particular case, the visitor is browsing the "Audio" category.

Audio URL

Remember, one of the first things we did in this series was to import catalog content into a list. We also imported terms into the term set Product Hierarchy, and associated each item in the list with a term from that term set.  When we connected our publishing site to our catalog, we specified that the full site navigation should contain terms from the Product Hierarchy term set. Because we have used the same term set to tag the items in our catalog and to build our site navigation, we can use a term from our site navigation to search for catalog items that have been tagged with that same term.

So, our query in the CSWP will therefore display search results for items that are in the catalog - Products Results result source, and that have been tagged with either "Audio", or any of the children of "Audio", for example "MP3 players" or "Speakers".

This selection reduced the relevant search results to 114, which happens to be the number of items in our catalog that belong to the "Audio" group.

Another key phrase from the selection Restrict by current and child navigation terms is "current." I'll talk more about the importance of this phrase in the next section.

  1. Click OK, and save the page.

On the "Audio" category, the search results have changed to show different results.

Audio results

If you browse to the "Cameras" category, you'll see three other search results displayed.

Camera results

If you browse to the "MP3" category, you'll see three other different search results are displayed.

MP3 results

If you are now thinking "OK, I understand how we got the correct search results for the "Audio" category, because that is the category we clicked, and where we changed the query in the Web Part. But why do we see different search results when we browse the catalog? And shouldn’t we change the query for all the other categories as well?"

Well, let's take a closer look at what's going on.

 

About the query configuration

We only had to configure one query because the same page is used for all categories. Remember how in stage 8 we assigned the page ContosoCategoryPage.aspx to all terms within the Site Navigation term set? We assigned this page to all terms, so even though we edited this page in the "Audio" category, we could have edited it in any other category, and achieved the same result.

We only had to configure the query once, because the query issued from the Web Part differs depending on which category we browse to. Remember that the CSWP contains a query that is automatically issued whenever someone browses to a page that contains a CSWP, and that search results are displayed in the Web Part. Also, remember that we selected Restrict by current and child navigation terms when we configured the query in the Web part. The word "current" is very important, because it means that the query issued by the CSWP will change depending on the category the visitor is currently browsing. If you edit the Web Part from another category, you can see that the Web Part has changed.

For example, if I browse to the "Cameras" category and take a closer look at the CSWP, I can see that:

  • "cameras" is included in the URL.
  • "Cameras" is in the top right corner of the query configuration.
  • The number of RelevantResults has changed to 118, which happens to be the number of items in the catalog that belong to the "Cameras" group.

CSWP camera query

So, when I browse to the "Audio" category, the CSWP issues a query for catalog items that have been tagged with "Audio" or any children of "Audio", and displays search results. When I browse to the "Cameras" category, the same CSWP (remember, we only used one page for all categories) issues a different query, this time for catalog items that have been tagged with "Cameras" or any children of "Cameras", hence different results are displayed.

 

How to view details of the query configuration

To view details of the query configuration, click on the TEST tab. The actual query issued by the CSWP, is shown in the Query text field.

CSWP TEST tab

In our example, the query that is issued by the CSWP from the "Audio" category looks like this:

(contentclass:sts_listitem OR IsDocument:True) SPSiteUrl:https://contoso/sites/catalog ListId:3a3f66cd-9741-4f15-b53a-b4b23c3187ea owstaxidProductCatalogItemCategory:#c771504f-6a2f-423f-98de-0e12fcfa08c9

If this doesn't make any sense to you, don't worry!  There is a logic to it, and I'll break it down to make it clearer.

  • (contentclass:sts_listitem OR IsDocument:True) SPSiteUrl:https://contoso/sites/catalog ListId:3a3f66cd-9741-4f15-b53a-b4b23c3187ea
    is our catalog result source, catalog - Products Results 
  • owstaxidProductCatalogItemCategory
    is the managed property for the site column Item Category (remember, our Product Hierarchy term set is tied to the Managed Metadata site column Item Category)
  • #c771504f-6a2f-423f-98de-0e12fcfa08c9
    is the GUID of the term in the current navigation, in this case "Audio"

 

In our Product catalog site collection, in the Product Hierarchy term set, you can see that the GUID represents the term Audio.

GUID audio term

 

So now we have configured the query for the CSWP on our category page. We still have to do some configuration to make it display more than three search results, and also give it a "Contoso look." I will show you how to do this in a later post. The next step is to add a CSWP to our catalog item page, and configure the query to show individual catalog items.

 

 

Next blog post in this series Stage 10: Configure the query in a Content Search Web Part on a catalog item page

 

 

Additional resources Configure Search Web Parts in SharePoint Server 2013 Scenario: Create SharePoint sites by using cross-site publishing in SharePoint Server 2013

Comments

  • Anonymous
    January 01, 2003
    How and when "Product Catalog - Product Results" result source got created in the publishing site?Why can't I edit it?

  • Anonymous
    January 01, 2003
    Its a really a good Article !

  • Anonymous
    January 01, 2003
    Hi Lisamariew71, Glad to hear that you are eagerly awaiting the next article :-) Last week and this week I have hit a bit of a snag, but I will do my best to get the rest of the articles out as soon as possible. Bella

  • Anonymous
    January 01, 2003
    Catch my 5 stars rate!

  • Anonymous
    January 01, 2003
    Hi again Lisamariew71, I will publish the next post on May 23rd. Bella

  • Anonymous
    January 01, 2003
    Hi Rothrock,

    I'm sorry, but I'm still a bit unsure about your scenario. Are you trying to set up a search center on your publishing site? If so, have you created a new site collection for where you can configure your search results page?

    Bella

  • Anonymous
    January 01, 2003
    Hi Ravi, Glad to hear that the posts have been useful :-) At the moment, I don’t have an ETA on the next step, but will post a publishing date soon. However, note that in the step on how to customize the catalog item page, I will show you how you can do this by using a Content Search Web Part (I will explain why I want to use a Content Search Web Part and not Catalog Item Reuse Web Parts in the blog post). Also, I will only explain how to set the query in the Content Search Web Part, and not talk about any design elements.   If you are interested in how to customize the automatically created catalog item page, see this article on MSDN:  msdn.microsoft.com/.../dn144674.aspx Hope this helps. Bella

  • Anonymous
    January 01, 2003
    Good Article.

  • Anonymous
    January 01, 2003
    Hi Rothrock,

    Just to be sure, you've set up your sub site on your publishing site, correct? If so, you're definitely on the right path.

    On the sub site, create a new page that you'll use to display your search results. On your publishing site, configure the Search Settings so that queries are sent to your newly created search results page.

    You can configure your new search results page in several different ways. But to make sure that you get search results from your catalog, add a Search Results Web Part to your new page, and configure it to use your catalog result source. If you then enter the query "cameras" on your publishing site, the Search Results Web Part will display only catalog item. By default, the URL that's displayed in the Search Results Web Part, is the URL of the catalog item itself. Most likely this URL will be /DispForm.aspx?ID something. However, when you hover over a search result, notice that the friendly URL is displayed. Also, when you click on a search result, you'll be taken to the item on your publishing site. To change the way your search results are displayed, you have to modify the display template(s) for the Search Result Web Part. For information on how to do that, see this blog series:http://blogs.technet.com/b/tothesharepoint/archive/2013/09/03/how-to-change-the-way-search-results-are-displayed-in-sharepoint-server-2013.aspx

    Now, regarding the other content that you want to display on your search results page. I don't know your content well enough to say if you can use KQL so that all content is shown within the same Search Results Web Part. But, have you considered using different search verticals for the different content? For more information about search verticals, see this blog article:http://blogs.technet.com/b/tothesharepoint/archive/2013/11/13/how-to-add-a-customized-search-vertical-to-your-search-results-page-in-sharepoint-2013.aspx

    Bella

  • Anonymous
    January 01, 2003
    The comment has been removed

  • Anonymous
    January 01, 2003
    Hi Jimbo,

    Just to make sure we're on the same page, you mean the result source that is automatically created when you connected to your catalog, correct?

    If so, have you in anyway made changes to this result source?

    Bella

  • Anonymous
    January 01, 2003
    Hi Zach,

    Have you tried to use the Local People Results result source when you configure the query of the CSWP? This result source will return items from the profile database and the User Profile service application.

    When you have the search results that you want, you'll have to modify the display template. To see how people results are displayed in the Search Center, you can take a look at the Item_Person display template (Site settings --> Master pages and page layouts --> Display Templates --> Search).

    Hope this helps,
    Bella

  • Anonymous
    January 01, 2003
    Any idea when the next article will be available?  I am waiting with bated breath! :-)

  • Anonymous
    January 01, 2003
    Great article! Very useful!!

  • Anonymous
    January 01, 2003
    Hi Muralee, Glad to hear that you like the series :-) At the moment, a seperate series on how to integrate e-commerce is not planned. However, should that change, I’ll be sure to let you know. Bella

  • Anonymous
    January 01, 2003
    Hi, The result source was automatically created when you connected your publishing site to your catalog (http://blogs.technet.com/b/tothesharepoint/archive/2013/03/07/stage-5-connect-your-publishing-site-to-a-catalog.aspx). You can edit the result source by going to Site settings --> Search Result Sources.​ Hope this helps. Bella

  • Anonymous
    January 01, 2003
    Hi Rothrock,

    I'm glad to hear that you got it to work :-)

    It sounds like you have quite a few different data sources. You might be able to combine them and set up a solution that uses your current data set, but you run the risk of this becoming increasingly difficult to maintain when your data set grows.

    I think search verticals are a very good way to "separate" results on a search results page. Depending on how your data is structured, you might be able to filter the different types of search results by using refiners (http://blogs.technet.com/b/tothesharepoint/archive/2013/11/11/how-to-add-refiners-to-your-search-results-page-for-sharepoint-2013.aspx). But that really depends on how your data is structured.

    Bella

  • Anonymous
    January 01, 2003
    The comment has been removed

  • Anonymous
    May 01, 2013
    This whole series of posts has been extremely useful - the only problem with it is that I'm eagerly waiting for you to write step 10 (customizing the category-item pages)!

  • Anonymous
    May 15, 2013
    Bella Nice work, Hope you can churn out whole stuff for e-commerce from SharePoint 2013. Like a connected web part in category page for ale-carte.

  • Anonymous
    July 02, 2013
    Hi Bella, Thanks for this great series! Is there a way to configure the CSWP to collapse and display only 1 result for items with the same title (but different colours)? So instead of displaying 3 results for "Compact Digital Camera M190" (because it is available in 3 colours) - just display 1 result for that item on the category page? Thanks, Bernado

  • Anonymous
    October 07, 2013
    Its a really a good Article !

  • Anonymous
    May 28, 2014
    The comment has been removed

  • Anonymous
    June 25, 2014
    The comment has been removed

  • Anonymous
    June 26, 2014
    The comment has been removed

  • Anonymous
    June 26, 2014
    PS: I have set up a sub site for a search center. I created my own search result source that includes my best guess as to how to combine the KQL from the three individual sources. But I get things from other sites and the catalog items aren't rewritten to look like they are local.

  • Anonymous
    June 27, 2014
    Yes! That is it. Thank you. This is making some sense. Last night I was able to get it working by creating a new source and a basic search center sub-site. But since we have several managed catalogs coming together the KQL for that will probably get unmanagable. I'll try what you have suggested. Do you think it will be easier to combine multiple result sources with this approach?

    The verticals also look very promising and I hadn't even thought of those.

  • Anonymous
    July 10, 2014
    This is a great write up. After reading it got my mind thinking...do you know of a way to use this CSWP to display people results along with their display picture? I am thinking it would make a good dynamic employee directory if I could get the query to return all people results with their picture, title, and dept.

    thanks!

  • Anonymous
    September 01, 2014
    Great series!

    When I attempt to configure the query, the Result Source created for the catalog doesn't show up in the dropdown in Quick Mode, only in Advanced Mode (where I don't have the option to select the Navigation term and its children). Any idea what might be going on with that?

  • Anonymous
    September 02, 2014
    Correct, I mean the Result Source that is automatically created when connecting to the Catalog; it doesn't show up in Quick Mode, only in Advanced Mode when configuring the Content Search Web Part.

    I've made no changes to the Result Source.

    Thanks!

  • Anonymous
    November 02, 2014
    Hi Bella, I would be really grateful if you could guide me for the below query :

    I have a requirement where there we have authoring site and publishing (public facing internet site).

    We have a set of modules and each module has a set of courses.

    The course structure is such that the content authors can upload and download pdf document for each course. But the same structure in publishing site should allow only downloading of pdf documents for users.

    Query1 :In such a scenario, should I use a document library or custom list?
    Query 2: Is it mandatory to have managed metadata column...terms and term sets for list and libraries. Does it contribute anything to the search index?


    Being a sharepoint beginner, I'm really stuck as to where to start from. Please guide me. :(