I have a Content Search Web Part, and I want to set it to search in a particular library.
I see that when I write a relative path in the query - I do not get results, only when I write the full path of the library - I get results.

Full path is problematic for me, because when I will move the site to the production environment I would have to change the address of the site to the new address in all my CSWPs.

Does it have any solution?

I want to write the query like this:


But it only works if I write it like this:

  • SharePoint has a crawled property ows_SiteName(Text), you can create a managed property on this crawled property for example SiteName and the add SiteName:yourSiteName to your query :) EDIT: It also has ows_SPSiteURL if you need :)
    – Gintas K
    Oct 24, 2013 at 13:07
  • @GintasK - Can you explain exactly how to use it in my query?
    – banana
    Oct 28, 2013 at 6:59
  • Have you tried using tilde (~)? Like this: path:"~/MySubsite/MyLibrary"
    – user2536
    Oct 28, 2013 at 7:21
  • @DanielZiga - It does not return results even with tilde.
    – banana
    Oct 28, 2013 at 7:46
  • if it works with path:"http://MySite/MySubsite/MyLibrary" it should work with path:*/MySubsite/MyLibrary I think. just add * and remove the quotes
    – Gintas K
    Oct 28, 2013 at 14:14

5 Answers 5


Having a similar challenge as you in regards to prepping for cutover from staging to production, but only when I try to use multiple path statements (see Multiple, Relative "Path:" in SharePoint 2013 CSWPs)

To return a single result, try:




Haven't tried the {Web.url} variable Carl mentions, but {SiteCollection.URL} and {Site.URL} both work for me. Also, as you noted, neither the asterisk or tilde syntaxes work for me either.

  • Thank you. I don't know why, but your suggestion as well as Carl's suggestion - Working well for me on one environment, and do not work at all on other environment. Strange...
    – banana
    Jul 31, 2014 at 8:12
  • Hmmm, do you have a mixed HTTPS/HTTP environment, or are you solidly one or the other? What happens, you just get zero results in one of your environments? Is it that none of the {Web.URL}, {Site.URL}, or {SiteCollection.URL} techniques work in one environment, and they all work in the other, or is it a mixed bag of results where like {Web.URL} works in one environment but not {Site.URL} or {SiteCollection.URL}, and {SiteCollection.URL} works in the other environment but not {Site.URL} and {Web.URL}?
    – Precursive
    Aug 1, 2014 at 15:06
  • BTW, I finally trial and errored out the syntax for retrieving multiples --- path:{Site.URL}(path1/patha OR path2/pathb) seems to work.
    – Precursive
    Aug 1, 2014 at 15:08

You will need to extend the CSWP in code to do this, and set the 'Query Text' attribute. As you've found there is no way to do this using the query builder OOTB

  • Per other answers there is a way to do exactly this.
    – kitsu.eb
    Sep 6, 2019 at 17:47

There is the {web.url} query variable. Maybe this is applicable?

Refer to the following TechNet article: http://technet.microsoft.com/en-us/library/jj683123(v=office.15).aspx


You can use listID insted of Path :

{searchboxquery} ListID:Your_Document_Library_ID
  • But listID is not identical in all environments, We will have to change this value when moving to production.
    – banana
    May 4, 2016 at 5:03
  • It works If you use backup/restore when moving to production. @banana May 12, 2016 at 15:12

I am also working on a similar activity to write relative URLs in CSWP so that I won't have to update the URLs again when site is moved to Production environment.

As per the inputs provided by @Precursive here, the query variable {SiteCollection.URL} does work but it returns the URL value in double quotes. So I initially used it as given below in my CSWP.


SharePoint translates it to

path: "http://mysiteURL"path1/patha/

Please note that we don't see any error and in fact results are also returned. But this is misleading(at least in my case). This is because it doesn't return results matching exactly with the path we desire i.e. "http://mysiteURLpath1/patha/" instead we get a lot of results which are out of scope of this path as well. And it was not acceptable in my case.

Thanks to Carl in 't Veld for sharing the MSDN article link explaining what are different query variables and how do to work them. As per this article, in order to get the query variable value without double quotes, we need to use an escape character in query variable. So I have changed my relative query as given below:


and SharePoint translates it to

path: "http://mysiteURL/path1/patha/"

which returns exactly the same results which you would get if you were using absolute path. Hope this helps someone in future.

  • thank you Sagar! if i didn't scroll down i would have the incorrect result set.
    – nirav
    Oct 12, 2018 at 12:51
  • Thanks, this worked for me. To others: you can go to the test tab to see the fully expanded URL.
    – kitsu.eb
    Sep 6, 2019 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.