Limit KeywordQuery search scope in Sharepoint to only a specific site?

Daniel A Okarma 1 Reputation point
2022-09-06T22:02:03.62+00:00

I have a client application that is connecting to our corporate SharePoint Online site. This application has only been registered on specific sites, not the root tenant (and I don't believe doing so would be an option, for security reasons.) I can do basic functionality in the sites where the application has permission (read, write, etc) but I'm having problems getting the KeywordQuery to works so I can run searches against the document libraries.

Currently, my code for the function to do a keyword lookup looks like this:

        public List<SP.Search.Query.ResultTable> SearchByKeyword(string _keyword)  
        {  
  
            SP.Search.Query.KeywordQuery query = new SP.Search.Query.KeywordQuery(_context);  
  
            query.QueryText = _keyword;  
  
            SP.Search.Query.SearchExecutor searchExecutor = new SP.Search.Query.SearchExecutor(context);  
              
            SP.ClientResult<SP.Search.Query.ResultTableCollection> results = searchExecutor.ExecuteQuery(query);  
  
            _context.ExecuteQuery();  
  
            List<SP.Search.Query.ResultTable> list = results.Value.ToList<SP.Search.Query.ResultTable>();  
              
            return list;  
        }  

NOTE: variable "_context" is defined globally in my class and should be assumed is a valid context object for a connection to my subsite (https://my.sharepoint.site/sites/mysubsite). Using this context object, I can read and write from the document libraries and lists without any problem.

When the code gets to the ExecuteQuery() call, an exception is thrown that reads:

"No User or App Context found"

I've seen other people post about this problem online and it appears that the solution is to make sure that the application has read/write permissions to the SharePoint site. But my app can't have permissions to the entire site and instead must rely on permissions to only specific subsites. I believe the KeywordQuery is querying from the root instead of the subsite that the context object is for.

Has anyone run into something similar or have any suggestions on narrowing the scope of the search? I tried adding " AND PATH:https://my.subsite.path/sites/mysite" to the QueryText property, but that doesn't seem to help--again, I'm assuming that's because the search is actually happening at the root tenant level and not within my own subsite where the application has permissions.

Thanks in advance!

Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,251 Reputation points
    2022-09-07T06:02:00.357+00:00

    Hi @Daniel A Okarma ,

    According to my research and testing, When this error "No User or App Context found" appears, you need to make sure that the application has Sites.ReadAll permissions.

    And you can use the following code to search in a specific site:

    keywordQuery.QueryText = "sharepoint path:https://tenant.sharepoint.com/sites/test";  
    

    Similar issue for reference :
    CSOM: Microsoft.SharePoint.Client.ServerException: No User or App Context found
    CSOM Search limit to single site

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.