Udostępnij za pośrednictwem


Getting Started with the Office 365 APIs

This weekend I had the pleasure of speaking on a couple of Office Development topics at Silicon Valley Code Camp, as well as the East Bay.NET user group meeting on Thursday (with special Halloween guest). It was great to pack three talks into one week as I’ve been doing so much internal-facing work lately, that I have been really itching to get back out to speak in front of the developer community.

One of the areas I’ve been working in for a while is building SharePoint Apps. Office and SharePoint Apps let you customize the Office and SharePoint experiences with your own customizations. Apps are web-based, and you use HTML and JavaScript to customize Office (Outlook, Word, Excel, PowerPoint) and SharePoint itself.

image

For more info on apps, see the MSDN Library: Apps for Office and SharePoint

We’ve also been working on another programming model that I’m really jazzed about. It allows you to build your own custom apps and consume data from Office 365 (Sites, Mail, Calendar, Files, Users). They are simple REST OData APIs for accessing SharePoint, Exchange and Azure Active Directory from a variety of platforms and devices. You can also use these APIs to enhance custom business apps that you may already be using in your organization.

image

To make it even easier, we’ve built client libraries for .NET, Cordova and Android. The .NET libraries are portable so you can use them in Winforms, WPF, ASP.NET, Windows Store, Windows Phone 8.1, Xamarin Android/iOS,. There’s also JavaScript libraries for Cordova and an Android (Java) SDK available.

image

If you have Visual Studio this gets even easier by installing the Office 365 API Tools for Visual Studioextension . The tool streamlines the app registration and permissions setup in Azure as well as adds the relevant client libraries to your solution via NuGet for you.

Before you begin, you need to set up your development environment.

image

Note that the tools and APIs are currently in preview but they are in great shape to get started exploring the possibilities. Read about the client libraries here and the Office 365 APIs in the MSDN Library. More documentation is on the way!

Let’s see how it works. Once you install the tool, right-click on your project in the Solution Explorer and select Add – Connected Service...

image

This will launch the Services Manager where you log into your Office 365 developer site and select the permissions you require for each of the services you want to use.

image

Once you click OK, the client libraries are added to your project as well as sample code files to get you started. The client libraries help you perform the auth handshake and provide strong types for you to work with the services easier.

The important bits..

 const string MyFilesCapability = "MyFiles";
static DiscoveryContext _discoveryContext;

public static async Task<IEnumerable<IFileSystemItem>> GetMyFiles()
{
    var client = await EnsureClientCreated();

    // Obtain files in folder "Shared with Everyone"
    var filesResults = await client.Files["Shared with Everyone"].
        ToFolder().Children.ExecuteAsync();
            
    var files = filesResults.CurrentPage.OrderBy(e => e.Name);

    return files;
}
    
public static async Task<SharePointClient> EnsureClientCreated()
{
    if (_discoveryContext == null)
    {
        _discoveryContext = await DiscoveryContext.CreateAsync();
    }

    var dcr = await _discoveryContext.DiscoverCapabilityAsync(MyFilesCapability);
            
    var ServiceResourceId = dcr.ServiceResourceId;
    var ServiceEndpointUri = dcr.ServiceEndpointUri;

    // Create the MyFiles client proxy:
    return new SharePointClient(ServiceEndpointUri, async () =>
    {
        return (await _discoveryContext.AuthenticationContext.
            AcquireTokenSilentAsync(ServiceResourceId, 
            _discoveryContext.AppIdentity.ClientId,
            new Microsoft.IdentityModel.Clients.ActiveDirectory
                .UserIdentifier(dcr.UserId, 
                Microsoft.IdentityModel.Clients.ActiveDirectory
                .UserIdentifierType.UniqueId))).AccessToken;
    });
}

This code is using the Discovery Service to retrieve the rest endpoints (DiscoverCapabilityAsync). When we create the client proxy, the user is presented with a login to Office 365 and then they are asked to grant permission to our app. Once they authorize, we can access their Office 365 data.

If we look at the request, this call:

     var filesResults = await client.Files["Shared with Everyone"].
        ToFolder().Children.ExecuteAsync();

translates to (in my case):

 GET /personal/beth_bethmassi_onmicrosoft_com/_api/Files('Shared%20with%20Everyone')/Children

The response will be a feed of all the file (and any sub-folder) information stored in the requested folder.

Play around and discover the capabilities. There’s a lot you can do. I encourage you to take a look at the samples available on GitHub:

Also check out these video interviews I did this summer to learn more:

Enjoy!

Comments

  • Anonymous
    October 14, 2014
    Nice article Beth.  Do you know where I can find the current list of supported project types?  Cannot wait for O365 API to be supported in LSCBA Project template.  Have a great day!

  • Anonymous
    October 14, 2014
    The comment has been removed

  • Anonymous
    October 14, 2014
    And when are we getting a new Lightswitch version?

  • Anonymous
    October 14, 2014
    I know we released an update to the NuGet package for msls recently. What do you mean by new? Visual Studio 14?

  • Anonymous
    October 14, 2014
    Well the last one was in March and I know since then you were cleaning up the uservoice board so just wondering when the next release is?  It used to be every 3 or 4 months. BTW my personal need to recommend this for more projects at work is a custom header ability built in and better menu system for navigation.

  • Anonymous
    October 14, 2014
    Actually we released an update to the VS tooling more recently than that. It was VS Update 3 when we did the update work on the publishing wizard IIRC. March was a major update to catch up to the SharePoint app platform. Since then, the updates have been smaller because the changes to the apps platform have been smaller. Office 365 APIs have been the focus lately. I know there's a lot of tools to work on for the Office/SharePoint dev platform and the team is trying to keep up on all of them. We're building a lot of stuff so you'll see our updates jump around the multiple tooling experiences.

  • Anonymous
    October 14, 2014
    Ok fine I guess we will wait to see what you release. I would like to tell you though I have heard several Product Managers complain that they can't brand the app with a nice custom header like you might get with any other engine.  I realize mobile is the focus however so many other technologies have come up with responsive headers to accommodate desktop mode and mobile at the same time.

  • Anonymous
    October 19, 2014
    The comment has been removed

  • Anonymous
    October 19, 2014
    @pp8357hot - Not sure what you mean, you can SharePoint enable a LightSwitch app and publish to the SharePoint store/corporate catalog. LS sits on the apps side of the Office 365 offerings. Office is providing developers choices for building on Office 365. You can go the apps route or you can access services data directly.  

  • Anonymous
    October 22, 2014
    so Beth, you're gone from LS team or?

  • Anonymous
    October 23, 2014
    @Kivito - I usually avoid talking about internal org structures at Microsoft, since they change. But I've always been on the broader Visual Studio team. I've always blogged about my passions around business app development. I do admit that my duties have turned more internally (and I have a family now) so I don't get out and blog/speak as much as I'd like these days.

  • Anonymous
    October 23, 2014
    Well that's great, I'm happy for you and wish you more time for blogging and family! I just hope that guys will not become lazy without a strong female hand in the Lightswitch kitchen.. ;)

  • Anonymous
    February 12, 2015
    I also wish you all the best, Beth, and I was also impressed by your leadership and energy with LightSwitch. But look at the LightSwitch "team" blog sometime, and you'll be quite embarrassed for your company. What sane developer will ever hang their hat on Microsoft's "RAD" after the Silverlight and now LightSwitch debacle? Personally, I think LightSwitch would be by far the best LOB RAD tool out there, if we could be convinced it has a future. You invested a lot of energy in it. Could you possibly get to MS to give any information on all on it? Or make it open source?  Anything?...

  • Anonymous
    February 16, 2015
    Agree  completley. ..beth,  Microsoft produced a fantastic product in LS, they and you pushed it, sold it and evangelised thousands of loyal developers.  Then without  a sound, a comment or a simple post  it looks like they killed it.  I will probably continue to use LS for add long as possible, partly because  I have locked myself into it with clients,  partly because there's no clear alternative but  mostly despite Microsoft astounding disregard  for their  customers and their appalling support  LS is still a fantastic product that surpasses anything else out  there. BETH, if you can provide clarity on the future of a product that you worked so hard to push  us towards it would be really appreciated by everyone who bought into what you sold us.

  • Anonymous
    February 17, 2015
    I understand you guys are frustrated in the decline of community activity around LightSwitch. You're preaching to the choir. But I have picked up other responsibilities so unfortunately it's been impossible for me to keep up as well. I still use LightSwitch. I still recommend it for apps that fit. I don't think there is any better alternative to rapidly developing mobile SharePoint apps that connect to multiple data sources and don't need a fancy UI.  It's still supported and part of VS2015. And now it's FREE with Community edition. If you guys know of something better that fits this space, I want to know about it too! I'm sorry if you think I tried to sell you something because I'm not a sales person, I'm an engineer and a teacher.  I always have had a passion for teaching people how to develop applications. I try my best to show you how to use something to it's maximum potential. That's what I have always done here on this blog. There a many resources here, not just about LightSwitch. (Believe it or not, the most read article here BY FAR is about installing SQL Server.) That's because developing software is my passion. I have been working in the open source community in the last year and I have learned a TON. I have learned what can and can't be realistically open sourced as well. I hope to start blogging here and there again soon about some of my new adventures. I hope you guys can support me on my journey and not hate me because my life moved on. I've always have had the best intentions here. -Beth

  • Anonymous
    February 17, 2015
    The comment has been removed

  • Anonymous
    February 17, 2015
    Error: DiscoveryContext could not be found. How to fix this issue

  • Anonymous
    February 18, 2015
    @rusticcloud - I still love VB. But honestly, C# really isn't that painful anymore. It's a lot easier than JavaScript ;-) And you will be happy to note that C#6 borrows static usings & exception filtering from VB :-) blogs.msdn.com/.../new-features-in-c-6.aspx @visha - this post was about the pre-release version of O365 client libraries. You should check out Chak's post on what's updated and the new samples: chakkaradeep.com/.../update-to-office-365-api-tools-and-client-libraries

  • Anonymous
    February 24, 2015
    The comment has been removed

  • Anonymous
    March 15, 2015
    We are using visual studio 2012, need to integrate the Office 365 files. how to achieve this using the jquery libraries. it is hard to find out the solution. please help with the details.