I’ve been learning a few things when working with the SharePoint API while on a SharePoint hosted app. Everything seemingly worked well for most of my REST requests such as GetListByTitle but when it came to search all I could get was a single simple error.

Microsoft.SharePoint.Client.ResourceNotFoundException, with error message ‘Cannot find resource for the request search.’

Basically this will be generated if you call /_api/search/postquery or /_api/search/query while trying to use SP.RequestExecutor. So typical code may go something along the lines of this:

// Initialize your RequestExecutor
var executor = new SP.RequestExecutor(appweburl);
var hostweburl = "http://your.host.web/url";
var appweburl = "http://your.app.web/url";

executor.executeAsync(
	{
	url: appweburl + "/_api/SP.AppContextSite(@target)/web/title?@target='" + hostweburl + "'",
	method: "GET",
	headers: { "Accept": "application/json; odata=verbose" },
	success: function (data) {
		alert("Success");
	},
	error: function () {
		alert("Error");
		}
	}
);

In a hosted app you do not actually use the SP.AppContextSite and @target to do this. You simply run a typical search query (/_api/search/…) against the app web. SharePoint handles the connections to the host web itself assuming that your App’s manifest has correct permissions. Scope must include Search under permissions with permission QueryAsUserIgnoreAppPrincipal.

Note: Provider hosted apps may not have this luxury.

Comments 1

  • Thanks! This really helped me a lot.

Leave a Reply

Your email address will not be published. Required fields are marked *