Step by Step approach to create a Provider Hosted Application in SharePoint 2013

Sathish Nadarajan
 
Solution Architect
April 8, 2013
 
Rate this article
 
Views
128241

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.

clip_image002[6]

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.

clip_image004[4]

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

clip_image005[4]

2. Click New Project.

3. Select the Template App for SharePoint 2013.

clip_image007[4]

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.

clip_image009[4]

5. On the selection of Provider Hosted, Click Next. The below screen will be asking the Certificate.

clip_image011[4]

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.

clip_image013[4]

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.

clip_image015[4]

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.

clip_image017[4]

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.

clip_image019[4]

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.

clip_image021[4]

16. Modify the Start Page attribute to point the IIS directly. Please refer the below figure.

clip_image023[4]

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.

clip_image025[4]

clip_image027[4]

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" ?>
 <!--Created:cb85b80c-f585-40ff-8bfc-12ff4d0e34a9-->
 <App xmlns="http://schemas.microsoft.com/sharepoint/2012/app/manifest"
      Name="SathishApp"
      ProductID="{1d3effd5-13fc-4551-9e45-9b6ad4ea5a18}"
      Version="1.0.0.0"
      SharePointMinVersion="15.0.0.0">
   <Properties>
     <Title>Sathish.App</Title>
     <StartPage>https://win8x64.dc07.loc/Sathish.AppWeb/Pages/Default.aspx?{StandardTokens}</StartPage>
   </Properties>
 
   <AppPrincipal>
     <RemoteWebApplication ClientId="*" />
   </AppPrincipal>
   
   <AppPermissionRequests>
     <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="FullControl" />
   </AppPermissionRequests>
 </App>

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

clip_image029[4]

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.

<appSettings>
  <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"/>
</appSettings>

b. AppManifest.xml – AppPrincipal

 <AppPrincipal>
   <RemoteWebApplication ClientId="1de402c2-911a-47f5-8b51-fd8b57144c41" />
 </AppPrincipal>

26. After updating the ClientID, we can build and Deploy the solution. Right click the solution and click Deploy.

clip_image031[4]

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.

clip_image033[4]

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
 cls
 
 Add-PSSnapin "Microsoft.SharePoint.PowerShell"
 
 # 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"
 
 #Get-SpAppPrincipal -?

29. On the successful registration, we will get the output like,

clip_image035[4]

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.

clip_image037[4]

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.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment