1

I'm trying to use the sharepoint online search api (/_api/search) from an application. Adding it from the Azure portal I see the search is only in the "delegated permissions" section. When I ran it in testing with the user login and approve it it works well. Since I don't want to need a user to login for this, I found this article

https://blogs.msdn.microsoft.com/vesku/2016/03/07/using-add-in-only-app-only-permissions-with-search-queries-in-sharepoint-online/

That made me believe it would be possible to use search as an app-only and not as a user. I followed all the steps, created the app through appregnew.aspx , I also added another permission via appinv.aspx so the permissions I asked for are the following :

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

I'm testing using ADAL JAVA SDK as follows:

Future<AuthenticationResult> future = context.acquireToken(
                resource, new ClientCredential(clientId,
                        clientSecret), null);

where resource is xxxxxx.sharepoint.com and I'm later using this token as the bearer token.

But when I'm trying to test this I get the following error:

2018-08-05 11:03:22 WARN ODataUtils:120 - Failed to get a successful response for uri [https://XXXXXX.sharepoint.com/_api/search], reason [{"error_description":"The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs."}];

Since this is sharepoint online I don't have a server and I'm not using the .NET framework so what other way do I have to debug this? or other idea what I'm doing wrong here?

Any help would be greatly appreciated.

Shira Ben-Dor
  • 71
  • 1
  • 6

1 Answers1

-2

Maybe you can use the Java to call SharePoint Search Api and show the result(But we need to spend many time to research on this, there are many uncertainties.)

The best choice for you is to use the VisualStudio to test the SharePoint Addin. Microsoft provide more support on it and you can use the ready-made template.

You can use the wide range of search-related APIs that SharePoint offers for search add-ins:
.NET client object model (CSOM) Key libs: Microsoft.SharePoint.Client.Search.dll ; Silverlight CSOM Key libs: Microsoft.SharePoint.Client.Search.Silverlight.dll ; ECMAScript (JavaScript, JScript) object model (JSOM) Key libs: SP.search.js ;

Search REST API http://server/_api/search/query

Some demo code: Client-side Object Model (CSOM) C#

using (ClientContext clientContext = new ClientContext("http://localhost"))
{
    KeywordQuery keywordQuery = new KeywordQuery(clientContext);
    keywordQuery.QueryText = "*";
    SearchExecutor searchExecutor = new SearchExecutor(clientContext);
    ClientResult<ResultTableCollection> results = 
        searchExecutor.ExecuteQuery(keywordQuery);
    clientContext.ExecuteQuery();
}

JavaScript Object Model (JSOM)

var keywordQuery = new
Microsoft.SharePoint.Client.Search.Query.KeywordQuery(context);
keywordQuery.set_queryText('SharePoint');
var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(context);
results = searchExecutor.executeQuery(keywordQuery);
context.executeQueryAsync(onQuerySuccess, onQueryFail);

REST HTTP GET request HTML

http://mylocalhost/_api/search/query?querytext='SharePoint'

HTTP POST request HTML

{
'__metadata' : {'type' : 'Microsoft.Office.Server.Search.REST.SearchRequest'},
'Querytext' : 'SharePoint'
}

Then set the permissions by VisualStudio and "Napa" Office 365 Development Tools

More information on Search add in: https://learn.microsoft.com/en-us/sharepoint/dev/general-development/search-add-ins-in-sharepoint

Seiya Su
  • 1,836
  • 1
  • 7
  • 10
  • 1
    I did paste in the question above the error that I see, "{"error_description":"The server was unable to process the request due to an internal error...}" . I'm not developing on Visual studio. Maybe an initial question would be, is that even possible, to use the query API without a user?? – Shira Ben-Dor Aug 06 '18 at 19:29
  • Basically, SharePoint is for internal users only. So I think for security, we cannot use it without a user.(If we are not dev(but IT pro admin), it seems we can set some external or anonymous user to see the search result, but i forget where i see the document ) – Seiya Su Aug 07 '18 at 00:08