How to create SharePoint Online add-in with ASP.NET Core 2.1

While support of .NET Core for SharePoint CSOM libraries on its own way (still no ETA), you can create a SharePoint add-in with ASP.NET Core 2.1 today. Of course, it adds some inconveniences, but at least you can target the latest version of ASP.NET. I’ve created a sample project at GitHub here so you can easily try it. This post describes how to configure everything to run it.

First of all, a few drawbacks worth mentioning:

- you can’t use F5 experience in Visual Studio for convenient debugging. Instead, you should manually upload your app into a site and attach Visual Studio to running web application.

- you can’t target a project to netcoreapp. Obviously you can’t do it because SharePoint CSOM for .NET Core is not available yet. That’s why you have to retarget your project to .NET Framework instead. Which means that you lose cross platform feature. If that’s essential thing for you, then you should wait for official .NET Core support. 

- solution described here only works for SharePoint Online. On-premises is a completely different story and out of the scope of the post

Let’s get started.

Visual Studio

  1. Open the project in Visual Studio and rebuild it. It should restore dependencies and rebuild everything successfully.
  2. Hit Ctrl+F5 to run the web app. You will see an error on the home page, but it’s ok since it expects that the home page will be opened from SharePoint (from All Site Contents page)
  3. Write down your site url. It should be https://localhost:44316/, but probably it differs in your environment.

SharePoint

You should deploy your app to a developer site collection or enable side loading feature if a site collection is not a developer site collection.

The easiest way to the enable side loading feature is through the PnP-PowerShell code:

Connect-PnPOnline -Url https://org.sharepoint.com/sites/my-site
Set-PnPAppSideLoading -On
Enable-PnPFeature -Identity e374875e-06b6-11e0-b0fa-57f5dfd72085 -Scope Site

Then you should register new SharePoint add-in with AppRegNew.aspx page:

  1. In your site collection go to _layouts/15/appregnew.aspx and register a new app. For app domain use localhost:44316 (be aware that port might be different in your environment, so please check that the port match the port from your running web application), for redirect url https://localhost:44316/ (or your web app)
  2. In Visual Studio under  SharePointAddIn project -> AppManifest.xml replace ClientId with one generated at step #1
  3. In AddinWebApp project -> appsettings.Development.json update ClientId and ClientSecret with values from step #1

Publish

  1. Right click on SharePointAddIn -> Publish -> Package. Upload .app file to App Packages library.
  2. Go to Apps in Testing library -> new app to deploy and deploy the app. Wait for the installation to be completed.

You're ready to go! Click on your app from Site Contents page and you will see the home page with your logged in SharePoint user in the middle of the page. For debugging just press F5 in VS to attach it to a running web application. Note that no app will deployed.

How it works

Basically I’ve created a regular ASP.NET Core 2.1 project and retargeted it to .NET Framework. This web app is used as a ���provider” for SharePoint add-in. Since SharePoint add-in template in Visual Studio doesn’t support ASP.NET Core, I had to “unlink” and remove default MVC web project and added ASP.NET Core instead. The tricky part was to modify TokenHelper.cs and SharePointContext.cs to work properly with ASP.NET Core. Some classes were from System.Web namespace which is not available in .NET Core projects. Fortunately everything can be easily fixed.

Once again the source code is at GitHub.