Office 365 Code Snippets for Windows

转载 2015年11月17日 16:25:23

The Office 365 Windows Snippets project contains a repository of code snippets that show you how to use the client libraries from the Office 365 API tools to interact with Office 365 objects, including users, groups, calendars, contacts, mail, files, and folders.

These snippets are simple and self-contained, and you can copy and paste them into your own code, whenever appropriate, or use them as a resource for learning how to use the client libraries.

The image below shows what you'll see when you launch the app.

You can choose to run all of the snippets, or just the ones you select. After you choose to run, you’ll be prompted to authenticate with your Office 365 account credentials, and the snippets will run.

Note: This project contains code that authenticates and connects a user to Office 365, but if you want to learn about authentication specifically, look at the Connecting to Office 365 in Windows Store, Phone, and universal apps.


This sample requires the following:

Register and configure the app

You can register the app with the Office 365 API Tools for Visual Studio. Be sure to download and install the Office 365 API tools from the Visual Studio Gallery.

Note: If you see any errors while installing packages (for example, Unable to find "Microsoft.IdentityModel.Clients.ActiveDirectory") make sure the local path where you placed the solution is not too long/deep. Moving the solution closer to the root of your drive resolves this issue.

  1. Open the O365-Win-Snippets.sln file using Visual Studio 2013.
  2. Build the solution. The NuGet Package Restore feature will load the assemblies listed in the packages.config file. You should do this before adding connected services in the following steps so that you don't get older versions of some assemblies.
  3. In the Solution Explorer window, right-click each project name and select Add -> Connected Service.
  4. A Services Manager dialog box will appear. Choose Office 365 and then Register your app.
  5. On the sign-in dialog box, enter the user name and password for your Office 365 tenant. This user name will often follow the pattern If you don't already have an Office 365 tenant, you can get a free Developer Site as part of your MSDN Benefits or sign up for a free trial. After you're signed in, you will see a list of all the services. No permissions will be selected, since the app is not registered to use any services yet.

  6. To register for the services used in this sample, choose the following services and permissions:

    • Calendar - Read and write to your calendars.
    • Contacts - Read and write to your contacts.
    • Mail - Read and write to your mail. Send mail as you.
    • My Files - Read and write your files.
    • Users and Groups - Sign you in and read your profile. Access your organization's directory.


After you click OK in the Services Manager dialog box, you can select Build Solution from the Build menu to load the Microsoft.IdentityModel.Clients.ActiveDirectory assembly, or you can wait until you debug.

Build and debug

After you've loaded the solution in Visual Studio, press F5 to build and debug. Run the solution and sign in with your organizational account to Office 365.

How the sample affects your tenant data

This sample runs REST commands that create, read, update, or delete data. When running commands that delete or edit data, the sample creates fake entities. The fake entities are deleted or edited so that your actual tenant data is unaffected. The sample will leave behind fake entities on your tenant.

Add a snippet

This project includes five snippets files: Calendar\CalendarSnippets.cs, Contacts\ContactsSnippets.cs, Email\EmailSnippets.cs, Files\FilesSnippets.cs, and UsersAndGroups\UsersAndGroupsSnippets.cs.

If you have a snippet of your own and you would like to run it in this project, just follow these three steps:

  1. Add your snippet to the snippets file. Be sure to include a try/catch block. The snippet below is an example of a simple snippet that gets one page of calendar events:

    public static async Task<List<IEvent>> GetCalendarEventsAsync()
            // Make sure we have a reference to the Exchange client
            OutlookServicesClient client = await GetOutlookClientAsync();
            IPagedCollection<IEvent> eventsResults = await client.Me.Calendar.Events.ExecuteAsync();
            // You can access each event as follows.
            if (eventsResults.CurrentPage.Count > 0)
                string eventId = eventsResults.CurrentPage[0].Id;
                Debug.WriteLine("First event:" + eventId);
            return eventsResults.CurrentPage.ToList();
        catch { return null; }
  2. Create a story that uses your snippet and add it to the associated stories file. For example, theTryCreateCalendarEventAsync() story uses the AddCalendarEventAsync () snippet inside the Calendar\CalendarStories.cs file:

    public static async Task<bool> TryCreateCalendarEventAsync()
        var newEventId = await CalendarSnippets.AddCalendarEventAsync();
        if (newEventId == null)
            return false;
        await CalendarSnippets.DeleteCalendarEventAsync(newEventId);
        return true;

    Sometimes your story will need to run snippets in addition to the one that you're implementing. For example, if you want to update an event, you first need to use the AddCalendarEventAsync() method to create an event. Then you can update it. Always be sure to use snippets that already exist in the snippets file. If the operation you need doesn't exist, you'll have to create it and then include it in your story. It's a best practice to delete any entities that you create in a story, especially if you're working on anything other than a test or developer tenant.

  3. Add your story to the story collection in MainPageXaml.cs (inside the CreateTestList() method):

    StoryCollection.Add(new StoryDefinition() { GroupName = "Calendar", Title = "Create", RunStoryAsync = CalendarStories.TryCreateCalendarEventAsync });

Now you can test your snippet. When you run the app, your snippet will appear as a new box in the grid. Select the box for your snippet, and then run it. Use this as an opportunity to debug your snippet.

Code snippets for tips

//将参数输出, 每次注意输出一个空格, 如果是最后一个那就不用输出空格了 printf("%s%s", argv[i], (i...
  • dscyw
  • dscyw
  • 2014年03月25日 14:00
  • 439

What are Groups for Office 365?

Groups for Office 365 is a new way of working with your colleagues and people outside of your networ...

Office 365 Starter Project for ASP.NET MVC

 Overview This sample uses the Office 365 APIs client libraries to demonstrate basic operation...

微软发布了一系列网络安全新功能(关于Windows和Office 365)

最新Microsoft Office 365 商业版下载>>> 最新Microsoft Office 365 商业协作版下载>>> 最新Microsoft Office 365...

Windows Azure 和 Office 365

[原文发表地址] 上周微软...

微软提供的三种核心服务:Windows+Office 365+Azure

微软提供的三种核心服务:Windows+Office 365+Azure 微软目前是全球最大的电脑软件提供商,消费市场、企业市场和云计算是微软最关注的领域,也是整个行业未来关注的重点,对应微软的三种核...

【Windows Azure】SAAS 多租户模式的Office 365应用开发指南

企业已经越来越多的把基础架构迁移到Windows Azure以及Office 365的办公环境,对于应用程序集成开放商来说,如何适用这种开发模式的转变,从原有面向本地服务器的编程访问方式,转换成面向云...

PowerShell for Office 365 mobi

  • 2017年09月30日 10:41
  • 6.89MB
  • 下载

PowerShell for Office 365 epub

  • 2017年09月30日 10:38
  • 6.13MB
  • 下载
您举报文章:Office 365 Code Snippets for Windows