20 January 2014

Provider Hosted App in SharePoint 2013

Create a windows azure empty web site
Down load the publishing profile (we will be using this to publishing setting to publish the app to azure)
Create a new SharePoint 2013 app in Visual Studio.
Visual studio will create app project and azure web application project in this solution.
The next thing required is to create the Client Id.
Client Id and Client Secret is used to connect Window azure web sites / azure cloud services with Office 365 App.
 In the SharePoint site go to _layouts/15/appregnew.aspx to create client ID , Client Secret and register the app.
The following fields are there in a form , Appid and App Secret are created by clicking the button beside the text box. Title App Domain(will be the windows azure domain you used to create the windows azure web site) and Redirect Url(this will be the full url including default.aspx starting with https://) need to be provided.
AppId,App Secret,Title,App domain and Redirect Url
And then register the new app.
The next thing is to set the client ID in the app manifest and web application config file.
In the web.config we also need to put the client secret.
Note that to put the full url in the Startpage.
We also need to remove the appsettings before publishing to ensure than we will be providing the credential on the fly.
GetContextTokenFromRequest is the key method to get the Context token and to create the context.
TokenHelper class have methods to pass Context  token and get the Refresh token and to pass the refresh token to get the access token.
All this token handling is encapsulated using GetClientContextWithContextToken method of TokenHelper Class.
Once you get the context you can communicate to the web and the SharePoint Objects.
To publish we need to import the profile we downloaded from the azure site after we click publish.