SharePoint Query Variables

Constant trouble

Mikael Svenson

by Mikael Svenson on 5/26/2014

Share this:

Article Details

Date Revised:

This is the third episode in the series SharePoint Search Queries Explained - The Series. See the intro post for links to all episodes.

The basis for doing query transformations in Result Sources, Query Rules, and search web parts is to specify KQL queries. Doings o dynamically is a big bonus. This is where Query Variables come into play. Knowing KQL syntax and variable usage will enable you to craft a query for just about anything – as long as you also know the managed properties to query against (which is a topic by itself).

As an example the Local SharePoint Results result source is specified as:

As an example the Local SharePoint Results result source is specified as

{?{searchTerms} -ContentClass=urn:content-class:SPSPeople}


author:"mikael svenson"

the final query sent to the search engine will be expanded to 

author:"mikael svenson" -ContentClass=urn:content-class:SPSPeople

As for the {?..} notation enclosing the full statement, this means that if the variables inside this statement are blank or empty then everything between {? and the matching end brace } will be omitted from the expanded query. If an empty query comes in, the final query is blank and not


as everything is removed due to {searchTerms} being empty. This is a very powerful and important notation to know. Use it around any variable which might be empty to ensure a valid query syntax, and I will gives several samples later in this episode as well.

Out of the box query variables

The TechNet article Query variables in SharePoint Server 2013 lists all the out of the box variables available to you. And there are quite a few of them. Below you will see the variables and sample expanded values for each variable.

Note: The exact definitions for the variables can be found at TechNet.

Site and site collection properties

These are variables are context aware to the current site or site collection on the page you are currently viewing. It’s important to note that they use Site/SiteCollection and not Site/Web as is the programmatically equivalent and used in managed property names.


Page, URL token, query string, and request properties

These variables are context aware to the current page’s properties or the URL you are using.


User properties

Any property available on a users profile is retrievable via the {User.} variable prefix.


Term and term set properties

The term variables are related to navigation in SharePoint which is using the term store. In addition they work for item properties which are taxonomy columns.


List and list item properties

List variables are useful when you add search based content on a list view or on an item page. An item page can also be a wiki page or a publishing page.


Other properties


Custom query variables

Using custom development you can insert custom variables to be used in your queries as well. You can look at the MSDN reference for User Segmentation to get an overview of this, or check out my code from SPC12 where I add information about who you follow into variables {FollowedUsers} and {FollowedSites}, which are multi-value variables.

If you want to add custom variables in SharePoint Online, you will have to resort to some JavaScript trickery which I will touch on in the episode about context triggering of query rules.

Variables with spaces or special characters in the values

Values which have spaces or special characters in them will be expanded with quotation marks around them.



expands to


author:"Mikael Svenson"

If you don’t want to enclose the value in quotation marks you can escape the variable with {\



expands to


author:Mikael Svenson

which in the above case would match an Author named Mikael, and any content including Svenson.

Query variables with multiple values

Query variables can contain multiple values, and by using the syntax {|ManagedProperty:{QueryVariable}} or {|{QueryVariable}}

The expansion will be made using the OR operator, and there is no option for AND expansion. If a user has set the Ask me about field on the user profile to: SharePoint, Search and Apps, then the transformation



expands into


((SharePoint) OR (Search) OR (Apps))


Sample 1

The query below will only show AllItems.aspx results for the current site’s default Document library. Note the quotes around the full statement and escaping of the variable.



Sample 2

On a page, show all other pages (omit self) in the same library tagged with term or child term on a taxonomy column named Category.

owstaxIdCategory:{ListItem.Category.IDWithChildren} ListID:{List.ID}  -ListItemID:{ListItem.ID}


owstaxIdCategory:{Page.Category.IDWithChildren} ListID:{List.ID} -ListItemID:{ListItem.ID}

owstaxIdTestCategory:#649bf5dc-b26d-4e63-8c97-43967f08f516 ListID:0c8e9283-ded5-4d4f-bd51-152283a67944 -listitemid:3


owstaxIdCategory:{ListItem.Category.IDWithChildren} path:"{\Site.URL}/Pages/" -path:{Page.Url}

owstaxIdCategory:#649bf5dc-b26d-4e63-8c97-43967f08f516 path:"" -path:""

Sample 3

On a page, show all other pages in the farm tagged with term or child term on a taxonomy column named Category, and boost equal items with a custom factor from the custom property on the term used. If there is no priority property on the term used, the XRANK part is omitted.

owstaxIdTestCategory:{ListItem.TestCategory.IDWithChildren} {?XRANK(cb={ListItem.TestCategory.Priority}) owstaxIdTestCategory:{ListItem.TestCategory}}


owstaxIdTestCategory:#649bf5dc-b26d-4e63-8c97-43967f08f516 XRANK(cb=1) owstaxIdTestCategory:#0649bf5dc-b26d-4e63-8c97-43967f08f516

Sample 4

Show items tagged with the same tag as a users ask me about fields. I’m using the general catch all taxonomy column here, but you can use any custom taxonomy column. I have also added {?…} which would remove the query if the user has no values in that field.


((owstaxidmetadataalltagsinfo:#00cf9414f-84e4-40ff-8cd7-d3e0a5170887) OR (owstaxidmetadataalltagsinfo:#00b88c4ac-8318-4b3f-b6db-2b2e208fc21c) OR (owstaxidmetadataalltagsinfo:#03da2996f-902d-46b9-96a9-610fd848adc7))

Sample 5

Find items which title contains values from a users ask me about.


((title:SharePoint) OR (title:Search) OR (title:Apps))

Sample 6

Pass in a value from the URL query parameter foo.



Pass in a value from the URL query parameter foo. bar

title:”bar bar”

Topic: SharePoint Search 2013

Sign in with

Or register