question

KonstantinHvan-3962 avatar image
0 Votes"
KonstantinHvan-3962 asked TongZhangMSFT-7548 edited

CSOM: Microsoft.SharePoint.Client.ServerException: No User or App Context found

Hi,

I've got the exception when using SharePoint Search API:

Microsoft.SharePoint.Client.ServerException: No User or App Context found

my code works fine on one SharePoint site, but on another it fails with above error.
My code is just a simple search query:

         KeywordQuery keywordQuery = new KeywordQuery(_context);
         keywordQuery.StartRow = startIndex;
         keywordQuery.QueryText = $"IsDocument:1";
         keywordQuery.RowLimit = _searchPagesChunkLimit;
         keywordQuery.RowsPerPage = _searchPagesChunkLimit;
         keywordQuery.TrimDuplicates = false;
         SearchExecutor searchExecutor = new SearchExecutor(_context);
         ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);
         _context.ExecuteQuery();

Please help.
Thanks.

sharepoint-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TongZhangMSFT-7548 avatar image
0 Votes"
TongZhangMSFT-7548 answered TongZhangMSFT-7548 edited

Hi @KonstantinHvan-3962,

I'm glad to hear you solve the problem ,if you have any issue about SharePoint, you are welcome to raise a ticket in this forum.

By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others." and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:


[CSOM: Microsoft.SharePoint.Client.ServerException: No User or App Context found]


Issue Symptom:
I've got the exception when using SharePoint Search API:

 Microsoft.SharePoint.Client.ServerException: No User or App Context found

my code works fine on one SharePoint site, but on another it fails with above error.
My code is just a simple search query:

 KeywordQuery keywordQuery = new KeywordQuery(_context);
      keywordQuery.StartRow = startIndex;
      keywordQuery.QueryText = $"IsDocument:1";
      keywordQuery.RowLimit = _searchPagesChunkLimit;
      keywordQuery.RowsPerPage = _searchPagesChunkLimit;
      keywordQuery.TrimDuplicates = false;
      SearchExecutor searchExecutor = new SearchExecutor(_context);
      ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);
      _context.ExecuteQuery();

Current status:

The application lacks rights, specifically it's Sites.ReadAll


You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community member's to see the useful information when reading this thread. Thanks for your understanding!


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.




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TongZhangMSFT-7548 avatar image
0 Votes"
TongZhangMSFT-7548 answered KonstantinHvan-3962 commented

Hi @KonstantinHvan-3962,

This error means that the user or app context was not found ,please check if the query content exist with the site, and check if you are connected to your site correctly.

More information for reference:
https://docs.microsoft.com/en-us/sharepoint/dev/general-development/using-the-sharepoint-search-query-apis#query-using-the-net-client-object-model
https://docs.microsoft.com/en-us/sharepoint/dev/general-development/sharepoint-search-rest-api-overview


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.




· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @KonstantinHvan-3962,

I am checking to see if the problem has been resolved. If there's anything you'd like to know, don't hesitate to ask.

0 Votes 0 ·

No, the problem has not been resolved yet.
And the answer given is just repeating the exception text. Geniously!
The connection to the site is correct as I can make basic context.ExecuteQuery calls without a problem,
the exception appears only on SearchAPI calls.

0 Votes 0 ·
KonstantinHvan-3962 avatar image
0 Votes"
KonstantinHvan-3962 answered

Well, we've found out what was the problem - it's a lack of rights for the application, specifically it's
Sites.ReadAll
I wish the error message would be more informative.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.