Categories

Copying Appointments from Exchange to a SharePoint Calendar

Recently I had the opportunity to programmatically copy Exchange appointments to a SharePoint calendar. This was done using the Exchange web services API. I found no examples of this procedure online, so I thought I would add my own example. This example looks 62 days into the future and copies appointments from a shared calendar in Exchange located in the public folders root.


// Here are the references you'll need
using System;
using System.Linq;
using System.Net;
using Microsoft.SharePoint;
using Microsoft.SharePoint.ApplicationPages.Calendar.Exchange;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Meetings;
using Microsoft.Exchange.WebServices.Data;

Warning: the following code includes a lot of try-catches but in my experience this was necessary.

// service is the Exchange Service - there are numerous examples elsewhere on how to obtain this
var publicFolders = Folder.Bind(service, WellKnownFolderName.PublicFoldersRoot);
// I'm only interested in one folder
var folderView = new FolderView(1);
// The public calendar/folder name is "Public Calendar"
var searchFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Public Calendar");
var findFoldersResult = publicFolders.FindFolders(searchFilter, folderView).First();
var calendarView = new CalendarView(DateTime.Today, DateTime.Today.AddDays(62));
var appointments = service.FindAppointments(findFoldersResult.Id, calendarView);
if (appointments.Any()) service.LoadPropertiesForItems(from Item item in appointments select item, PropertySet.FirstClassProperties);
foreach (var appointment in appointments)
{
	// GetItem checks calendarList for the existence of an item identified by the UniqueId. It either returns that item
	// or creates a new one with the custom column containing the UniqueId set to the value passed in
	var calendarEvent = GetItem(calendarList, appointment.Id.UniqueId);
	try
	{
		calendarEvent[SPBuiltInFieldId.Title] = appointment.Subject;
	}
	catch {}
	try
	{
		calendarEvent[SPBuiltInFieldId.StartDate] = appointment.Start;
	}
	catch { }
	try
	{
		calendarEvent[SPBuiltInFieldId.EndDate] = appointment.End;
	}
	catch { }
	try
	{
		calendarEvent[SPBuiltInFieldId.fAllDayEvent] = appointment.IsAllDayEvent;
	}
	catch { }
	try
	{
		calendarEvent[SPBuiltInFieldId.Location] = appointment.Location;
	}
	catch { }
	try
	{
		calendarEvent[SPBuiltInFieldId.FreeBusy] = appointment.LegacyFreeBusyStatus == LegacyFreeBusyStatus.NoData ?
			null : appointment.LegacyFreeBusyStatus.ToString();
	}
	catch { }
	try
	{
		calendarEvent[SPBuiltInFieldId.Description] = appointment.Body.Text;
	}
	catch { }
	try
	{
		var categories = new SPFieldMultiChoiceValue();
		foreach (var category in appointment.Categories)
		{
			categories.Add(category);
		}
		calendarEvent[SPBuiltInFieldId.Category] = categories;
	}
	catch { }
	try
	{
		var attendees = new SPFieldUserValueCollection();
		attendees.AddRange(appointment.RequiredAttendees.Select(attendee => web.AllUsers.GetByEmail(attendee.Address)).Select(user => new SPFieldUserValue(web, user.ID, user.Name)));
		calendarEvent[SPBuiltInFieldId.ParticipantsPicker] = attendees;
	}
	catch { }
	calendarEvent.Update();
}

Learn more about DMC's Microsoft consulting services.

Comments

There are currently no comments, be the first to post one.

Post a comment

Name (required)

Email (required)

CAPTCHA image
Enter the code shown above:

Categories

Related Blog Posts