SharePoint 2013 Autohosted apps

MSDN subscribers can activate Office 365 developers benefit that gives them free Office 365 development instance for year. If you have this subscription then you can use your local Visual Studio to develop new style applications and then run and test them on Office 365 environment. In this posting I will introduce you hybrid development of Office 365 SharePoint applications.

What are autohosted apps?

Autohosted apps are currently available only on Office 365. They differ from provider-hosted apps by fact that they are deployed fully to cloud: SharePoint application is deployed to Office 365 and web application is deployed to Windows Azure based cloud. All complex configuring is done automatically for you. You just write code and push Run to see how it works.

MSDN benefits. If you have good enough MSDN subscription you will get Windows Azure and Office 365 developer accounts for year. Of course, you can continue those accounts if you continue your MSDN subscription. There are scenarios where you need Windows Azure to make your development machine reachable for Office 365 but you don’t need it with every development.

Tools

To create autohosted app you need Visual Studio and tools for building SharePoint apps. After installing Visual Studio you can install tools using Web Platform Installer. I also suggest you to install latest Windows Azure tools and SDK-s if you plan to build some more advanced stuff.

No local SharePoint needed! Before you start installing virtual machines for SharePoint 2013 – stop! If you build Autohosted apps you don’t need local SharePoint installation. Visual Studio, tools for SharePoint apps, Office 365 subscription and internet connection are all you need.

I suggest you to use special site collection on Office 365 to test and run your authosted apps. This way you leave also room for other experiments on your cloud SharePoint.

Building autohosted app

Now you are ready to build your first autohosted app. Run Visual Studio, open new project windows and select App for SharePoint 2013.

Visual Studio 2012: Create SharePoint 2013 autohosted app

Visual Studio asks you now about some details of your new application.

Visual Studio 2012: SharePoint 2013 autohosted app settings

It’s recommended that you validate the URL you inserted. Not sure if NSA will find out about your test site but Visual Studio needs access to your Office 365 site collection later to deploy your applications.

Visual Studio solution

SharePoint 2013 autohosted solution in Visual Studio 2012When settings are okay click Finish and see what Visual Studio generated for you.

You have two projects – one for SharePoint app and the other for web application that contains actual application logic and server-side code.

All pages are expected to be in Pages folder. You can find default page that helps you easily test if web application can connect to SharePoint using Client-Side Object Model (CSOM) or not.

You will also find jQuery files and TokenHelper class. TokenHelper file contains code that helps your web application on token management as connection to SharePoint uses OAuth protocol.

The code in default page is as follows:


namespace SharePointApp2Web.Pages
{
   
public partial class Default : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs
e)
         {
             var contextToken = TokenHelper.GetContextTokenFromRequest(Page.Request);
            
var hostWeb = Page.Request["SPHostUrl"
];


            
using (var clientContext = TokenHelper.GetClientContextWithContextToken(hostWeb, contextToken, Request.Url.Authority))
             {
                 clientContext.Load(clientContext.Web, web => web.Title);
                 clientContext.ExecuteQuery();
                 Response.Write(clientContext.Web.Title);
             }
         }
     } }

The code gets context token and URL of SharePoint host where request came and then uses this data to make request to SharePoint. If request succeeds then title of host web is printed to screen.

Running autohosted app

Without making any changes let’s run our solution. First you are asked to trust your application.

SharePoint 2013: Trust your application

Click “Trust It”. After trusting your application you are redirected to application like show here:

SharePoint 2013 autohosted app running on local machine

You see that your application is running on your local machine and you can use Visual Studio debugger to debug it. Now, this is really fancy – you run cloud-targeted application on your own machine on Visual Studio debugger.

If you add your application to some site in cloud without using Visual Studio then web application will run somewhere on Windows Azure and your local environment is not needed anymore.

Conclusion

New Visual Studio tools for building SharePoint 2013 apps enable you to write and test apps that run on Office 365 coud. You can debug your web application on your own development machine and support for this comes out-of-box with SharePoint app building tools. We needed no local SharePoint installation to test and run our autohosted app. Although this is the convenient way to build things there are still some limitations like missing access from Office 365 to your machine. As you don’t need communication on this direction often you can get most of your apps done using autohosting model on Office 365.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    8 thoughts on “SharePoint 2013 Autohosted apps

    • August 11, 2013 at 11:01 am
      Permalink

      The documentation is really scarce on who be paying for running the Azure code of your auto hosted app. Do you as a developer pay the Azure subscription fee? or does the buyer of the app pay for it? Any idea on that?

    • August 13, 2013 at 10:21 pm
      Permalink

      Which version of Visual Studio did you mean? In Visual Studio 2012 along with Web Essentials 2012 I can’t find the project template “App for SharePoint 2013”.

    • August 13, 2013 at 10:23 pm
      Permalink

      Vardhaman, in the case of auto-hosted apps there shouldn’t be any additional expenses. It is coming to Office 365 when next version of SharePoint is deployed there (not all accounts have it already).

    • September 4, 2013 at 10:58 pm
      Permalink

      Gunnar, I believe Vardhaman’s question may have been along the lines of who is responsible for paying for the Azure services subscription required to the run the autohosted app. The app subscriber? or is the developer taking this on?

    • September 13, 2013 at 7:30 am
      Permalink

      As far as I understand then Azure host for autohosted app is included in O365 subscription price. I have seen no separate pricing for this.

    • January 4, 2014 at 7:37 am
      Permalink

      Hi Gunnar ,
      I want to know if i have the autohosted apps with 6GB size. Now i want to store that app in sharepoint store than every buddy use that app. I don’t have windows azure account. Than if any buddy upload my app and in own office 365 deploy that app. So they pay for that or free..?

    • January 8, 2014 at 1:28 pm
      Permalink

      You may have free and paid applications in SharePoint store. Up to you to decide. I haven’t seen my invoices getting bigger when adding applications to O365. Not sure, of course, what happens with 6GB application – it’s total godzilla :)

    Leave a Reply

    Your email address will not be published. Required fields are marked *