Using Query Rules with CSOM Queries in SharePoint 2013

I’ve had this question from a few people and never had a chance to track down the answer until today.  I’ve written a few times already about query rules and how awesome they are: actually shows 9 postings where I’ve mentioned them in some way or another.  They are pretty freakin’ brilliant, but the question I’ve been getting asked about is whether or not they fire when someone issues a query via CSOM?  Well as it turns out for the most part the answer is yes.  The “for the most part” really means if you are keeping track of where your query rules AND your client context endpoints are, they do. 

For example, suppose you create a query rule in the site collection; as long as you create your ClientContext for CSOM operations using the same URL then any query rules in that site collection will be invoked when you execute your query via CSOM.  So in this scenario you would create your ClientContext something like this (there are a few variations to how you can do it, I’m just trying to demonstrate the Url usage): 

ClientContext ctx = TokenHelper.GetS2SClientContextWithWindowsIdentity(new Uri(““), Request.LogonUserIdentity);

(Note that this code snippet is based on a SharePoint App I wrote, which is why it’s using TokenHelper)

The other thing to remember is that you can have your query rules fire for ANY CSOM call if you create them in the Search Service Application.  All query rules there are automatically invoked when a CSOM query comes in, no matter what site collection the ClientContext is connected to.


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s