Click here to Skip to main content
13,197,200 members (57,553 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

15.6K views
1 bookmarked
Posted 5 Oct 2012

Create Client Context To Access SharePoint 2013 (Office 365 Preview) in AutoHosted SharePoint App Model using SharePoint Client Object Model (CSOM)

, 5 Oct 2012
Rate this:
Please Sign up or sign in to vote.
How to create client context to access SharePoint 2013 (Office 365 preview) in AutoHosted SharePoint app model using SharePoint Client Object Model

If we are using SharePoint app model AutoHosted Environment, we need to use Client Context model to access SharePoint.

Therefore, first you need to get a Valid Access Token from SharePoint Server. For that, you need to pass the SharePoint Site Url (This is available as SPHostUrl in the Query String) and Context Token that we can generate from passing the request Object.

TokenHelper class provides methods that can be used to access the SharePoint server and generate Access Tokens.

image

  1. Get the Context Token by passing the HttpRequest.
    String context = TokenHelper.GetContextTokenFromRequest(Request);  
  2. Then get the Uri from the query string which provides the path to SharePoint server.
    Uri SharePointUri = new Uri(Request.QueryString["SPHostUrl"]);
  3. Then validate and generate access Token.
    SharePointContextToken contextToken = 
       TokenHelper.ReadAndValidateContextToken(context, SharePointUri.Authority);
    String AcessToken = TokenHelper.GetAccessToken(contextToken, sharepointUrl.Authority).AccessToken;
  4. Finally, get a ClientContext by passing Url and AccessToken.
    ClientContext ClientC = 
    TokenHelper.GetClientContextWithAccessToken(sharepointUrl.ToString(), AcessToken);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Melick
Sri Lanka Sri Lanka
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171020.1 | Last Updated 5 Oct 2012
Article Copyright 2012 by Melick
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid