Host Multiple Provider-Hosted SharePoint Apps Within a Single Assosiated Web Application


While developing Provider Hosted Apps for our clients I noticed a lot overhead in our projects. Packaging, deploying, same code distributed over all provider hosted apps. In previous version of SharePoint we were getting used to provide a structured solution (WSP), providing a great set of features to our customers. The code was hosted within a single solution and solution package for easy release management. Although there are reasons why you should not do this, it is still a tradeoff between an easy to deploy solution and a structured way to deploy different features within a different release cycle. I don’t want to start this discussion here, but let’s take a look if it is even possible. Can SharePoint Provider Hosted Apps run within a single Web Application Project? In short, yes, it can work, although there are several reasons why you don’t want to get in to this! What we want to achieve is a solution structure were we have multiple App Manifest bound to a single App Web. In the example we have HighTrustSampleApp1 and HighTrustSampleApp2 added to the project and a Single App Web. How to accomplish this Ø Create a new solution Ø Add a SharePoint 2013 App (provider hosted); two projects will be added to you solution Ø Add a second SharePoint 2013 App (provider hosted); again, two projects will be added Ø Remove one of the webapplication Ø Select the Second App (App project) and go to properties Ø Set the Web Project the same as the Web App created with HighTrustSampleApp1 Ø Add a Second ASP.Net webpage to the project for your second app (App2.aspx) Ø Copy & Paste the codebehind from Default.aspx in the App2.aspx.cs Ø Open the App manifest of HighTrustSampleApp2 Ø Set the start page to App2.aspx Ø Ready for now! When we build and deploy this solution it will half work J. When pressing F5, to browser will pop up. If you press Trust on the second App in your project, both app will work! But effectively it is reusing the access token from the other app. Things to do in the SharePointContext & TokenHelper To authorize your App access to SharePoint your AppWeb has a library with some bunch of code to handle this OAuth handshake. Before you start believing in magic please make sure you know how OAuth works and how SharePoint authenticates your app using high or low trust techniques. The library has been delivered by default to host a single app, but in our case we are interested in hosting multiple apps in a single web app project. ClientId One of the things we need to deal with is the ClientId, which is (normally) different for every single App. The IssuerId can be shared between apps, so we can leave that one as is. The clientId is grabbed from the web.config where the clientid is registered. The ClientId should be different for the different apps, so you would need to develop a way to differentiate the app calls SharePointContext The Tokens are cached into the Http Context, so here you would need multiple session variables, one for every app Visual Studio So it looks pretty straightforward to update the code the get different different apps working within a single webapplication. But you will get annoyed by Visual Studio, because it has no support for developing multiple apps in a single Webapplication. When you are developing locally the ClientID is continuously updated to a new one on every deploy. Visual Studio registers your app for you in SharePoint and you’re good to go. Unfortunately this will not work. Conclusion For now, I have stopped my journey in exploring the ability to host multiple provider-hosted SharePoint Apps in a single web application project. Technically you can make it work, but there will be some issues on the way. We decided to continue to deploy our apps as a Virtual Application/Directory in IIS in a single Web Application for provider hosted Apps. Works great, with great support within Visual Studio. Define your apps in a scope which need to be deployed together, to minimize the App overhead. Get started with Provider Hosted Apps How to: Create high-trust apps for SharePoint 2013 (advanced topic) http://msdn.microsoft.com/en-us/library/office/fp179901(v=office.15).aspx Scripts to configure you development and production environment http://msdn.microsoft.com/en-us/library/office/dn579380(v=office.15).aspx Packaging and publishing your Provider Hosted App http://msdn.microsoft.com/en-us/library/office/jj860570(v=office.15).aspx