Thursday 16 July 2015

Downloading files from OneDrive using the Office 365 API (MVC add-in)

For this example I used the "Office 365 Starter Project for ASP.NET MVC" developed by Microsoft, which you can find here, including all the instructions to set it up.

This sample uses "the Office 365 API Tools to demonstrate basic operations against the Calendar, Contacts, and Mail service endpoints in Office 365 from a single-tenant ASP.NET MVC application.", so when you click in "My Files" button, you get a list of all files and folders within your personal OneDrive and the capability of deleting them. 

What I'm going to demonstrate is how to extend those capabilities, by adding a new link (Download) which will allow you to download the files you want. 

First, we have to create the method to download the file in our FileOperations class, located inside the "Helpers" folder.























Then, we have to create our "Download" action in the file controller. This action will be a FileStreamResult, as we want to send binary content using a Stream instance. 
Finally we will return the result of type File, passing the Stream instance and the file name. 

Note: In order to dynamically retrieve the item content type, we will use MimeMapping.GetMimeMapping method.



















To finish, we need to edit the view and add an action link to our controller. This action will be displayed only when the item is a file.














... The result:



























Happy coding ;)

Monday 13 July 2015

JSLink with callback handler and synchronous ajax calls to validate user input.

Scenario:

For this specific case, I based myself on one of our client's requests, which was "to create some sort of validation, when a user submits an item in a booking list". 

There was a list indicating the stock of each item available for booking, so the approach was to request the number of items available (via ajax call) in the mentioned list and if that number was greater than "0", then the user should be able to make a booking.

Details:

Material field name : Material
Item availability list name : Item Availability
Booking list name: Material Bookings

Solution:

After the JSLink file being referenced in the list definition (Schema.xml), we can start developing our business logic.


  • Adding the callback handler to the new form.













  • In the "AddCallbackHandler" function we will have to:

    1. Get form context for current field
    2. Create a validator set to register the required validator and our custom validator
    3. Register our error callback
    4. Return the field default html (choice in this case) and a <span> tag which will contain our custom error message to be displayed when saving a new item is not allowed.
























  • Now let's define our custom validation
I developed a separate function to get the results, just because we needed to make more than one call. Note: "async" was set to false, otherwise it would have to be promisified.


  • To finish, we just have to define what happens when an error occurs.







Find the full code below:



Hope you find it useful ;)