Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Previously it was not possible to use search APIs in the SharePoint only with add-in only or app-only permissions, but this capability has been included in the SharePoint Online and you can use the search APIs now with add-in only or app-only permissions. This is really interesting capability, which will open up multiple different additional scenarios and there has been pretty high demand for this capability from the community.
When you are using SharePoint Online search APIs with app-only permissions, you will need to request full permissions to the tenant when you are registering the add-in/app for the tenant. This will grant needed permissions to query information from the Office 365 tenant without security trimming applied.
Updated on 5th of August – Please note the updated guidance on providing tenant level permissions. You’ll need to grant these permissions under the tenant admin URL. See following PnP MSDN article for additional details – How to provide add-in app only tenant administrative permissions in SharePoint Online.
Here’s the needed steps to make this happen.
<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="https://sharepoint/content/tenant"
Right="FullControl" />
</AppPermissionRequests>
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Search.Query;
static void Main(string[] args)
{
// Create client context with app-only permissions using
// client id and secret
var tenantAdminUri = new Uri("https://vesaj.sharepoint.com");
string realm = TokenHelper.GetRealmFromTargetUrl(tenantAdminUri);
var token = TokenHelper.GetAppOnlyAccessToken(
TokenHelper.SharePointPrincipal,
tenantAdminUri.Authority, realm).AccessToken;
using (var cc = TokenHelper.GetClientContextWithAccessToken(
tenantAdminUri.ToString(), token))
{
// Just to double check permission setup
Web web = cc.Web;
cc.Load(web);
cc.ExecuteQuery();
string title = web.Title;
// Actual query to be executed. You will need to have
// tenant full permissions when used in app-only mode
KeywordQuery keywordQuery = new KeywordQuery(cc);
keywordQuery.QueryText = "WhateverYouWant";
SearchExecutor searchExecutor = new SearchExecutor(cc);
ClientResult<ResultTableCollection> results =
searchExecutor.ExecuteQuery(keywordQuery);
cc.ExecuteQuery();
// Just to output something
Console.Write("Number of results - " + results.Value[0].RowCount);
// Pause console for showing output
Console.ReadLine();
}
}
Q - Does it work with permissions given directly using Azure Active Directory?
Unfortunately no. Currently this capability is only available if permissions are given using classic ACS based technique by using appregnew.aspx page. Having similar support with permissions given directly from the Azure Active Directory is being looked at, but there’s no exact schedule at the time of writing this blog post.
Q - Does this work in on-premises (SharePoint 2013 & 2016)?
Unfortunate no. This is currently specific capability implemented only for SharePoint Online and it’s not available in SharePoitn 2013 or in SharePoint 2016. Please use the UserVoice by providing us feedback around the requirements for this for on-premises.
Q - Does this model work with SharePoint hosted add-ins/apps?
No. SharePoint hosted add-ins do not support app-only or add-in only permissions. You cannot also provide tenant level permissions for SharePoint hosted add-ins.
Q - What permissions are needed for getting this to work?
To make the search queries work with add-in only or app-only context, app/add-in registration will need to have tenant full permissions like shown in the above sample. Person who will register these permissions for the add-in/app will need to be also tenant administrator.
Q - When permissions are set, app/add-in can then query all the information in SharePoint Online?
This is correct. If you provide add-in or app full permissions for the tenant, including the support or app-only / add-in only access, requests using this context will have full permission to the information in SharePoint Online regardless of the end user permissions.
Techniques showed in this blog post are part of the Office 365 Developer Patterns and Practices (PnP) guidance, which contains guidance and reusable solutions for demonstrating different patterns and practices related on the development for Office 365 and SharePoint on-premises.
Check the details around PnP from dev.office.com at https://aka.ms/OfficeDevPnP. Please join us on sharing patterns and practices for the community for the benefit of the community. If you have any questions, comments or feedback related on this sample, blog post or anything on PnP, please use the Office 365 Developer Patterns and Practices Yammer group at https://aka.ms/OfficeDevPnPYammer.
“Sharing is caring”
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in