Showing posts with label sharepoint. Show all posts
Showing posts with label sharepoint. Show all posts

Passing a file from an Azure Logic App to a Web API

(Ce billet en aussi disponible en fran├žais.)

Logic App is one of my favorite tools in my cloud toolbox. It's very easy to connect things together, something without even coding! Last week, I needed to pass a file from a SharePoint folder to an API. I moved files tons of times using Azure Logic Apps, but this time something was not working. Thanks to Jeff Hollan (@jeffhollan) who put me on the good path by giving me great advice, my problem was quickly solved. In this post, I will share with you the little things that make all the difference in this case.

The Goal


When a file is created in a SharePoint folder, an Azure Logic App needs to get triggered and passes the file name and its content to a Web Api. In this case, I'm using Sharepoint, but it will work the same way for all folder connector types (ex: DropBox, OneDrive, Box, GoogleDrive, etc.)

Note:
In this post, I'm using a SharePoint Online, but the same thing could perfectly work with a SharePoint on premise or in a Virtual machine. In this situation, On-premise Data Gateway needs to be installed locally. It's very easy to do, just follow the instruction. One gotcha... You MUST use the same Microsoft account of type "work or school" to connect to the Azre.portal.com and installing the On-premise Data Gateway.

The Web API App


Let's start by building our Web API. In Visual studio create a new Web API App. If you would like to have more details about how to create one see my previous post. Now, create a new controller and add a new function UploadNewFile with the following code:

[SwaggerOperation("UploadNewFile")]
[SwaggerResponse(HttpStatusCode.OK)]
[Route("api/UploadNewFile")]
[HttpPost]
public HttpResponseMessage UploadNewFile([FromUri] string fileName)
{
    if (string.IsNullOrEmpty(fileName))
    {
        return Request.CreateResponse(HttpStatusCode.NoContent, "No File Name.");
    }

    var filebytes = Request.Content.ReadAsByteArrayAsync();

    if (filebytes.Result == null || filebytes.Result.Length <= 0)
    {
        return Request.CreateResponse(HttpStatusCode.NoContent, "No File Content.");
    }

    // Do what you need with the file.

    return Request.CreateResponse(HttpStatusCode.OK);
}

The tag [FromUri] before the parameter is just a way to specify where that information is coming from. The content of the file couldn't be passed in the querystring, so it will be passed through the body of our HTTP Request. And it will be retrieved with the code Request.Content.ReadAsByteArrayAsync(). If everything works we return a HttpResponseMessage with the HttpStatusCode.OK otherwise some message about the problem. You can now publish your Wep API App.

In order to be able to see our WebAPI App from our Logic App, one more thing needs to be done. From the Azure portal, select the freshly deployed App Service and from the options section (the left area with all properties) select CORS, then type * and save it.

changeCORS

The Logic App


Assuming that you already have a SharePoint up and running, let's create the new Logic App. Once the Logic App is deployed click the edit button to go in the designer. Select the Blank template. In this post, I need a SharePoint trigger when a New File is created. At this point, you will be asked to answer a few questions in order to create your SharePoint connector. Once it's done select the folder where you will be "dropping" your files.

Now that the trigger is done, we will add our first (an only) action. Click Add Step. Select available functions, then our App Service and finally the method UploadNewFile.
SelectApiApp
Thanks to swagger, Logic App will be able to generate a parameter form for us. Put the filename in the Filename parameter textbox. The Logic App should look like this.

FullLogicApp

The last thing we need to do is specify to our Logic App to pass the file content to the body of the HTTP request to the API. Today, it's not possible to do it using the interface. As you probably know, behind that gorgeous sits a simple json document, and it's by editing this one that we will be able to specify how to pass the file content.

Switch to Code view, and find the step that calls our API App. Simply add "body": "@triggerBody()" to that node. That will tell Logic App to bind the body of the trigger (the file content) and pass-it to the body of our web request. The code should look like this:

"UploadNewFile": {
    "inputs": {
        "method": "post",
        "queries": {
        "fileName": "@{triggerOutputs()['headers']['x-ms-file-name']}"
        },
        "body": "@triggerBody()",
        "uri": "https://frankdemo.azurewebsites.net/api/UploadNewFile"
    },
    "metadata": {
        "apiDefinitionUrl": "https://frankdemo.azurewebsites.net/swagger/docs/v1",
        "swaggerSource": "website"
    },
    "runAfter": {},
    "type": "Http"
}

You can now save and exit the edit mode. The solution is ready, enjoy!

References:

Reading Notes #119

 

Stack of books-300

Suggestion of the week

  • BizTalk Summit 2013 Wrap-up (Kent Weare) - Very important news from the integration world. This post is a very complete summary of the event. Good job.

 

Cloud


Databases

 

Programming

Miscellaneous


~Frank


Reading Notes Special Books Edition

For the last months I read few technical books.  I thought it could be interesting to add it.  This is not a complete review and it only reflect my personal opinion.


EF CodeFirst coverProgramming Entity Framework: Code First 
By Julia Lerman, Rowan Miller
Publisher: O'Reilly Media
Released: November 2011
ISBN 10:1-4493-1294-2

Note: By the time I write this post the version of Entity framework is 5.0.
Even if this book was about the previous version of Entity framework, it’s a golden mine of information. I found very useful the example that helps me to understand how to define the relation and more importantly what was the deference between the attribute way and the fluent way. It was easy to read, because well explains not because it was doing only the “beginner” stuff. Since EF is really useful when starting a new project or even for a POC, I think this book should be in all developer’s bookshelf.

 
PowerShell BestPractices coverWindows PowerShell 2.0 Best Practices
By Ed Wilson
Publisher: Microsoft
Released: December 14, 2010
ISBN-10: 0735626464

If you are a .Net developer and you still don’t know PowerShell or don’t know why you should consider PowerShell than this book is not for you… You should read some Introduction books or post THEN read this book.
This book is all about best practices. It will help you to structures your code or your library of scripts. That way, you will be able to understand and found and reuse all of it. Whether to do something in a build or to deploy something in Windows Azure or even just to quickly do a repetitive task this book will help to do it.  A book to keep nearby. 


Claims-Based_CoverClaims-based Identity Second Edition device
By Dominick Baier, Vittorio Bertocci, Keith Brown and Matias Woloski
Publisher: Microsoft
Releaed: April 21, 2010
ISBN-10: 0735640599

This is definitely THE book to get started with claims. It starts right at the beginning explaining what’s a claim, why we should use it.  Many different scenarios are presented. Each of them is presented with and without claims and most of the time how to migrate from one to the other.  The complete solution is available on codeplex.com and if a PDF is good enough for you; it's also available on codeplex for free. 


Building N-Layered Applications with ASP.NET 4.5N-Layer_Build_cover
By Imar Spaanjaars
Publisher: Imar.Spaanjaars.Com
Released: July 2013

This document is, in fact, a series of post about the best practices related to Asp. Net 4.5. While writing this notes, only two or three posts were available online. To get the full document with the source code you must pay. Eventually, all parts of the rich document will be available for free, but it's only 20$ and its own is value.
The solution explained in this document is simple enough so it’s easy to understand the architecture but detailed enough to cover most of the case.  The solution will include a lot of technologies like: Entity Framework, Asp. Net MVC, WCF Services and many patterns: Dependency Injection, Repository, Unit Test, Mocking, etc.
It’s a really well done document, and I think it’s a must to any web developer or architect.

Microsoft SharePoint 2013 App Development
Microsoft® SharePoint® 2013 App Development
By Scot Hillier, Ted Pattison
Publisher: Microsoft Press
Released: November 2012
ISBN 10:0-7356-7498-1

I read this book to know more about the new “Apps” thing.  While I was ready, I understand that my project was not a good match for SharePoint App, so I didn’t finish it… yet. However, I found the book explications really clear, and it gives me all the tooling and options I needed to get started.
Since Apps are now very present in SharePoint, I decided to include it in my notes.



~Frank






Reading Notes #45

WasabiCloudCloud

Azure lets us focus on our product rather than focusing on how we need to scale the application
“For a rich comparison of SQL Azure and Windows Azure Table storage, see Joseph Fultz’s MSDN Magazine article at: http://aka.ms/SQLAzureVsAzureTables.”
This blog has three posts that give detailed explanation of how a test rig with Controller and Agents can be created in Windows Azure.”
There does not seem to be any good working OAUTH v2 examples for Java using ACS

Programming


Miscellaneous


~Frank