Anonymous access for SharePoint autohosted apps

Mar 12, 2014

These days I’ve been working on an auto hosted app for SharePoint (office 365 actually).

I had to grant an anonymous access to one of the pages without requiring clients to give anonymous access to their SharePoint site. But I still needed to read a few properties from the App Web web site (the SPWeb provisioned by SharePoint when installing an app)

I had to play around a lot but I finally found the solution:

//uri of the app web, you can pass this url via a query string parameter

var targetUrl = new Uri("https://<tenantname>-<appinstallationid>.sharepoint.com/<appname>");

var token = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, targetUrl.Authority, TokenHelper.GetRealmFromTargetUrl(targetUrl));

using (var ctx = TokenHelper.GetClientContextWithAccessToken(targetUrl.ToString(), token.AccessToken))

You also need to add the “allow app only calls to SharePoint” in the manifest.

clip_5F00_image002_5F00_thumb_5F00_2012688F.jpg

I hope it’ll save you a lot of time.

Cheers


Last edited Sep 6, 2021 by Vincent


Tags: