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.

Office 365 IT Professional——RMS与SharePoint Online集成管理

客地址:    比如一个公司的项目团队,好多人为了能将自己的Idea展现给领导,希望被采纳都会积极献策,同时为了保证采纳后的想法...
  • ShelleyLiu0415
  • ShelleyLiu0415
  • 2015年08月20日 14:48
  • 821

Office 365 开发与实践 (一) 基本服务介绍

Office 365是微软提供的一种SaaS服务。提供了几乎所有Office的功能,并且允许开发者扩展现有功能。这篇博客简单介绍一下Office 365提供哪些基本的服务。...
  • shrenk
  • shrenk
  • 2016年10月06日 21:42
  • 1465

使用 Office 365 PowerShell 管理用户帐户和许可证(七) Office 365 许可和 Windows PowerShell:删除许可证

 Office 365 许可和 Windows PowerShell:删除许可证   上一次修改主题:2016-12-21 介绍如何使用 Office 365 PowerS...
  • jsjpanxiaoyu
  • jsjpanxiaoyu
  • 2017年05月16日 15:02
  • 256

Office 365 ProPlus 安装及管理

在前两篇文章中已经完成了O365用户的手动创建和批量导入并分配对应的许可。既然用户已经具备使用这个服务的许可了, 今天我们就一起来看看如何去使用O365,和本地的Office又有何差别,然后介绍使用即...
  • hanzheng260561728
  • hanzheng260561728
  • 2016年04月07日 10:30
  • 811

基于Office 365 无代码工作流分析-初试通知表单的制作!

3.2.1 初试通知表单制作我们先来看看InfoPath制作的初试通知表单: 这个表单中我们比较多的字段是直接用户填写的字段,比如姓名、身高、...
  • fogyisland2000
  • fogyisland2000
  • 2014年11月03日 22:53
  • 4859

如何申请国际版一年期的Office 365开发者账号

1、地址 2、点击Join now 3、填写内容 4、填完后,给注册邮箱发邮件 5、收到邮件,点击 red...
  • tom1995
  • tom1995
  • 2017年04月22日 10:40
  • 1223

将域名绑定到Office 365上

当我们申请Office365网站后,默认的登陆名字为 这样的格式比如我申请的域名是wumaoge,那我的登陆用户名为:admin@wumaoge....
  • accado
  • accado
  • 2014年01月16日 00:09
  • 8195

Code Snippets的使用

一.什么是代码片段(code snippets) 代码片段:指用来存放程序执行代码的一块内存区域。 诸如很多开发工具基本都集成了这一功能,可以说你在coding的时候经常都在使用它,或许你不知道你...
  • Chowbindeblog
  • Chowbindeblog
  • 2016年03月06日 14:20
  • 281

《Office 365 开发入门指南》

终于等来了这一天,可以为我的这本新书画上一个句号。我记得是在今年的2月份从西雅图回来之后,就萌发了要为中国的Office 365开发人员写一些东西并最终能帮到更多中国用户的想法,而从2月26日正式写下...
  • sD7O95O
  • sD7O95O
  • 2017年12月29日 00:00
  • 256

用 PHP 连接上 Office 365 REST API 进行操作入门

Office 365 简单说就是云中的 Office,是能够提供最佳生产力和高效协同的高端云服务,是微软公司基于云平台的应用套件。 Office 365 REST API 包含了对以下数据的操作: E...
  • u014296016
  • u014296016
  • 2015年04月14日 17:06
  • 1496
您举报文章:Office 365 Code Snippets for Windows