1

I am currently using the SharePoint search rest api on a document library. The search results are coming fine. Now I have a requirement where I want to filter the search results based on the Author of the document that is uploaded.

Below is the query I am using to filter the search results based on current logged in user. The aim is to filter the results from the Created By column

var url = "http:../_api/search/query?querytext='AnyText+AND+Author:{User}+AND+ListId:a87b68a9-06fd-421c-ae0e-c93416e7e612+AND+IsDocument=True'";

This query is not returning any results. However if I pass the username hard-coded the results are coming as expected. e.g.

 var url = "http:../_api/search/query?querytext='AnyText+AND+Author:Test User (Company)+AND+ListId:a87b68a9-06fd-421c-ae0e-c93416e7e612+AND+IsDocument=True'";

So the above query works fine.

So my question is, Is there a way to pass the current logged in user in the Author.

Author:{User}
Author:{User.Title}

Unfortunately the above approach is not working fine as I think the api is not get the current user.

Source: Link 1 Link 2

Thanks in advance..!!

2

You need to use queryTemplate parameter instead of queryText.

Modify your endpoint somewhat as below :

var url = "http:../_api/search/query?queryTemplate='AnyText AND Author:{User}
AND ListId:a87b68a9-06fd-421c-ae0e-c93416e7e612 AND IsDocument=True'";

When you use queryTemplate , SharePoint transforms tokens inside it to a value that is understandable to it i.e {User} get transformed to the current user or {searchTerms} gets replaced with the actual value used in the search box etc.

When you use queryText, it will understand plain text so that's why i guess you need to manually write the actual user name.

Reference - SharePoint Search REST API overview

0

In order to be able to change the query at a moments notice perhaps it would be an idea to either use SourceID or perhaps SourceName / SourceLevel as per

https://www.techmikael.com/2015/01/how-to-query-using-result-source-name.html

  • I don't know how can I use this for my issue – Ankit Katiyar Jul 31 '18 at 13:17
  • Create the proper kql query in a search resultat source and refer to it by source id – Kasper Bo Larsen Jul 31 '18 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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