Share-n-dipity

SharePoint serendipity is the effect by which one accidentally discovers something fortunate, especially while looking for something else entirely. In this case, it is the occassional musings, observations, and Ouija board readings about the phabulously

Using Query Rules with CSOM Queries in SharePoint 2013

Using Query Rules with CSOM Queries in SharePoint 2013

  • Comments 4
  • Likes

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:  http://social.technet.microsoft.com/Search/en-US?query=%22query%20rules%22&beta=0&rn=Share-n-dipity&rq=site:blogs.technet.com/b/speschka/&ac=8 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 https://www.foo.com; 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("https://www.foo.com"), 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.

Comments
Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment