Provider Hosted App in SharePoint Online using Visual Studio 2017 + Deploy Provider Hosted Add-in to Microsoft Azure

In this SharePoint online tutorial, I am going to explain what is a provider hosted add-in (previously known as SharePoint hosted Apps)? How we can we develop a provider hosted add-in using visual studio 2017 and how we can deploy to Microsoft Azure. And then we will discuss how we can access provider hosted add-in from our SharePoint online site.

What is Provider Hosted Add-in in SharePoint Online?

A provider hosted add-in or Apps is an add-in which includes a web application (mostly asp.net web form or asp.net MVC application). service or a database which will be hosted externally from SharePoint farm (on-premise) or from SharePoint online subscription. The custom logic will run outside of SharePoint farm.

Configure App Environment for SharePoint Hosted Add-in Development & Prerequisites

To work with SharePoint Add-in development we need to configure the apps or add-in environment. If you are new to SharePoint 2016, you can download the SharePoint 2016 installation guide and check out some new features of SharePoint 2016.

Once SharePoint 2016 installation over, you can Configure Add-In or app development environment in SharePoint server 2016.

Another prerequisite is that you need to use a developer site for developing provider hosted add-in in SharePoint online. You can read an article on How to create developer site in Office 365 SharePoint 2013 Online?

Note: For SharePoint Online, we do not need to configure anything, all configurations are done by Microsoft.

Steps to Develop Provider Hosted Add-in using Visual Studio 2017

Now we will see step by step how we can develop a provider hosted add-in using visual studio 2017. If you have not installed visual studio 2017, then you can read an article on visual studio 2017 step by step installation.

Open Visual Studio 2017, then click on File -> New Project and then in the New Project dialog box, choose Visual C# -> Office/SharePoint and then choose “SharePoint Add-in” like below:

Provider hosted app in SharePoint Online

Then it will ask you to give a developer site URL. Give a developer site URL which you have created in the previous steps. Then from the “How do you want to host your SharePoint Add-in”, choose “Provider-hosted add-in” like below:

SharePoint provider hosted app MVC tutorial

SharePoint provider hosted app MVC tutorial

Then it will ask you for credentials, Put username and password and then it will show “SharePoint Online” like below:

SharePoint version

SharePoint version

In the next screen, it will ask you to choose which type of web application you want to create. You can choose one from below:

  • ASP.NET Web Forms Application
  • ASP.NET MVC Web Application

Here I choose ASP.NET Web Forms Application, but you can choose as per your skills.

ASP.NET Web Forms Application

ASP.NET Web Forms Application

Then in the next screen, we need to Configure authentication settings. For SharePoint Online choose “Use Windows Azure Access Control Service (for SharePoint cloud add-ins)”.

If you are developing a provider hosted add-in or apps for SharePoint 2013 on-premise environment, you can create your own certificate and use that certification. It should look like below:

Configure authentication settings

Configure authentication settings

Once you click on Finish, The Add-in project will be ready. You can see two projects: One is the Add-in project and another project is the Web application project (ASP.NET Web Form Application Project.) You can see the visual studio solution will look like below:

Solution Explorer

Solution Explorer

Add Business Login In Provider Hosted Add-in:

Now you can add your business logic inside the Provider hosted app, Here I have just added an Asp.Net Label in the Default.aspx code. You can add your own custom business logic.

Custom business logic

Custom business logic

Similarly, you need to add the code in the code behind file.

Remember in provider hosted add-in or provider hosted apps we can write CSOM (.Net Object model code). So we can use below two dlls to write our business logic.

  • Microsoft.SharePoint.Client.Dll
  • Microsoft.SharePoint.Client.Runtime.Dll

By default when you create the Add-in project, both the dlls will be added before.

Here I have just written the code to display the Site title and the code looks like below:

var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
using (var clientContext = spContext.CreateUserClientContextForSPHost())
{
clientContext.Load(clientConte