SharePoint 2013: Provider Hosted App Development Prerequisites

Recently I got rolled off from a SharePoint 2010 project for few weeks. So, I decided to spend some of my free time on SharePoint 2013. Since apps are a new concept in SP 2013 I decided to create some sample apps. I did not make any production apps as I am yet to get allocated on a project which uses SP 2013 apps. In this series of blog posts I will try to share my leanings. 
This series will consist of following posts:
Develop SharePoint 2013 apps with NAPA
Access data in Host web
Isolated App domain Setup Issues
Create SharePoint Hosted App in Visual Studio
Provider Hosted App Development Prerequisites
App Event Receivers
Remote Event Receivers
This post will describe how to set up development environment for Provider Hosted apps also know as High Trust SharePoint Apps for on-premise development. The assumption is that you have already created an isolated app domain. Following are the steps involved:
  • Open IIS manager on the SharePoint Server and double click on Server Certificates in IIS section.
  • Click on Create Self-Signed Certificate.
  • Specify Friendly Name of the certificate as "HighTrustSampleCert". 

IIS 7

IIS 8

  • The certificate will appear in the list of Server certificates.
  • Go to windows explorer and create a folder named C:\Certs.
  • In Server Certificates, right click on the certificate just created i.e. HighTrustSampleCert and click on Export.
  • In Export To, provide the path created earlier and give file a name like "HighTrust". The file has .pfx extension.
  • Provide the Password. For example, pass@word1.
  • Double click on the certifictae "HighTrustSampleCert" in Server Certificates.
  • Click on Copy to File button at the bottom right to start export wizard.
  • Select the option No, do not export the private key and click Next.
  • Select default values and click Next.
  • Browse to C:\Certs and name the file HighTrust.cer and click Next.
  • Finally click on Finish.
  • The message box with message export was successful will appear.
  • In Central Administration, create a site collection based on Developer Site template. The app will be deployed to this site. 
  • Log in to SQL Server and give db_owner permissions in the content database of the site collection to the user.
  • Now run the following PowerShell commands to configure SharePoint 2013 to use certificates
$publicCertPath = "C:\Certs\HighTrust.cer"
$issuerId = "11111111-1111-1111-1111-111111111111"
$spurl = "http://ais.contoso.com"
$spweb = Get-SPWeb $spurl
$sc = Get-SPServiceContext $spweb.site
$realm = Get-SPAuthenticationRealm -ServiceContext $sc
$certificate = Get-PfxCertificate $publicCertPath
$fullIssuerIdentifier = $issuerId + '@' + $realm
New-SPTrustedSecurityTokenIssuer -Name "High Trust" -Certificate $certificate -RegisteredIssuerName $fullIssuerIdentifier IsTrustBroker
New-SPTrustedRootAuthority -Name "HighTrust" -Certificate $certificate 
iisreset
  • Replace the $issuerId with any GUID but it should be in lower case. Also, replace $spurl with the name of a site which should be based on developer site template. 
  • If SSL is used this step can be skipped otherwise run the follwoing powershell commands to disable HTTPS requirements.
$serviceConfig = Get-SPSecurityTokenServiceConfig
$serviceConfig.AllowOAuthOverHttp = $true
$serviceConfig.Update()

Creating a sample Provider Hosted App:

  • Open Visual Sudio 2012/2013 and create a project of type App for SharePoint 2013.
  • Give a name to the project and click Ok. Provide the developer site url and select Provider-hosted app.

  • Click Next and select "Use a certificate" option. Browse to the location where the certificate was stored and enter the password and GUID created during setting up environment for Provider hosted apps above and finally click Finish.
  • Notice that two projects are created, a SharePoint app project and a web application project. Select web application project and press F4. If SSL is disabled using the above PowerShell scripts, then change the setting SSL Enabled to False. 
  • Click on Start or press F5 to deploy the app to SharePoint. You will be asked if you trust the application. Select Trust It and then enter credentials if prompted.
  • The web page will appear with title of the web as shown.
  • If you open the code behind of the Default.aspx page, it contains the code to write the Title of the web.
using (var clientContext = TokenHelper.GetS2SClientContextWithWindowsIdentity(hostWeb, Request.LogonUserIdentity))
{
    clientContext.Load(clientContext.Web, web => web.Title);
    clientContext.ExecuteQuery();
    Response.Write(clientContext.Web.Title);
}
  • Till this point no "AppWeb" is created in the SharePoint as there is nothing in the SharePoint project apart from icon file and the Manifest file. Right click on SharePoint app project and select Add - New Item.
  • Select Module and name it Pages and click OK.
  • Rename the Sample.txt file created by the module and name it Start.aspx.


  • Remove the contents of the file and copy below markup to the page.

<%-- The following 4 lines are ASP.NET directives needed when using SharePoint components --%>

<%@ Page Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" MasterPageFile="~masterurl/default.master" Language="C#" %>

<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
    Provider Hosted App
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="PlaceHolderMain" runat="server">

    <div>     
           Welcome to provider hosted app!
    </div>

</asp:Content>
  • Also, observe that after creating a module a Feature is created in the SharePoint app project. Open the feature and make sure that the Pages module must created is in "Items in the Feature" section. If it is in "Items in Solution" then move it to "Items in the Feature" section.
  • Now go to AppManifest.Xml file and change the start page to start page.
  • Press F5, trsut the app, provide credentials and finally the new app page will show up.