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.
    239-SNAGHTML2f4a67b_55F581DA.png
  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}.
    239-SNAGHTML2f63699_55F581DA.png
  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.
    239-SNAGHTML300e500_03E2D493.png
  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.
    239-image_7294a35a-2e01-422f-9db2-49dcc9f1b2f1_03E2D493.png
  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.
    239-SNAGHTMLe8d453_03E2D493.png
  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.
    239-SNAGHTML111b35c_03E2D493.png

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

Advertisements

16 comments

  • Hello Laura,

    This post is really very good and I found it very useful.

    Thank you very much for sharing this

    Like

  • Hi

    I had a request to do a multi blog rollup. I tried using the content search webpart. I managed to get the titles of the multiple blogs showing up but could not find a managed property to display any of the body of the bog posts. Any idea if this is even possible?

    Thanks,
    Alon @alongoldberg

    Like

    • I’m not sure, I usually just have to click around and try each of those available fields to see what they display. I would think that the body would be one of them, but maybe it’s got a different name. You can look through the search schema for stuff like that.

      Like

  • Laura,

    I have a question which is little out of the track, I am trying to create the “auto complete sharepoint list”, is there any way or what you best suggest me.

    Like

  • Hi Laura,

    im trying to build a querry that will take the latest changed documents from 3 different pages.

    This is my string:
    (path:”https://mysite/Management
    20System/Managementprocesses” OR path:”https://mysite/Management%20System/Mainprocesses” OR path:”https://mysite/Management%20System/Activities” )
    (FileExtension:doc OR FileExtension:docx OR FileExtension:xls OR FileExtension:xlsx OR FileExtension:ppt OR FileExtension:pptx OR FileExtension:pdf) (IsDocument:”True” OR contentclass:”STS_ListItem”)

    on orting tab i sort for latest changed. But im getting random results only… am i doing something wrong?

    Like

    • Sorting always acts strange for me as well, so I’m not sure what to tell you about that one. The column called “lastmodifiedtime” is the one I usually use.

      Like

  • Sorting is a known “bug”. See my post http://techmikael.blogspot.no/2013/06/how-to-change-default-sort-of-search.html for how to work around it.

    Like

  • Thanks for the clear explanation. However, if the value in my query string is multiword then no results are returned. What do you advise?
    Example:www.site.com?Toy=Teddy bear
    thanks,

    Like

  • Laura, I have question. I am trying to build a url from the userprofile
    properties.

    ==============
    {searchTerms} path:”https://mycourtdocumentsdev.usa.doj.gov/Pacer/home/”
    ======================

    I have several Doc Lib named ( MD, TX, NY). Our user profile Department property has also (MD,TX,NY).

    I want to build https://mycourtdocumentsdev.usa.doj.gov/Pacer/home/MD

    I can get the UserProfile Department using {User.Department} (value “MD”)

    Question:

    How can I concatenate {User.Department} to make it
    path:”https://mycourtdocumentsdev.usa.doj.gov/Pacer/home/MD”

    Thanks, Utpal

    Like

    • Hi Utpal, I think you’d have to create a custom display template to accomplish that. You’d have to use code to concatenate them together in the HTML.

      Like

  • Hi Laura,

    We use this to aggregate user tasks throughout our 2013 site and it works great. We use a query to pull user tasks that are assigned to the current user and not “Completed”. But the issue we are finding is that when you complete a task it does not fall of the list right away because this it is dependent on the search crawl. We have continuous crawl set up but it still takes up to 15 minutes. Any ideas?

    Also, we have found that the User Tasks and Site Task Aggregator web parts do not work with lists created in 2013. And they slow the page load down dramatically.

    Thanks for any help you may be able to provide!
    Mark
    @Marx707

    Like

    • Yes, there is not a way to make it instantaneous, it’s based on the last crawl, and 15 minutes is normal for the continuous crawl. I agree about the old user tasks and the aggregator web parts. They are very old and not compatible with those new list types. Don’t use those old web parts, they’re old, clunky and inefficient.

      Like

  • Hi Laura I hope you are well I followed your example for URL query string but did not work for me. I am wondering why..

    Like

  • Hi Laura, You have a great post about the use of the query string. I used it to find the right list, and find the right item, but it shows only a link to the specific item. I like to show some fields of that item and not the link. Background:
    – In O365 SharePoint Online I created a teamsite where the name of that site is a customer name. So the customer name is and now the teamsite has the URL …/SharePoint.com/sites/…/
    -At the homepage of this teamsite I have to show information from a certain list ../Lists/Customers, which exists of customer information as name, address, etc. The info to show is customer name, address etc.
    -Therefore I use the CQWP, where the filter must be like Customer Name is equal to …
    -I don’t know how to extract the customer name from the URL or to find the field that represents the customer name on the specific teamsite. And use that in the filter as explained.

    May you know a next step without developing some jquery/java code?
    Thnx for your response!

    Like

Leave a reply or question

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s