This article on provider hosted application in SharePoint 2013 explains each and every step involved in creation of a Provider hosted App, one of the new App hosting Model available in SharePoint 2013.
Apps in SharePoint
Basically in SharePoint, the application can be hosted by 3 types.
a. SharePoint Hosted Application.
b. Auto Hosted Application.
c. Provider Hosted Application.
Let us concentrate on the Provider Hosted Application. The remaining, I am planning to write a separate article.
Provider Hosted Application
Provider Hosted Application is very useful to re-use the existing .Net application with very minor changes and their hosting environment. The topology of the Provider Hosted Application is as follows.
The user calls the SharePoint server and the SharePoint portal intern calls the .Net Application as a Separate Application. Moreover, there is an option to create an App Part and loads the .Net Application inside the App Part which can be placed on the SharePoint Page.
With this brief introduction about the App Development Model, we can move on to the creation of Basic Provider Hosted Application using Visual Studio 2012.
Steps to Create a Provider Hosted Application:
1. Open the Visual Studio 2012 as Administrator
2. Click New Project.
3. Select the Template App for SharePoint 2013.
4. On the Creation of the Solution, Visual Studio will ask for the Site Collection against which we are going to deploy our app. And on the same screen, we need to choose the type of hosting which we are planning. In our case, it is going to be Provider Hosted Application.
5. On the selection of Provider Hosted, Click Next. The below screen will be asking the Certificate.
6. The Certificate needs to be created on the SharePoint machine and pfx file needs to be exported and shared with the Visual Studio Machine. Creating a certificate on the SharePoint machine can be covered on a separate article. As of now, I am assuming that, we have a certificate created and an Issuer ID has been associated with the certificate.
7. Now, the Solution has been created. The basic solution is as follows.
8. Our solution will comprise of 2 projects.
a. App Project
b. AppWeb Project
9. App Project – This is going to be deployed on the SharePoint.
10. AppWeb Project – This is going to be the .Net Web Application. This application can be hosted on any IIS.
11. Go to the Property of the AppWeb project and make sure that the Target Framework is set to 4.5.
12. Create the virtual directory will be helpful to host our AppWeb on the Local IIS, instead of using the IIS Express, let us host our application on the IIS itself.
13. By Clicking the “Create Virtual Directory” button on the Web Tab, virtual directory will be created on the local IIS. You can confirm this by going to inetmgr.
14. After the property configurations, we are ready with our Provider Hosted Application. Rebuild the solution to make sure everything is fine.
15. Go to the AppManifest.xml in the App project. AppManifest file will be looking like this.
16. Modify the Start Page attribute to point the IIS directly. Please refer the below figure.
17. Go to Permissions Tab. Add the corresponding scope and the permissions as per the requirement. In this demo, I am adding the web as full control. This will give the rights to do CRUD Operations over the list present with in my web.
By doing this, we are sure that our app is having the Full Control Permission through the Web Scope.
18. The AppManifest.xml will be like this.
<?xml version="1.0" encoding="utf-8" ?>
<RemoteWebApplication ClientId="*" />
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="FullControl" />
19. Now, we came to the final step of our provider hosted application creation. That is, we need to register our clientID with the sharepoint before deploying the app.
20. ClientID is nothing but a GUID. But the only special thing about the ClientID is, it should be small case. i.e., some thing like 24576c92-961f-442b-a866-e612222cad36
21. To generate the GUID, we can’t use the “Create GUID” option in the Visual Studio. Because that will generate the GUID with Upper Case too. But, it will not work for our scenario.
22. Hence, from our sharepoint site itself, go to the AppRegNew.aspx page. The page will present inside the layouts folder. The URL would be something like https://MyServer/sites/MySiteCollection/_layouts/15/Appregnew.aspx.
23. The page will look like
24. By clicking the “Generate Button” of the App ID, ClientID can be generated.
25. Copy the GUID and we need to paste that in 2 places.
a. Web.Config File – App Settings.
<add key="ClientId" value="1de402c2-911a-47f5-8b51-fd8b57144c41"/>
<add key="ClientSecret" value="7Q1y02pvvWMBW7fzlAEnHsSGGATFWra1YEFCIo117sg="/>
<add key="ClientSigningCertificatePath" value="C:\MyCertificate.pfx"/>
<add key="ClientSigningCertificatePassword" value="****"/>
<add key="IssuerId" value="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"/>
b. AppManifest.xml – AppPrincipal
<RemoteWebApplication ClientId="1de402c2-911a-47f5-8b51-fd8b57144c41" />
26. After updating the ClientID, we can build and Deploy the solution. Right click the solution and click Deploy.
27. You will get the following error. Don’t panic about the error. Still we haven’t completed our app development. Still there is one last thing we need to do after getting this error.
28. To rectify this error, we need to register our ClientID on the SharePoint Farm. We can do this by using powershell on the SharePoint Farm.
// Registering App principal
# set intialization values for new app principal
$appDisplayName = "Sathish.App"
$clientID = "1de402c2-911a-47f5-8b51-fd8b57144c41"
$targetSiteUrl = "https://MyServer/sites/MySiteCollection/"
$targetSite = Get-SPSite $targetSiteUrl
$realm = Get-SPAuthenticationRealm -ServiceContext $targetSite
$fullAppPrincipalIdentifier = $clientID + '@' + $realm
Write-Host "Registering new app principal"
$registeredAppPrincipal = Register-SPAppPrincipal -NameIdentifier $fullAppPrincipalIdentifier -Site $targetSite.RootWeb -DisplayName $AppDisplayName
$registeredAppPrincipal | select * | Format-List
$registeredAppPrincipal | select * | Format-List | Out-File -FilePath "Output.txt"
Write-Host "Registration Completed"
29. On the successful registration, we will get the output like,
30. Now, go back to the Visual Studio and do deploy once again. This time, we will not be getting any error message. The site will be opened like below.
31. Click on Trust It. The app will get installed. We can launch the App from our SharePoint Portal.
With this we are now familiar with creating a Provider Hosted Application. Further on the next article, we will have a look at how to convert this into a Claims Aware Application.