Home > Blockchain >  Sharepoint Online Search error with CSOM 'No User or App Context found'
Sharepoint Online Search error with CSOM 'No User or App Context found'

Time:10-07

I try to make a basic search using Sharepoint REST API, but I run into this error: view error

The authorization is done by a token created using ClientId and ClientSecret values.

The permission of Azure App are:


  • AllSites.Read
  • AllSites.Manage
  • Sites.Read.All
  • Sites.ReadWrite.All
  • Sites.Search.All
  • Sites.Selected
  • User.Read.All
  • User.ReadWrite.All

Also the application is registered with permissions:

<AppPermissionRequests AllowAppOnlyPolicy="true">
    <AppPermissionRequest Scope="https://sharepointDomain.com/sites/mySite"
                          Right="FullControl" />
</AppPermissionRequests>

If I use SharePointOnlineCredentials for authentification, the search is working, but unfortunately we can use this anymore in .Net 6.0

private static void SearchFile(string folderPath, string searchText)
        {
            using (ClientContext clientContext = new ClientContext(SiteUrl))
            {
                clientContext.ExecutingWebRequest  = (sender, e) =>
                {
                    e.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer "   accessToken;
                };

                //clientContext.Credentials = credentials;

                Web web = clientContext.Web;
                clientContext.Load(web, a => a.ServerRelativePath);
                clientContext.ExecuteQuery();

                KeywordQuery keywordQuery = new KeywordQuery(clientContext);
                keywordQuery.QueryText = searchText;
                SearchExecutor searchExecutor = new SearchExecutor(clientContext);
                ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);
                clientContext.ExecuteQuery();
            }
        }

Any help will be really appreciated!


CodePudding user response:

Microsoft explain in the Using add-in only / app-only permissions with search queries in SharePoint Online article that the search requires this permission :

<AppPermissionRequests AllowAppOnlyPolicy="true">
     <AppPermissionRequest Scope="https://sharepoint/content/tenant"
                           Right="FullControl" />
</AppPermissionRequests>

It's quite a high level privilege requirements. I don't know if you can switch to user context request, but I think it would be preferable.

CodePudding user response:

I´ve tried with this with no luck:

<AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
<AppPermissionRequest Scope="http://sharepoint/social/tenant" Right="FullControl" />
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl"/>
<AppPermissionRequest Scope="http://sharepoint/search" Right="QueryAsUserIgnoreAppPrincipal" />
  • Related