Jul 20, 2012

SPThumbMaker-Example for an Auto-Hosted Azure SharePoint App

To dig a little bit deeper into the possibilities of auto-hosted and provider hosted SharePoint 2013 apps, I’ve created a small example called “SPThumbMaker”. The app generates thumbnails for images that are stored in a picture library on the host site. To see how to get started with Auto-Hosted Azure apps read my previous post or the post Creating Provider Hosted Apps from my MVP mate Thorsten Hans.

Extend the Ribbon and the ECB to add the actions for the thumbnail generation

Ribbon Button “Create Thumbnails”

image

ECB Action

image

Elements XML of the two actions:

image

To only show the actions in picture libraries only I registered the actions on the list template type 109 (Picture Library). The “~remoteAppUrl” token points to the dynamically hosted ASP.NET site. Because my ASP.NET site must know for which image it should generate templates I’ve also appended the tokens “{ListId}” and “{ItemId}”. Beside the “~remoteAppUrl” token there is no difference to how you to SharePoint 2010 ribbon extension. But there this one restriction:
You can’t make use of JavaScript in the CommandAction. This is not allowed by design.

Get SharePoint Look and Feel on your ASP.NET Site

To integrate remote apps seamless into SharePoint the product team added the possibility to add the SharePoint chrome to our remote app.

Add the jQuery and ASP.NET AJAX Libraries

image

Dynamically load the “SP.UI.Controls.js” script. The scripts contains everything you need to render the SharePoint chrome on your site. This way you can load any other SharePoint script and CSS from within your app.

image

Render the SharePoint Chrome. You can customize the chrome via the options parameter. For example set the icon, title, add links…

image

Add a div container to render the chrome

image

Now the site header looks like this Smile

image

Server-side Thumbnail Generation

The generation works as follows:

I in the code behind of the ASP.NET form I extract the URL parameters, valid the SharePoint Context Token and get an OAuth AccessToken.

image

In the markup of the page I render image tags with a dynamically generated urls to a custom HttpHandler that generates the thumbnails.

image

I append to the URL all the parameters that I’ve got from the action (SPHostWeb, ListId, ItemId), the AccessToken and the size of the thumbnail.

image

In the HttpHandler I extract the query parameters and use the AccessToken to get a CSOM ClientContext to load the target  image from the host site and generate the thumbnail.

image

Finally the result looks like this

image

I hope this post helps to get a little bit better understanding of what you can do with auto-hosted and provider hosted apps.

You can download the source code here.

3 comments:

seoinheritx said...

This is an amazingly helpful post with some really useful tips. Thank you for taking the time to write this up.

Sharepoint Development | Sharepoint Developers

vaughanprattq said...

putting an essential idea about SPThumbMaker for use of SharePoint Application and good to read this post.

Guinevere Montoya said...

I have just basic idea of SharePoint and I am going to learn further but I was unknown about remote apps with SharePoint,you show with example I will try on that.