In this series, I will talk about how to start programming with Office 365 in shortest investment of time. I assume my audience will be .NET developers who do not have prior knowledge of Office 365.
Prerequisite: Programming Office 365 Jump Start 1
Exchange Online
Microsoft released Exchange Web Services Managed API SDK for programming messaging solutions for Exchange Online. The SDK is a class library that you can reference in your application and use its methods to connect to Exchange Online and consume its services. Version 1.1 is available at http://bit.ly/z1EY06, but version 1.2 is soon to be released of which documentation can be found here: http://bit.ly/yHcqVE. After installing the SDK, an assembly is available at C:Program FilesMicrosoftExchangeWebServices1.1Microsoft.Exchange.WebServices.dll for accessing Email, Calendar, Tasks and Contacts hosted in the cloud.
Authenticating to Exchange Online
Using the SDK, authenticating to Exchange Online with admin username and password is fairly easy:
If the user is already logged on to that domain, username and password are not required. Use exchange.UseDefaultCredentials = true instead.
Querying User Availability
Prior to scheduling an appointment among two or more attendees, it is better to check whether they are free/busy at that time. Otherwise, it may result in conflict with other appointments.
var attendees = new List<AttendeeInfo> { new AttendeeInfo() { SmtpAddress = "attendee1@mycompany.onmicrosoft.com", AttendeeType = MeetingAttendeeType.Organizer }, new AttendeeInfo() { SmtpAddress = "attendee2@mycompany.onmicrosoft.com", AttendeeType = MeetingAttendeeType.Required } }; var results = exchange.GetUserAvailability(attendees, new TimeWindow(DateTime.Now, DateTime.Now.AddDays(3)), AvailabilityData.FreeBusyAndSuggestions);
The code above returns whole bunch of information on availability and suggestions (responsible flag for this was AvailabilityData.FreeBusyAndSuggestions) according to availability of the attendees for next three days. You can go ahead and explore by yourself various types of properties this method reveals, but let us take a look at how we can get access to the suggestions that it made:
It is also capable of calculating conflicts automatically and return as part of the TimeSuggestion object.
Creating an Appointment
Now that we have learned how to query user’s availability, let us pick up a suggested time and create an appointment:
var appointment = new Appointment(exchange) { Subject = "Discuss Exchange migration", Body = "Let us find a migration process that will cause least downtime of the service.", Start = appointmentTime, End = appointmentTime.AddMinutes(30) }; appointment.RequiredAttendees.Add( "attendee2@mycompany.onmicrosoft.com"); appointment.Save();
Sending an Email
The quality indicator of a good SDK is that it gives a solid object model and makes it fairly easy to consume services such as sending email while taking care of the underlying complexities associated with communications all by itself:
var email = new EmailMessage(exchange) { Body = "Hello from my cool C# demo.", Subject = "Email by code" }; email.Attachments.AddFileAttachment("c:\mypic.jpg"); email.ToRecipients.Add("user@mycompany.onmicrosoft.com"); email.Send();
Accessing Folders
In this example, we will get access to a folder and delete all its contents including sub-folders:
var folder = Folder.Bind(exchange, WellKnownFolderName.DeletedItems); folder.Empty(DeleteMode.HardDelete, true);
If we wanted to search for folders that contain certain string, instead of well known folders, we can do so as well. First of all we have to decide our search criteria, then call ExchangeService’s FindFolders method, which takes a folder root to search from, search criteria (filter) and maximum how many folders we are interested in getting in return:
var filter = new SearchFilter.ContainsSubstring( FolderSchema.DisplayName, "Reports"); var results = exchange.FindFolders( WellKnownFolderName.MsgFolderRoot, filter, new FolderView(10)); foreach (var folder in results) { Console.WriteLine(folder.DisplayName); // If we have three folders contain 'Reports', output: // Annual Reports // Monthly Reports // Report formats }
Scheduling Out of Office
Let us schedule an Out of Office automatic reply starting from now for five days, which will send message to people at work only (Internal):
var oofSettings = new OofSettings { InternalReply = new OofReply("Hi, thank you for your email, but I am out of office."), Duration = new TimeWindow(DateTime.Now, DateTime.Now.AddDays(5)), State = OofState.Scheduled }; exchange.SetUserOofSettings( "admin@mycompany.onmicrosoft.com", oofSettings);
Notification Streaming
Exchange Web Service SDK allows to listen for change notifications whether it’d be new email arrival, Free/Busy status changed, item created/deleted and so on. Let us see how we can listen for new email arrival:
var subscription = exchange.SubscribeToStreamingNotifications( new FolderId[] { WellKnownFolderName.Inbox }, EventType.NewMail); var connection = new StreamingSubscriptionConnection(exchange, 30); connection.AddSubscription(subscription); connection.OnNotificationEvent += (s, a) => { foreach (var item in a.Events.Select( notification => notification as ItemEvent)) { Console.WriteLine(string.Format( "Type: {0}, ItemId: {1}", item.EventType, item.ItemId.UniqueId)); } }; connection.Open();
Conclusion
There are countless many things that you can do with the object model. I hope this post has enough exercises to get you started with Exchange Online programming.
Pingback: Programming Office 365 Jump Start 1 | Tanzim Saqib
Pingback: Dew Drop – January 8, 2012 (#1,236) | Alvin Ashcraft's Morning Dew