How to create provider hosted app in SharePoint online

In this SharePoint Online tutorial, I am going to explain what is a provider hosted add-in. How to create a provider hosted app in SharePoint Online.

Also, we will see how to deploy a provider hosted app/add-in to Microsoft Azure. Then we will see how to access provider hosted add-in/app from our SharePoint Online site.

What is Provider Hosted Apps/Add-in in SharePoint Online?

A provider hosted add-in or Apps is an add-in that includes a web application (mostly asp.net webform 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 the SharePoint farm.

Configure App Environment for SharePoint

To work with SharePoint Add-in development we need to configure the apps or add-in environment.

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 a 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.

Create provider hosted app in SharePoint online

Now we will see step by step how to develop a provider hosted apps/add-in using visual studio 2017/2019.

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
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:

provider hosted app in sharepoint online using azure
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:

provider hosted app in sharepoint online
provider hosted app in sharepoint online

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.

provider hosted app in sharepoint online step by step
SharePoint 2013 online step by step

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:

sharepoint provider hosted app azure
SharePoint provider hosted app azure

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:

provider hosted app in sharepoint online tutorial
provider hosted app in SharePoint online tutorial

Add Business Logic 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.

create provider hosted app sharepoint online step by step
create provider hosted app SharePoint online step by step

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(clientContext.Web, web => web.Title);
clientContext.ExecuteQuery();
Label1.Text = "Your Site Title Is: " + clientContext.Web.Title;
}
how to deploy provider hosted app in sharepoint online
how to deploy provider hosted app in sharepoint online

Generate & Update Client Id and Client Secret

Now we need to create the Client Id and Client Secret for the SharePoint Online site.

Open SharePoint site on which you want to add the add-in here I will use the developer site. To create the Client ID and Client Secret, you need to acc the below URL. We need to first access appregnew.aspx page by the following URL.

_layouts/15/appregnew.aspx
Example: https://onlysharepoint2013.sharepoint.com/sites/DevSite/_layouts/15/appregnew.aspx

Once you open this page, click on the Generate button to generate the Client Id and Client Secret and then Provide the details like below:

  • Title: Title of the Add-in
  • App Domain: You need to provide the Azure website URL without, https:// Example: SharePointTraining.azurewebsites.net
  • Redirect URL: Here you need to provide the Azure web site URL with https:// like, https://SharePointTraining.azurewebsites.net

Then click on Create button.

provider hosted app in sharepoint online
provider hosted app in sharepoint online

Once you click on Create, you can see a confirmation page like below where it will show “The app identifier has been successfully created.” You need to copy the Client Id and Client Secret, because we need to update in the web.config file.

provider hosted app in sharepoint online
provider hosted app in sharepoint online

Now Open the Web.Config file (Asp.Net Web form application) and update the Client Id and ClientSecret in AppSettings like below:

create provider hosted app sharepoint online
create provider hosted app SharePoint online

Then we need to update Client Id in AppManifeast.xml file like below:

Right click on the AppManifeast.xml file and then click on “View Code” like below:

provider hosted app in sharepoint online
create provider hosted app SharePoint online

This will open the Code file and there update ClientId in RemoteWebApplication like line number 15 below:

sharepoint 2013 app development step by step
SharePoint 2013 app development step by step

Once we modify the Client Id and Client Secret, your SharePoint Online provider hosted add-in is ready for deployment.

Deploy provider hosted app SharePoint online

Now it is the time to deploy the Provider-hosted add-in to the developer site. The solution has two projects, we will have to deploy the Asp.Net web application to Microsoft Azure.

Before deploying we need to get the profile of azure web site.

To deploy the site to Azure, we need to create a website in Microsoft Azure and then we need to download the Publish profile of the web site.

Follow the below tutorial before proceeding further.

Once you have downloaded the publish profile, Right click on Asp.Net Web Project and then click on Publish like below:

create provider hosted app sharepoint 2013 on premise step by step
create provider hosted app sharepoint 2013 on premise step by step

Then in “Pick a publish target“, choose App Service and then click on “Import Profile” like below:

provider hosted app deployment in sharepoint online
provider hosted app deployment in SharePoint online

There browse to the published profile and click on Publish. The publish will start.

provider hosted app in sharepoint 2013 online
provider-hosted app in SharePoint 2013 online

Then you will be able to see the Publish will be successful. And you can see below. But if you will check, you can see the site URL it is pointing is to an HTTP URL, not an https URL. So do this click on the “preview” link in the Site URL section.

provider hosted app deployment in sharepoint online
provider hosted app deployment in SharePoint online

Then you can see the below Publish screen, there change the Destination URL to https like below:

provider hosted app deployment in sharepoint 2013
provider hosted app deployment in sharepoint 2013

Then in the next screen click on Next.

provider hosted app in sharepoint online
provider hosted app in sharepoint online

Then you can see the site URL changed to https and you can just click again on the Publish button, which will publish the application to Microsoft Azure.

provider hosted app in sharepoint 2013
provider hosted app in sharepoint 2013
how to debug provider hosted app in sharepoint online
how to debug provider hosted app in SharePoint online

In the above steps, the Asp.Net web application will be successfully deployed to Azure. Next, we will publish the Add-in project.

Publish Add-in Project to SharePoint Online Site

Now we will publish the Add-in project to SharePoint Online site. Right click on the Add-in Project and click on Publish…

how to deploy provider hosted app in sharepoint online
how to deploy provider hosted app in sharepoint online

Then you can see Edit, button with yellow mark, which means some issues are there. Click on the Edit button.

create provider hosted app sharepoint online step by step
create provider hosted app sharepoint online step by step

Then in the next screen Put the Client Id and Client Secret which we got from the above steps. It should look like below:

create provider hosted app sharepoint online
create provider hosted app sharepoint online

Then click on Finish button. And then click on Deploy your web project button like below once again.

create provider hosted app sharepoint 2013 on premise step by step
create provider hosted app sharepoint 2013 on premise step by step

You can see it will continue publishing the item.

provider hosted app in sharepoint 2013 online
provider hosted app in sharepoint 2013 online

Package Provider Hosted Apps/Add-in

Now we can package the Add-in project. Click on the “Package the add-in” button.

provider hosted app deployment in sharepoint online
provider hosted app deployment in sharepoint online

Then it will display you the URL and the Client ID, you can verify and change if required. By default, it shows the correct site URL and Client ID.

provider hosted app deployment in sharepoint online
provider hosted app deployment in SharePoint online

Then it will generate the .app file and it will open the folder which contains the .app file.

C:\Users\Administrator\source\repos\DempProviderHostedAddin\DempProviderHostedAddin\bin\Debug\app.publish\1.0.0.0

Upload .app file to Developer Site:

Now it is time to deploy the .app file to the developer site. Open the developer site and open “Apps in Testing” list where we need to add the .app file.

Click on “+ new app to deploy” which will open a dialog box like below. Click on the “upload” link. Browse to the .app file from the above location and click on OK.

provider hosted app deployment in sharepoint 2013
provider hosted app deployment in sharepoint 2013

Then click on Deploy button like below.

provider hosted app in sharepoint online
provider hosted app in sharepoint online

Then it will ask you to trust the App like below, click on Trust It.

provider hosted app in sharepoint 2013
provider hosted app in sharepoint 2013

Then you can see it will take sometime to install the app, it will show “installing” status like below:

provider hosted app in sharepoint online
provider hosted app in sharepoint online

Once the App or Add-in installed successfully, you can see it like below.

provider hosted app in sharepoint 2013 online step by step
provider hosted app in sharepoint 2013 online step by step

Add Provider Hosted Add-in in SharePoint Online

Now we can test SharePoint Provider hosted add-in or apps. Open Site Contents page and you can see the app or add-in there. In Classic SharePoint site, the add-in will appear like below:

Provider hosted add-in SharePoint online example
Provider hosted add-in SharePoint online example

In SharePoint modern site contents page, you can see the add-in will appear like below:

provider hosted app in sharepoint online step by step
provider hosted app in sharepoint online step by step

Once you will click on the add-in, you can see it will open the Azure web site like below:

provider hosted app in sharepoint online
provider hosted app in sharepoint online

Conclusion

I hope this SharePoint tutorial helps to learn provider hosted Apps/add-in in SharePoint Online and learned about:

  • What is Provider Hosted Apps/Add-in in SharePoint Online?
  • Create provider hosted app in SharePoint online
  • Add Business Logic In Provider Hosted Add-in
  • Generate & Update Client Id and Client Secret
  • Deploy provider hosted app SharePoint online
  • Publish Add-in Project to SharePoint Online Site
  • Package Provider Hosted Apps/Add-in
  • Upload .app file to Developer Site:
  • Add Provider Hosted Add-in in SharePoint Online
>