Blog

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.

code
1
2
3
4
5
6
7
8
9
10
// 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.

code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// 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.

Related Blog Posts

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:

loading