1

In the past months i was developing provider hosted apps for Sharepoint 2013 online. Everything worked fine until i run into the following problem: in some cases for my customer it's not acceptable to start the provider hosted app from inside of Sharepoint (Site Contents / Clicking on the app's tile).

One of my tasks would be:

My customer has a corporate website (not hosted on Sharepoint). Once a form on this website is submitted by a visitor I should write an item into a Sharepoint list in the app web or the host web. Of course in this case nobody is logged in into Sharepoint, so the web part of the provider hosted app should be able to write data to a Sharepoint list without lauching it from inside Sharepoint, without a logged in user.

What do you think, is it possible? Maybe i could use app-only auth?

Thank you very much!

  • app-only still requires a valid user to be authenticated. – wjervis Sep 21 '15 at 16:42
  • @wjervis So you say, that a valid user is always needed when accessing data on Sharepoint? I thought that an app's Client Id and Client Secret in itself could be enough. And that means that there is no such Sharepoint app wich can be lauched / accessed outside of the host web it was installed? – Mpk1 Sep 21 '15 at 17:23
2

Yes, you can use app-only policy to access SharePoint Online. First step is to allow it in your app.manifest.

Second step may be little tricky. I did it about a year ago and it required to modify TokenHelper.cs file.

My case was simple, as I was making exclusively app-only calls. I did not use SharePointContext or SharePointContextFilterAttribute, so I removed them from my code. I relied on TokenHelper to create ClientContext.

I see now, that with current version of TokenHelper.cs no modifications are required. I've created new SharePoint App project in Visual Studio, removed files mentioned earlier and edited HomeController. user.Title returns "SharePoint App".

public ActionResult Index()
{
  var uri = new Uri("https://sample.sharepoint.com/sites/sample/");
  var accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, uri.Authority, TokenHelper.GetRealmFromTargetUrl(uri));
  using (var context = TokenHelper.GetClientContextWithAccessToken(uri.ToString(), accessToken.AccessToken))
  {
    var web = context.Web;
    var user = web.CurrentUser;

    context.Load(user, u => u.Title);
    context.ExecuteQuery();

    ViewBag.UserName = user.Title;
  }

  return View();
  }
}
  • 1
    This still requires that a user is authenticated in SharePoint. OP is asking how to accomplish without having the user log into SharePoint; from an external web app. – wjervis Sep 21 '15 at 17:13
  • @wjervis I'm aware what is the question. This was exactly the case with my application. I was creating sites from outside of SharePoint using app-only policy. All comunication was originating from my application, without users ever loggin in to O365. – dstarkowski Sep 21 '15 at 17:17
  • @dstarkowski Thank you for your answer. Maybe you can share the source code of your solution? – Mpk1 Sep 21 '15 at 17:32
  • @dstarkowski One final question left: how to access app web (a list in the app web) with your soultion? Is itt possible? – Mpk1 Sep 22 '15 at 11:13
  • Ok, i figured it out :) The uri must be changed to the app's uri :) (Which the code won't automatically get, becasue the app isn't lauched from inside of Sharepoint, so there is no redirect and no query parameters. You have to enter it manually.) – Mpk1 Sep 22 '15 at 12:21

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.