Vince365

Anonymous access for SharePoint autohosted apps

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_image002

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

Cheers

Facebook Like
Anonymous