>> Hi and welcome to Microsoft Build 2018.
My name is Caitlin Bales and I'm a software engineer on
the Microsoft 365 Programmability team.
Today, I want to talk to you about
our new Microsoft Graph SDK for
Java which allows you to get started
writing rich server-side applications
that utilize the power of
Graph while minimizing the amount
of boilerplate code you need to write.
The Java SDK features
strongly type models for
all the objects on the V 1.0 endpoint.
It uses a request builder pattern to build up
your URLs to naturally create
your navigation paths through Graph.
It also handles the boilerplate logic
for sending and receiving requests,
so that you can work directly with
the responses instead of
writing HTTP connection logic yourself.
Many objects in the SDK are
set by default such as the logger,
HTTP provider and the serializer.
But each of these can be customized or
swapped out to fit your specific needs.
We also make it really easy to send custom headers and
even custom requests from within the SDK if you need it.
Finally, there are many
extensibility points within the SDK.
Some of these are generic helpers that make
it easier to perform tasks across
the SDK and others perform
very specific tasks for individual workloads.
We also encouraged developers to
contribute their own helpers where
they think they would provide value to
the larger Graph developer community.
Today, we're going to look at a demo
that's going to show off these helper classes in
more depth and show how can make it
easier to post content to Graph using the SDK.
Multipart form post is request type that allows us to
pass multiple body parts into our request at one time.
This is especially useful on OneNote, for example,
where a page can have multiple types of
content on it such as text,
HTML, images and video.
As you can see in orange here,
multipart makes use of boundaries to
separate each content piece from the next.
So, let's look at how the Java SDK
simplifies making this multipart request to OneNote.
So, now I switch over to our demo.
So, you got on a blank OneNote notebook here.
We're going to be posting
some content from SharePoint into it.
So, we've got a blank notebook
and we're going to find some content from
within SharePoint and consolidate
some links that we can share out into it.
Here, we've got a simple Java console app.
So, we've got three files to it.
So, it's got a constant file,
that's where we're going to post our client ID,
our client secret as well as a
little HTML template that we can post our links into.
We've also got an authenticator.
So, that's how we're going to get our
access token that we
can post into our OneNote.
Then finally, our OneNote sync
which is going to be the bulk of
our communication to Graph.
So to get started, we're
going to setup our authenticator.
We're going to have it fetch an access token for us.
So in this case, we're using
the client credential flow to get an access token for us.
Then once we've gotten an access host
token that were authenticated,
we can go ahead and create that Authentication Provider.
So, that's the only thing that we need to do.
By default, we've got all the other pieces for us.
So, we've got a serializer.
We've got a logger.
We've got the HTP provider.
All that set by default.
The only other piece that we need is
Authentication Provider because we
can't guess your access token.
Then we can actually pass that
in to the Graph service client,
create from config and now we've got
a Graph client that we can start making calls to.
So, if we call graphClient,
we can see that from here we've got all
of our root level objects available to us.
So, we can call users and you can
see we can start chaining up that request and this
is the reprise builder pattern that I was talking about.
We call buildRequest.
Now, we have our request that's completed.
This is the point at which you can add custom headers,
you can add query strings to that and then when you're
ready to send the request you can call get, post, patch.
All those verbs that are available
to you. Let's call get here.
Now, you've got a full request
once we call get that's actually sending
it across the wire to the service.
Once we get that request back,
it's actually going to deduce serializer by default.
In this case, we're getting a user collection back
and we can use that in our program as a user collection,
as a deserialize object just the same way
that we would use anything else in
our program. So, we're ready to go.
But in this instance,
let's go ahead and look at how we would work with
this program for multipart use.
So down here, we've got
a little function that's posting content using multipart.
Multipart here is our helper function in our Java SDK.
So, we're creating a new multipart
object that's going to help us.
Let's find some content that we can push to it.
So, here we've got our HTML body where we've
got a default that has some filler content,
like a title and a body.
We just want to replace that filler
content with a string that we've built up
previously in the sample that has some links in it
from our OneDrive group
from different SharePoint files that we've found.
So, we'll put all that HTML content into our HTML body.
Just to show off some other ways that we can
post data using multipart,
I've also grabbed some files from the file system.
So, I've loaded up an image file and a PDF file.
Now, let's attach those to the multipart.
So here, you can see in the multipart part
we've added our HTML part,
we need to give it a title and we need to give
it the contents of the HTML part itself.
So adding an HTML part is pretty simple.
Adding a file part,
we need to specify what type that file part is.
An HTML part, we always know what the type is
but in a file part it's more ambiguous.
So, that just the way you would do in a raw request,
we need to specify what type that is.
So, this particular files JPEG.
It could be a PNG.
The great thing about OneNote is you can specify
a lot of file types and it displays them all beautifully.
So, you can pretty much throw whatever you want at it.
In this case, we are just storing an image and a PDF.
We're giving it a name and then we're giving it
that file reference that we had before.
Once we actually send that multipart,
it's going to send up that file as a stream.
The last thing that we need to do now is,
if you remember before,
we had all those multipart boundaries in orange,
the multipart helper will actually
insert all those boundaries for us.
But in the header of our request,
we actually need to specify what that boundary is.
That way when we send the request,
the service knows what to separate it with.
So, multipart has a property
on it called header and we can call to get
that header back and we can put
that into the header of our request.
So now that we've put that into our header option array,
we can actually call the Graph service.
So, we're going to get
the OneNote of the group that we're interested in.
We'll get a specific section
of our OneNote page that I showed you earlier.
Then run a call the pages API.
So, this will actually post a new page to that section.
We're going to pass in
the header option that we created earlier,
when we build the request.
Finally, we're ready to post.
So when we called multipart.content,
it's going to build up that full stream request with
all the multipart boundaries embedded into
it and that will post all that content to it.
So, let's run it and see what happens.
The first thing we'll see is that we are seeing a lot
of GET methods that's building up
all of that data that we need to
actually get the content
that we want to write to the stream.
So, we're getting all those links
and all that good stuff.
Finally, we're going to see that it's
posting that content.
So, here is the content that is posted from our query.
You see that it's given us a title,
that's from our default string.
We've got in all the links that we
posted from OneDrive that we've searched for,
it's also posted our image part and our PDF part.
So, each of these are different parts
of our same multipart requests that we're able to
do in one query using
the multipart helper in the Java SDK.
To get started with the SDK,
head over to graph.microsoft.com
and click on samples and SDKs.
All of our libraries are open source and
available on GitHub where you can read our wiki,
open issues and submit pull requests.
If you have any questions on how to
build your app on top of Graph,
you can post a question on Stack Overflow
with a tag Microsoft Graph.
We have developers from all over
the Graph Product Teams monitoring this tag.
They are fast to respond and for
my experience, are super helpful.
Thank you so much for watching and I can't
wait to see what you build with Microsoft Graph.