Skip to content

Instantly share code, notes, and snippets.

@chrisobriensp
Created July 10, 2014 21:07
Show Gist options
  • Select an option

  • Save chrisobriensp/220f7f1b5cdbb8067474 to your computer and use it in GitHub Desktop.

Select an option

Save chrisobriensp/220f7f1b5cdbb8067474 to your computer and use it in GitHub Desktop.
SharePoint .NET CSOM code sample showing how to get ClientContext using "app only" authentication. (N.B. the app permissions must allow this)
// obtain web URL somehow - line below is fine for a list item RER, but you'll need another approach elsewhere..
string webUrl = properties.ItemEventProperties.WebUrl;
Uri webUri = new Uri(webUrl);
string realm = TokenHelper.GetRealmFromTargetUrl(webUri);
string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, webUri.Authority, realm).AccessToken;
using (var clientContext = TokenHelper.GetClientContextWithAccessToken(webUrl, accessToken))
{
// do CSOM stuff here..
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment