Site icon @WonderLaura

Content Search Web Part with Query String

In many SharePoint solutions, Query String URLs are very useful in being able to create something like a page or dashboard that displays different content depending on a value used in the URL of the page.  I decided that I wanted to create a web part on each customer site on our internal SharePoint site, to show all documents related to that customer from anywhere in the site collection.  The Content Search Web Part is perfect, and I wanted it to be part of a template, so that just whatever the site was named, that was the keyword that was used.  Also, I wanted to create a company policy web part page that would send a department name as a query string, and have the web part show the appropriate files.

This applies to SharePoint 2013 and Office 365, the enterprise versions.

Both of these types of solutions can be achieved with the Content Search Web Part, but since the web part does not allow Web Part Connections, it is a little more difficult to figure out.  As a matter of fact, I scoured the internet for about 15 minutes and couldn’t find any solutions to this.  So, here’s the answer.

Query String Filter:

  1. In the Content Search Web Part’s web part toolpane, click the Change Query button.
    239-image_c65b9a09-c167-4ba0-a978-23c981a35486_55F581DA.png
  2. I start by choosing Select a Query: Documents and Restrict by app: Don’t restrict results by app.  I want to show all files in the farm that the logged in user has access to.
  3. Click Switch to Advanced Mode (at the top right in the above screenshot).
  4. In the Keyword filter drop-down, choose Value of a parameter from URL.  Click Add keyword filter.  Notice that now the query in the query text box says {QueryString.MyParameter1}.
  5. Change MyParameter1 to your own word.  Mine is going to be called Department, so I simply change it to {QueryString.Department}.  This isn’t the final product, but I want you to see how it works.  Click OK, and then in the Web Part toolpane click OK.
  6. Now change the URL of the page you’re on, to add the query string.  My page is called policies.aspx and is in my Site Pages library: http://rtm.contoso.com/SitePages/policies.aspx
    With the query string, it’s
    http://rtm.contoso.com/SitePages/policies.aspx?department=accounting
    This will show me any file anywhere in SharePoint with the word accounting in it.  I don’t really want that, I want a specific column named Department to be equal to accounting or marketing or whatever.
  7. It helps that my column, Department is a site column, because once search indexes it, it will show up where I need it in this next step.  Go back into the web part toolpane and click Change Query again.
  8. Click the Property Filter drop-down, and choose Show all managed properties.
  9. Choose Department and then choose Equals, and then look at all of the options here!  I encourage you to play around and try some of these, but for this specific example, choose Value of a parameter from URL.  Click Add Property Filter.
  10. So, now change the previous query to this instead:
    (IsDocument:”True” OR contentclass:”STS_ListItem”) Department={QueryString.Department}

    Notice that now the web part will show items where the value of the department field, which is a site column used across many libraries in this case, is equal to the word you put in the query string.

  11. Now when you save all these changes and view your page with the full URL with a query string, you see see the data change as you change the query string in the URL (as show in step 6)

Token From URL Filter

I have customers, and I have a sub-site for each customer, which has information about the projects we are working on, with associated files, notes, etc.  We are not using managed metadata or any kind of fancy navigation settings, these are just team sites.  I want each of these sites to have a web part that will show other related customer documents.  For example, the project managers keep all statements of work in a SOW library in another top level site, and there is other such information like that which is not stored on the customer sub-site.  The content search web part can be used to show data just pertaining to some information in the current URL, not necessarily the query string.  I name each site with the name of the customer, and I’m sure not to mash together words.  If the customer is something like Adventure Works, I use the URL Adventure_Works and not something like AW or AdventureWorks.  The search engine needs to be able to match these up.

Here’s how:

  1. Repeat steps 1 through 3 above.
  2. In the Keyword Filter drop-down box, choose Value of a token from URL.  Click Add Keyword Filter.
  3. So what the heck is a token? It’s a piece of the URL, counting backwards from the end.
    So, if my URL is:
    http://rtm.contoso.com/sites/projects/adventure_works/sitepages/default.aspx
    Here are the tokens:
    • Token 1 = default.aspx
    • Token 2 = sitepages
    • Token 3 = Adventure_works
    • Token 4 = projects
  4. Since Token 3 is the name of my customer, I change the query to say {URLToken.3}.  Click OK.
  5. You can also test this by looking at the TEST tab in the query builder.  If my site name (customer) is LSU, you can see that it shows lsu as part of the query text.

Now you’ve seen a couple of different ways to configure the query in the Content Search web part.  Here are some additional related references:

I wrote a series of 4 blog posts a while back, explaining what query strings are and some ways to use them:

Here’s my post (with video) about the Content Search web part: SharePoint 2013 Web Part- Content Search

Also, on our weekly SharePoint Power Hour live show, we have covered the content search web part several times:

SharePoint Power Hour Episode 47- Search Center  – Duration- 47-01

SharePoint Power Hour- Episode 4 – Search features in SharePoint 2013  – Duration- 56-16

SharePoint Power Hour Episode 30- SharePoint 2013 Search Discussions  – Duration- 43-06

SharePoint Power Hour Episode 54 – Search Part 2  – Duration- 57-31

SharePoint Power Hour Episode 17- Guest Mark Watts joins us to talk about Display Templates in 2013

SharePoint Power Hour Episode 50 – Project Site rollups  – Duration- 1-01-31


Exit mobile version