Wednesday 8 May 2013

Azure is the new SharePoint ‘layouts’ directory

SharePoint developers have always had a need to store files in a central location that can be accessed/shared from ALL SharePoint sites. This is often needed for supporting files such as images, CSS and JavaScript - usually we don’t want such files to exist in each individual site, because when updates are needed we might then have 10,000 different places to update the file. As you can imagine, there are other reasons to avoid this duplication too.

One long-established way of working around this is to deploy such files to the server filesystem, rather than into a specific SharePoint site. Commonly, the ‘_layouts’ folder under SharePoint root directory (i.e. ‘14’ for SharePoint 2010 or ‘15’ for SharePoint 2013) was used for such shared files. A second method involved deploying files as ‘uncustomized’, or ‘GhostableInLibrary’. This works by adding a list item ‘stub’ for the file into each site or library, but since the file is also deployed to the SharePoint server’s filesystem, the file contents are pulled from there. In the content database, the SQL record literally has a pointer to the location of the physical file on the SharePoint server (again, this would be somewhere under SharePoint root directory).

Although not just related to app development, I included this article in my 10 part series on apps:

  1. SharePoint 2013 apps – architecture, capability and UX considerations
  2. Getting started – creating lists, content types, fields etc. within a SharePoint app (provisioning)
  3. Working with data in the app web, and why you should
  4. Access end-user data (in the host web) from a SharePoint 2013 app
  5. Rolling out SharePoint 2013 apps to the enterprise - tenant scope and PowerShell installs
  6. Azure is the new SharePoint ‘_layouts’ directory [this article]
  7. “Host web apps” – provisioning files (e.g. master pages) to the host web
  8. “Host web apps” – provisioning fields and content types
  9. Deploying SP2013 provider-hosted apps/Remote Event Receivers to Azure Websites (for Office 365 apps)
  10. Working with web parts within a SharePoint app

Cloud says no

Once you start to develop sandboxed solutions or SharePoint 2013 apps, of course the technique above cannot be used. Files cannot be provisioned to the server filesystem, since the servers might not belong to you – in the case of Office 365, the servers are run by Microsoft. So what can we do?

Well, something I’m sure I’m not the first to think of is that if the production servers are connected to the internet (and cloud services like Office 365 always are), then you can just store your images, CSS and JavaScript files on some other servers (or service), and your pages will run just fine. For what it’s worth, I’ve been doing this on my blog site for quite a while – if you go looking, you’ll see I have some CSS and JavaScript being loaded ‘across the internet’ from http://sharepointnutsandbolts.azurewebsites.net rather than http://www.sharepointnutsandbolts.com.  Like me, you might find that Azure is a good choice here – it’s a solid offering with several flavors (I use the free version), and numerous options exist for getting your files there (FTP, WebDeploy, continuous deployment from TFS/Git etc.). Of course, any similar cloud service would work fine too – as indeed might some on-premises non-SharePoint servers (rather than service) you supply. Just remember that with that latter option, you’d need to take care of high-availability, backup/restore, maybe load-balancing etc. Thanks to Azure, I can let Microsoft deal with that :)

As you can imagine, we now regain the ability to store a single instance of files, even if not on the SharePoint server itself. To illustrate, here’s what the ‘default’ and ‘centralized’ approaches look like in pictures:

Default app/sandboxed development model:

Azure for storing app files 2

Centralized development model:

Azure for storing app files

When should I be considering this?

I think this broad approach is relevant to the following scenarios:

  • Developing sandboxed solutions
  • Developing SharePoint-hosted apps
  • Developing auto-hosted apps

The scenario I’ve left out is provider-hosted apps. This is because here your supporting images, CSS and JavaScript files are stored outside of SharePoint anyway – and by default are stored as a single instance. After all, a provider-hosted app is just a non-SharePoint website at the end of the day. Auto-hosted apps are perhaps an interesting case because the app’s supporting files are deployed to Azure, but the same deal applies – they are not shared between app instances.

If you are working in these models and not currently considering this approach, perhaps you should. Yes, it can make the initial development process more complex (since your app is effectively stored in multiple places), but rolling out updates once in production will most likely be much simpler. Even if you don’t like this idea, then at least ensure you’ve considered your update mechanisms for these files.

An interesting observation (if it hadn’t occurred to you already), is that provider-hosted apps are mainly stored outside SharePoint – including their business logic. This means, amongst other things, that the creator has the option of making fairly big changes to the app without requiring the app to be upgraded or resubmitted to the Store. Certainly worth bearing in mind.

Further considerations

I think the following also need to be considered:

  • Versioning
    • You’ll most likely need to provide some form of versioning of your “remotely-stored” CSS and JavaScript files, for example storing them under folders named “v1”, “v2” or “1.0.0.0”, “2.0.0.0” etc. This will allow you to deploy updates for later versions of your apps, whilst maintaining the original experience for users/sites who do not upgrade
    • Remember also that if you did want to make changes to existing CSS/JS files, these will most likely be cached at the browser for existing users. So you may need to do something to bust the cache (as discussed for non-app scenarios in Avoiding bugs from cached JavaScript and CSS files in SharePoint), but remember also that changes to the URL used will require changes/upgrades to the app itself unless the page is provider-hosted
  • Alternative storage flavors:

6 comments:

Jussi Palo said...

Probably only for public sites. With corporate intranets I'd prefer not to have files 'in the open'. Or can ACS be used here together with SharePoint Online?

Nigel said...

Hi Chris how do you make the versioning work if the reference for the css is on the masterpage ? I understand the different folders bit. Its how it get referenced on the master page that is the issue.

Chris O'Brien said...

@Jussi,

Yes, if details in those files cannot be exposed publicly then I agree it's probably not a good idea. However, for the average corporate app, frankly I can't imagine there's too much in the CSS or JavaScript which would pose a concern. Maybe if the JavaScript exposed some environmental details (e.g. absolute URLs/server names etc.) then that would be a bad thing, but that just sounds like poorly-written JavaScript.

One scenario I'd say would almost definitely not be suitable, is apps in the Store. In this case, usually the vendor would not want to expose their implementation and intellectual property in this way (especially if the app is not free).

And you raise an intriguing suggestion about securing Azure content via ACS - would only be applicable for SharePoint Online currently, but maybe this is possible (if not now, then soon).

Thanks for the comment,

Chris.

Chris O'Brien said...

@Nigel,

Just include the path to the CSS file in the link URL e.g. MyAzureSite/MyApp/v1/CSS/MyFile.css

HTH,

Chris.

sihugh said...

This is also useful if you want to consider "domain sharding" to take advantage of browser download capabilities. Referring to resources on a different domain can improve page performance if done judiciously.

Reference: Steve Souders' Blog

Chris O'Brien said...

@sihugh,

Agree (and it's a good point), but I have to say I've slightly lost touch on how many 'active channels' modern browsers maintain with a web server. I thought this was less of a concern now because browsers were no longer restricted to 2 connections as they were in the past?

If not, then I agree :) Nice point.

Thanks,

Chris.