Provider-hosted applications are the
most complex of the three app types because hosting and running the
application code is up to the developer, rather than allowing
SharePoint and Azure do these things for you. However, with the
increased complexity comes increased flexibility, because you manage
your application’s back end. This means that making fixes or updates to
it is straightforward, and the ultimate user for the application might
never know or care to know about the changes being made to the
application. Only if you make changes to the components of your app
that reside within the SharePoint site itself would you need to issue
an update to the application package in the app catalog or Marketplace.
However, you should consider that you might need to deal with
multi-tenancy issues, such as tenant specific–data partitioning, given
that an app may be installed on multiple sites.
The application backend can theoretically be any
technology and is not limited to Internet Information Server. The only
stipulation is that the SharePoint farm or SharePoint Online must be
able to communicate with it over HTTP (port 80) and HTTPS (port 443).
For on-premises applications, Provider-hosted
applications are the only choice when back-end code is involved. This
requires setting up server-to-server authentication .
Now try out deploying a Provider-hosted application in the following exercise using SharePoint Online and Windows Azure.
In this example you create a very
basic SharePoint Provider-hosted application using Visual Studio and
Azure Web Sites. You need a SharePoint Online tenant set up as well as
an Azure account with Azure Web Sites enabled. You also need to have
the app catalog created from the previous exercise, “Creating a Private
App Catalog in Office 365,” prior to starting this exercise.
1. Log into the Azure management portal and create a new Azure Web Site as shown in Figure 7. You must give it a unique URL, so remember to write it down because you will need it later on.
2. After the
site is created, go into the Azure dashboard for the site and download
the publishing profile for Visual Studio using the Download publish
profile link. Save the file locally somewhere.
3. Navigate to SharePoint Online’s tenant admin screens by logging into:
http://portal.microsoftonline.com,
clicking the Admin menu in the top right of the homepage and then
selecting SharePoint. Create a new SharePoint Developer site by
clicking into the Site Collections management page, and then click the
New button and select Private Site Collection, as shown in Figure 8.
4. Give your site a title; for example,
My New Site, as well as a URL, such as
mydevsite. Select the Developer Site template from the list of available templates, as shown in Figure 9, and then click OK.
5. After the site has been created, navigate to it and then navigate to the following page in the browser: https://<my SPO site url>.sharepoint.com/sites/mydevsite/_layouts/15/appregnew.aspx.
6. After the form appears, complete it by clicking the Generate button to create the client ID and secret for you.
7. Give your application a name such as My Awesome App.
8. In the App Domain box specify the exact URL for the Azure Web Site you created in step 1. The form should look like Figure 10.
9. Click Create to create the application registration.
10. Copy all the details provided about the application registration. Figure 11 shows an example.
11. Create a new SharePoint app in Visual Studio using the App for SharePoint 2013 template.
12. Enter the name of your app in the first box; for example, My Awesome App.
13. Enter the URL to your SharePoint Online development site.
14. Select Provider-hosted from the app type drop-down list. When it’s complete, the form should look similar to Figure 12.
15. Click Finish.
16. Right-click the Web project in the solution and select Publish as shown in Figure 13.
17. Click the
Import button and select the profile file you downloaded from Azure
previously. Download it from Azure if you skipped this step earlier.
Validate the connection to make sure it’s working by clicking the
Validate Connection button as shown in Figure 14.
18. Click Publish and let your site deploy. When the deployment finishes, a browser should pop up a screen like the one shown in Figure 15.
19. Go back
into the Azure management dashboard for your site and click the
Configure tab. Create the ClientId and ClientSecret app settings and
copy in the App ID and App Secret you generated in step 10 (see Figure 16). Azure Web Sites reads them from here even if you have them in your
web.config. Not configuring the settings in Azure will mean your app crashes.
20. Click the Save button to commit the settings to Azure.
21.
Right-click the SharePoint app project in Visual Studio and choose
Publish. If the Publish wizard is not already on the profile page,
click profile in the left-side navigation to get to the Start screen.
22. Type a new profile name; for example, Office 365 Profile, and click Next.
23. On the Hosting page enter the full URL to your Azure Web Site, including the
/pages/default.aspx; for example,
https://cjsproviderhostedapp.azurewebsites.net/pages/default.aspx. See Figure 17.
24. Enter the App ID you generated into the Client ID field and the App Secret into the Client Secret field.
25. Click Next. A summary page appears like the one shown in Figure 18.
26. Click Finish. A new Windows Explorer window opens with your application package called MySharePointProviderHostedApp.app.
27. Open a browser window and navigate to the tenant admin portal for your Office 365 environment at: http://portal.microsoftonline.com.
28. Under the Admin menu in the top navigation, click SharePoint to go to the SharePoint Online tenant admin pages.
29. Click Apps in the left-side navigation.
30. Click the App Catalog link. The app catalog you created in the previous exercise appears.
31. Click the Distribute apps for SharePoint tile.
32. Click the New App button. You are prompted to pick the .app file for your application.
33. Click Browse and select the
.app
file that Visual Studio packaged for you in step 26. Click OK. A dialog
appears asking for information about your application, as shown in Figure 19.
34. You are not required to enter any information, but you can enter the information if you want to, and then click Save. Your SharePointProviderHostedApp application appears in the app catalog.
35. Navigate to your development site in SharePoint Online.
36. Click Site Contents in the left-side navigation.
37. Click Add an App.
38. Click From Your Organization in the left-side navigation; you should see your newly added application listed.
39. Click the tile for the application. A prompt appears asking whether to trust your app.
40. Click Trust It, as shown in Figure 20. Your new app appears listed in the site contents.
41. Click it.
You are redirected to your app and should see the title (whatever you
called your site when you created it) of your site written to the page,
as shown in Figure 21.
In this exercise you created a
rudimentary SharePoint Provider-hosted app that simply lists the name
of the host site that app is installed in. You created, packaged, and
deployed the code portion of your application to an Azure Web Sites
site and configured the client ID and secret in the settings for that
application. You then packaged and deployed the SharePoint app using
Visual Studio’s Publish wizard. Once the packaging process was complete
you uploaded the newly created app package to the app catalog in
SharePoint Online.
Behind the scenes Visual
Studio uses Web Deploy to deploy the app code to the Azure Web Site.
When the application ran, it used the Client-Side Object Model (CSOM)
combined with OAuth to call back to SharePoint and retrieve the name of
the host Web SharePoint site. It did this using the client ID and
secret you set up in the Azure settings along with the TokenHelper class provided in the app template project in Visual Studio.