Skip to content

Instantly share code, notes, and snippets.

@chrisobriensp
Created December 14, 2014 18:21
Show Gist options
  • Select an option

  • Save chrisobriensp/58a3be48cac4b539316e to your computer and use it in GitHub Desktop.

Select an option

Save chrisobriensp/58a3be48cac4b539316e to your computer and use it in GitHub Desktop.
Extract from an MVC controller which authenticates to SharePoint and uses .NET CSOM code..
// code taken from an MVC controller which authenticates to SharePoint and uses .NET CSOM code. The SharePointContext and
// TokenHelper helper classes are added by Visual Studio..
[SharePointContextFilter]
public ActionResult Index()
{
User spUser = null;
// this method calls various methods on the TokenHelper class which is also added..
var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
using (var clientContext = spContext.CreateUserClientContextForSPHost())
{
if (clientContext != null)
{
spUser = clientContext.Web.CurrentUser;
clientContext.Load(spUser, user => user.Title);
clientContext.ExecuteQuery();
ViewBag.UserName = spUser.Title;
}
}
return View();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment