System.UnauthorizedAccessException when submitting a Sharepoint API search query request

Andrei Papou 0 Reputation points
2023-10-10T09:28:15.25+00:00

Hi Everyone,

I've created a SharePoint app that I am planning to use in order to retrieve a list of documents from the entire tenant. I am able to successfully generate a token but when I use that token to search for documents (https://contoso.sharepoint.com/_api/search/query?querytext='IsDocument:true') I am getting the following response:

{
    "error": {
        "code": "-2147024891, System.UnauthorizedAccessException",
        "message": {
            "lang": "en-US",
            "value": "Attempted to perform an unauthorized operation."
        }
    }
}

I granted my new app the following app permissions:

<AppPermissionRequests AllowAppOnlyPolicy="true">
      <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="Read"/>
</AppPermissionRequests>

Which always worked before, and it still works with my old apps, but when I create new ones and grant the same permissions, I get 403 Forbidden.
Has anything changed in sharepoint policies, which no longer allows apps to search query requests?

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,827 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,044 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. ChengFeng - MSFT 5,035 Reputation points Microsoft Vendor
    2023-10-11T06:08:37.89+00:00

    Hi @Andrei Papou

    It seems that you are getting an unauthorized operation error while trying to use the SharePoint search API with your app.

    This could be due to several reasons, such as:

    1.Your app does not have the required permissions to access the SharePoint content. You may need to grant your app more permissions, such as Full Control for the site collection or web scope.

    2.Your app has not been trusted by the site collection administrator. You may need to request or approve the app permissions in the SharePoint admin center.

    3.Your app token has expired or is invalid. You may need to refresh or regenerate your app token using the appropriate authentication method56.

    To troubleshoot this issue, you can try the following steps:

    User's image

    Check the permissions of your app in the SharePoint admin center and make sure they match your app manifest file.

    User's image

    Check the status of your app in the SharePoint app catalog and make sure it is approved and installed.

    Check the validity of your app token and make sure it is not expired or corrupted.

    User's image

    Check the syntax and format of your search query and make sure it is valid and supported by the SharePoint search API.

    Hope these are useful to you


    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.

    Best Regards

    Cheng Feng


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.