1

The Scenario

I am developing a Sharepoint provider hosted app. the Sharepoint app web is only used as a repository for storing documents in document libraries and The web application use O365 multitenant app to authenticate users by using ADAL Tokens.So all the CSOM calls are executed using app only access token because acquiring the share point user access token is not always possible since user can directly access our web application with out redirecting via sharepoint

The Requirement

I need to provide functionality to the user to search the documents in the document library which is in the app web via Sharepoint search. I'm using CSOM to execute search queries.

The problem.

The access privileges of the sharepoint app only token is not sufficient to access sharepoint serach.

What i have tired

Since getting the user access token is not an option i tried asking more permission in the app manifest.

Querying SharePoint search is possible if the app is granted "Scope:Tenant, Permission : Read".

A Sample Code that i used to test the search functionality

Uri targetWeb = new Uri("AppWebUrl");                     
var responseToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, targetWeb.Authority, targetRealm);
var ctx = TokenHelper.GetClientContextWithAccessToken("AppWebUrl", responseToken.AccessToken);

KeywordQuery keywordQuery = new KeywordQuery(ctx);
keywordQuery.QueryText = keywordQueryValue;
keywordQuery.RowLimit = 500;
keywordQuery.StartRow = 0;
keywordQuery.SelectProperties.Add("Title");
keywordQuery.SelectProperties.Add("UniqueId");
SearchExecutor searchExec = new SearchExecutor(ctx);
ClientResult<ResultTableCollection> results = searchExec.ExecuteQuery("keywordQuery");
ctx.ExecuteQuery();

Questions

  1. Is it feasible to ask this level of permission from a customer when they are installing the app?

  2. Is there any way to access share point search in my scenario other than asking Tenant Read permission?

If any one can shed some light on this it will be highly appreciated

Thank you

2

I don't think there is a way to query Search via app-only permissions. As a workaround you could use stored credentials and execute the Search using that context.

But you need to understand that Search relies on a proper user context to function properly. Unless your security implementation is really simplistic you are likely to see issues where users see documents they don't have access to, or vice versa.

Another thing, it sounds like that by bypassing authentication you are essentially opening up your application to the entire Internet (unless it is behind a firewall). Are you comfortable with the population at large being able to access your content?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.