Hello Everyone,
In a following post we will see how to send Meeting
Invitation through Exchange Server API. As part of our Example , I have one Calendar
List and user will navigate to Calendar and create new Item. when user submits the form a mail will
be triggers to the Attendees and it will block attendees Outlook Calendar .
Prerequisites:
For EWS Integration (if client Exchange server is hosted on premise).Need
to do following things @ client environment.
Install EWS Managed API 2.0 into SharePoint server. Size of
this API is 1 MB approx.
Please download from
http://www.microsoft.com/en-us/download/details.aspx?id=35371
Configure Exchange Impersonation for all users in an organization
(Exchange Management Shell is required for running scripts).
In one of my posts we have seen "Create Exchage Server Connection via impersonate user account by Exchange API" how to create Exchange Server Connection via impersonate user account.
Please find the Event Receiver Code below,In following code we are creating Meetings (normal),All Day Events and Recurrence Events .
Please find the Event Receiver Code below,In following code we are creating Meetings (normal),All Day Events and Recurrence Events .
using Microsoft.Exchange.WebServices.Data; using Microsoft.Office.DocumentManagement; using Microsoft.SharePoint; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Mail; using System.Net.Mime; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Text.RegularExpressions; namespace ExchageMeetingRequest { /// <summary> /// List Item Events /// </summary> public class MeetingInvitation : SPItemEventReceiver { public static string url = string.Empty; public string siteURL = string.Empty; public static bool attendeesChanged = false; public static bool scheduleChanged = false; public static bool itemChangedToRecurrence = false; public static SPFieldUserValueCollection newAttendees = new SPFieldUserValueCollection(); public static string recurrencevalue = string.Empty, title = string.Empty, objective = string.Empty; public string meetingForwardBy = string.Empty; InsertUpdateMeetingData updateMeetingData = new InsertUpdateMeetingData(); /// <summary> /// An item is being updated. /// </summary> public override void ItemUpdating(SPItemEventProperties properties) { SPWeb web = properties.Web; if (properties.ListTitle == "Meetings") { try { bool scheduleChanged = false; DateTime beforeEventDateTime = (DateTime)properties.ListItem["EventDate"]; DateTime afterEventDateTime = new DateTime(); DateTime beforeEndDateTime = (DateTime)properties.ListItem["EndDate"]; DateTime afterEndDateTime = new DateTime(); if (properties.AfterProperties["EventDate"] != null && properties.AfterProperties["EndDate"] != null) { afterEventDateTime = DateTime.Parse(properties.AfterProperties["EventDate"] as string).ToUniversalTime(); afterEndDateTime = DateTime.Parse(properties.AfterProperties["EndDate"] as string).ToUniversalTime(); //Checking whether Meeting dates are changed or not if (beforeEventDateTime != afterEventDateTime || beforeEndDateTime != afterEndDateTime) { scheduleChanged = true; } } if (properties.AfterProperties["Attendees"] != null) { SPFieldUserValueCollection beforeAttendees = new SPFieldUserValueCollection(web, properties.ListItem["Attendees"].ToString()); SPFieldUserValueCollection afterAttendees = new SPFieldUserValueCollection(web, properties.AfterProperties["Attendees"].ToString()); bool presentInBeforeAttendees = false; if (newAttendees != null) { if (newAttendees.Count > 0) { newAttendees.Clear(); } } foreach (SPFieldUserValue afterAttendee in afterAttendees) { foreach (SPFieldUserValue beforeAttendee in beforeAttendees) { if (afterAttendee.User.LoginName == beforeAttendee.User.LoginName) { presentInBeforeAttendees = true; break; } else { presentInBeforeAttendees = false; } } if (!presentInBeforeAttendees) { newAttendees.Add(afterAttendee); } } if (newAttendees.Count > 0) { attendeesChanged = true; SPFieldUserValue forwardBy = new SPFieldUserValue(web, properties.AfterProperties["Editor"].ToString()); if (forwardBy != null) meetingForwardBy = forwardBy.User.Name; } } } catch (Exception ex) { //Exception Handling } } } /// <summary> /// An item was added. /// </summary> public override void ItemAdded(SPItemEventProperties properties) { if (properties.ListTitle == "Meetings") { try { int iUID = 0; SPWeb web = properties.OpenWeb(); siteURL = web.Url; SPListItem item = properties.ListItem; string CreatedByEmail = string.Empty; if (properties.ListTitle == "Meetings") { MailAddressCollection macCollection = new MailAddressCollection(); title = Convert.ToString(item["Title"]); objective = Convert.ToString(item["Meeting Objective"]); //reading Meeting Agenda - Custom column which was created in Meetings(Calendar) List string agenda = Convert.ToString(item["Meeting Agenda"]); int iUID = item["ID"]; SPFieldUserValue createdByUserValue = new SPFieldUserValue(web, Convert.ToString(item["Created By"])); if (!string.IsNullOrEmpty(createdByUserValue.User.Email)) CreatedByEmail = Convert.ToString(createdByUserValue.User.Email); else CreatedByEmail = Constants.Admin; SPFieldUserValueCollection attendeesUserValueCollection = (SPFieldUserValueCollection)item["Attendees"]; item["Attendees"] = attendeesUserValueCollection; if (attendeesUserValueCollection != null) { foreach (SPFieldUserValue fuv in attendeesUserValueCollection) { if (!string.IsNullOrEmpty(fuv.User.Email)) macCollection.Add(new MailAddress(fuv.User.Email)); } } SPSecurity.RunWithElevatedPrivileges(delegate() { objective = "Meeting Objective"; if (macCollection.Count != 0) { bool isAllDay = false; bool isRecurrance = false; //Get Event Author SPFieldUserValue user = new SPFieldUserValue(properties.Web, properties.ListItem["Author"] as string); // Create an Exchange connection via impersonationg Author account. ExchangeServiceConnection conn = new ExchangeServiceConnection(user.User.Email, user.User.LoginName); //Get all attendees. SPFieldUserValueCollection col = (SPFieldUserValueCollection)properties.ListItem["Attendees"]; Regex reg = new Regex(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|local|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b"); isAllDay = (bool)properties.ListItem["All Day Event"]; isRecurrance = (bool)properties.ListItem["Recurrence"]; if (isRecurrance) ReOccuranceMeeting(properties, user, conn, col, reg, iUID, url, web); else CreateMeetingRequest(properties, user, conn, col, reg, isAllDay, iUID, url, web); } }); } } catch (Exception ex) { //Exception Handling } } } /// <summary> /// An item was updated. /// </summary> public override void ItemUpdated(SPItemEventProperties properties) { if (properties.ListTitle == "Meetings") { try { string agenda = string.Empty; SPWeb web = properties.OpenWeb(); siteURL = web.Url; SPListItem item = properties.ListItem; MailAddressCollection macCollection = new MailAddressCollection(); string CreateEmail = string.Empty; title = item["Title"].ToString(); objective = item["Meeting Objective"].ToString(); agenda = Convert.ToString(item["Meeting Agenda"]); int UID = item["ID"]; SPFieldUserValue createdByUserValue = new SPFieldUserValue(web, Convert.ToString(item["Created By"])); if (!string.IsNullOrEmpty(createdByUserValue.User.Email)) { CreateEmail = Convert.ToString(createdByUserValue.User.Email); } SPFieldUserValueCollection attendeesUserValueCollection = (SPFieldUserValueCollection)item["Attendees"]; item["Attendees"] = attendeesUserValueCollection; foreach (SPFieldUserValue fuv in attendeesUserValueCollection) { if (!string.IsNullOrEmpty(fuv.User.Email)) macCollection.Add(new MailAddress(fuv.User.Email)); } if (properties.ListItem["Meeting URL"] != null) { title = item["Title"].ToString(); if (item["Meeting Objective"] != null) objective = Convert.ToString(item["Meeting Objective"]); SPSecurity.RunWithElevatedPrivileges(delegate() { if (attendeesChanged) { if (item["Meeting Type"].ToString() == "Decision Meeting") { using (SPSite spSite = new SPSite(siteURL)) { using (SPWeb spWeb = spSite.OpenWeb(Constants.DecisionMakingWebName)) { url = "MeetingUrl"; objective = "Meeting Objective"; } } } MailAddressCollection macColNewattendees = new MailAddressCollection(); foreach (SPFieldUserValue fuv in newAttendees) { if (!string.IsNullOrEmpty(fuv.User.Email)) macColNewattendees.Add(new MailAddress(fuv.User.Email)); } if (macColNewattendees.Count != 0) { SPFieldUserValue user = new SPFieldUserValue(properties.Web, properties.ListItem["Author"] as string); //Get Event Author // Create an Exchange connection via impersonationg Author account. ExchangeServiceConnection conn = new ExchangeServiceConnection(user.User.Email, user.User.LoginName); //Get Appointment ID var appid = properties.ListItem["ApplicationId"]; SendMeetingRequestToNewAttendee(web, properties.ListTitle, item["ID"], CreateEmail, "Meeting Invitation", macColNewattendees, title, objective, UID, url, meetingForwardBy, conn, appid); } attendeesChanged = false; meetingForwardBy = string.Empty; newAttendees = null; } if (scheduleChanged) { if (macCollection.Count != 0) { bool isAllDay = false; isAllDay = (bool)properties.ListItem["All Day Event"]; if (properties.ListItem.Fields.ContainsField("ApplicationId")) //if contains AppointmentID field. { //Get Event Author SPFieldUserValue user = new SPFieldUserValue(properties.Web, properties.ListItem["Author"] as string); // Create an Exchange connection via impersonationg Author account. ExchangeServiceConnection conn = new ExchangeServiceConnection(user.User.Email, user.User.LoginName); //Get all attendees. SPFieldUserValueCollection col = (SPFieldUserValueCollection)properties.ListItem["Attendees"]; if (col == null) //if not any attendees exists . (This could be null if attendees column not added to Event Content Type) { } var appid = properties.ListItem["ApplicationId"]; //Get Appointment ID //Create a regular expression for checkin mail format. Regex reg = new Regex(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|local|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b"); if (appid == null) CreateMeetingRequest(properties, user, conn, col, reg, isAllDay, UID, url, web); else //if appointment exist reschedule existing appointment ReScheduleMeetingRequest(properties, user, conn, col, appid, reg, UID, url, web); } } scheduleChanged = false; } }); } } catch (Exception ex) { //Exception Handling } } } /// <summary> /// This Method is used to create Reoccurance Meeting /// </summary> private void ReOccuranceMeeting(SPItemEventProperties properties, SPFieldUserValue user, ExchangeServiceConnection conn, SPFieldUserValueCollection col, Regex reg, int MeetingID, string MeetingURL, SPWeb web) { try { string Title = string.Empty; string meetingAgenda = string.Empty; string MObjective = string.Empty; string URL = string.Empty; string ApplicationId = string.Empty; SPList spList = web.Lists.TryGetList(Constants.MeetingMasterListName); SPListItem item = spList.GetItemById(MeetingID); if (item != null) { Title = Convert.ToString(item["Title"]); meetingAgenda = Convert.ToString(item["Meeting_x0020_Agenda"]); MObjective = Convert.ToString(item["Meeting_x0020_Objective"]); } URL = MeetingURL; MeetingLoc = "Meeting Location"; string recurrence = Convert.ToString(properties.ListItem["RecurrenceData"]); Appointment meeting = new Appointment(conn.Service); //Create a new appointment object // Set properties on the meeting. meeting.Subject = Convert.ToString(properties.ListItem["Meeting_x0020_Subject"]); //set subject meeting.Start = (DateTime)properties.ListItem["EventDate"]; //set Start date DateTime endDateTime = (DateTime)properties.ListItem["EndDate"]; meeting.End = meeting.Start.Date.AddHours(endDateTime.Hour).AddMinutes(endDateTime.Minute).AddSeconds(endDateTime.Second); meeting.Location = Convert.ToString(properties.ListItem["Location"]); //set location meeting.Body = "Assigning Meeting Body"; foreach (var itemAttendee in col) //loop in Attendees. { if (!string.IsNullOrEmpty(itemAttendee.User.Email) && reg.IsMatch(itemAttendee.User.Email)) //check mail existance and mail format { meeting.RequiredAttendees.Add(itemAttendee.User.Email); } } recurrence = recurrence.ToLower(); Regex pattern = new Regex(@"<repeat><(?<FREQ>\w+)\s(?<OPTIONS>.*)/></repeat>"); Match match = pattern.Match(recurrence); //gets the pattern type String freq = match.Groups["FREQ"].ToString().ToUpper(); //gets the pattent options String options = match.Groups["OPTIONS"].ToString().ToLower(); // Build a regex to parse out the interval pattern = new Regex(@"(day|week|month|year)frequency=""(?<INTERVAL>\d+)"""); string[] ops = options.Split('='); //String interval = ""; match = pattern.Match(options); // Pull out the interval String interval = match.Groups["INTERVAL"].ToString(); // The list of day values we're going to scan for String[] days = new String[] { "SU", "MO", "TU", "WE", "TH", "FR", "SA" }; if (string.IsNullOrEmpty(interval)) interval = "1"; if (freq.Equals("DAILY")) { meeting.Recurrence = new Recurrence.DailyPattern(meeting.Start.Date, Convert.ToInt32(interval)); } else if (freq.Equals("DAILY") && options.Contains(@"weekday=""true""")) { days = new String[] { "MO", "TU", "WE", "TH", "FR" }; freq = "WEEKLY"; } if (freq.Equals("WEEKLY")) { //interval = ops.Last().Replace("'", ""); List<DayOfTheWeek> byday = new List<DayOfTheWeek>(); //DayOfTheWeek[] byday = new DayOfTheWeek[] { }; foreach (String d in days) { // Is this day set in the recurrence options? if (options.Contains(d.ToLower() + @"=""true""")) { switch (d) { case "SU": byday.Add(DayOfTheWeek.Sunday); break; case "MO": byday.Add(DayOfTheWeek.Monday); break; case "TU": byday.Add(DayOfTheWeek.Tuesday); break; case "WE": byday.Add(DayOfTheWeek.Wednesday); break; case "TH": byday.Add(DayOfTheWeek.Thursday); break; case "FR": byday.Add(DayOfTheWeek.Friday); break; case "SA": byday.Add(DayOfTheWeek.Saturday); break; } } } meeting.Recurrence = new Recurrence.WeeklyPattern(meeting.Start.Date, Convert.ToInt32(interval), byday.ToArray()); } else if (freq.Equals("MONTHLY")) { //interval = ops[1].Replace("'",""); // Build a regex to parse out the day in a "every N months on the Nth" pattern pattern = new Regex(@"\sday=""(?<BYDAY>\d+)"""); // Match against the pattern options XML match = pattern.Match(options); // Pull out the by day option String byDay = match.Groups["BYDAY"].ToString().ToUpper(); if (string.IsNullOrEmpty(byDay)) byDay = "1"; meeting.Recurrence = new Recurrence.MonthlyPattern(meeting.Start.Date, Convert.ToInt32(interval), Convert.ToInt32(byDay)); } // Parse out yearly-specific options else if (freq.Equals("YEARLY")) { // Build a regex to parse out the month in a "every N (month) Nrd" pattern pattern = new Regex(@"\smonth=""(?<BYMONTH>\d+)"""); // Match against the pattern options XML match = pattern.Match(options); // Pull out the by month option String byMonth = match.Groups["BYMONTH"].ToString().ToUpper(); //interval = ops[1].Replace("'",""); // Build a regex to parse out the day in a "every N months on the Nth" pattern pattern = new Regex(@"\sday=""(?<BYDAY>\d+)"""); // Match against the pattern options XML match = pattern.Match(options); // Pull out the by day option String byDay = match.Groups["BYDAY"].ToString().ToUpper(); if (string.IsNullOrEmpty(byDay)) byDay = "1"; Month m = (Month)Convert.ToInt32(byMonth); meeting.Recurrence = new Recurrence.YearlyPattern(meeting.Start.Date, m, Convert.ToInt32(byDay)); } else if (freq.Equals("MONTHLYBYDAY") || freq.Equals("YEARLYBYDAY")) { // Parse out the weekdayOfMonth property into an index we can use DayOfTheWeekIndex di = GetWeekdayOfMonthIndex(options); // The list of day values we're going to scan for days = new String[] { "SU", "MO", "TU", "WE", "TH", "FR", "SA" }; DayOfTheWeek dow = new DayOfTheWeek(); foreach (String d in days) { // Is this day set in the recurrence options? if (options.Contains(d.ToLower() + @"=""true""")) { switch (d) { case "SU": dow = DayOfTheWeek.Sunday; break; case "MO": dow = DayOfTheWeek.Monday; break; case "TU": dow = DayOfTheWeek.Tuesday; break; case "WE": dow = DayOfTheWeek.Wednesday; break; case "TH": dow = DayOfTheWeek.Thursday; break; case "FR": dow = DayOfTheWeek.Friday; break; case "SA": dow = DayOfTheWeek.Saturday; break; } } } meeting.Recurrence = new Recurrence.RelativeMonthlyPattern(meeting.Start.Date, Convert.ToInt32(interval), dow, di); } // Build a regex to parse out the number of occurrences pattern = new Regex(@"<repeatinstances>(?<REPEAT>\d+)</repeatinstances>"); // Match against the XML match = pattern.Match(recurrence); // Pull out the number of occurrences String repeat = match.Groups["REPEAT"].ToString(); // Build a regex to parse out the end by date pattern = new Regex(@"<windowend>(?<ENDDATE>.+)</windowend>"); // Match against the XML match = pattern.Match(recurrence); // Pull out the end date String endDate = match.Groups["ENDDATE"].ToString(); meeting.Recurrence.StartDate = meeting.Start.Date; if (!string.IsNullOrEmpty(repeat)) meeting.Recurrence.NumberOfOccurrences = Convert.ToInt16(repeat); if (!string.IsNullOrEmpty(endDate)) meeting.Recurrence.EndDate = Convert.ToDateTime(endDate); // Create the meeting and send the meeting invitation to attendees. meeting.Save(SendInvitationsMode.SendOnlyToAll); properties.ListItem["ApplicationId"] = meeting.Id.UniqueId; //update event item properties.ListItem.SystemUpdate(); } catch (Exception ex) { // } } /// <summary> /// Used to create Meeting Request /// </summary> private void CreateMeetingRequest(SPItemEventProperties properties, SPFieldUserValue user, ExchangeServiceConnection conn, SPFieldUserValueCollection col, Regex reg, bool isAllDay, int MeetingID, string MeetingURL, SPWeb web) { try { string Title = string.Empty; string meetingAgenda = string.Empty; string MObjective = string.Empty; string URL = string.Empty; SPList spList = web.Lists.TryGetList(Constants.MeetingMasterListName); SPListItem item = spList.GetItemById(Convert.ToInt32(MeetingID)); if (item != null) { Title = Convert.ToString(item["Title"]); MObjective = Convert.ToString(item["Meeting_x0020_Objective"]); Description = Convert.ToString(item["Description"]); } URL = MeetingURL; Appointment app = new Appointment(conn.Service); //Create a new appointment object app.Subject = Convert.ToString(properties.ListItem["Meeting_x0020_Subject"]); //set subject app.Start = (DateTime)properties.ListItem["EventDate"]; //set Start date app.End = (DateTime)properties.ListItem["EndDate"]; //set end date app.Body = "Set Meeting Request Body"; foreach (var itemAttendee in col) //loop in Attendees. { if (!string.IsNullOrEmpty(itemAttendee.User.Email) && reg.IsMatch(itemAttendee.User.Email)) //check mail existance and mail format app.RequiredAttendees.Add(itemAttendee.User.Email); } if (isAllDay) app.IsAllDayEvent = true; app.Save(); //save appointment object this could send meeting requests to attendees. properties.ListItem["ApplicationId"] = app.Id.UniqueId; //update event item properties.ListItem.SystemUpdate(); } catch (Exception ex) { //Exception Handling } } /// <summary> /// /// </summary> private void ReScheduleMeetingRequest(SPItemEventProperties properties, SPFieldUserValue user, ExchangeServiceConnection conn, SPFieldUserValueCollection col, object appid, Regex reg, int MeetingID, string MeetingURL, SPWeb web) { try { string Title = string.Empty; string Subject = string.Empty; string meetingAgenda = string.Empty; string MObjective = string.Empty; string URL = string.Empty; SPList spList = web.Lists.TryGetList(Constants.MeetingMasterListName); SPListItem item = spList.GetItemById(Convert.ToInt32(MeetingID)); if (item != null) { Title = Convert.ToString(item["Title"]); meetingAgenda = Convert.ToString(item["Meeting_x0020_Agenda"]); MObjective = Convert.ToString(item["Meeting_x0020_Objective"]); URL = MeetingURL; Appointment app = Appointment.Bind(conn.Service, new ItemId(appid.ToString())); app.Subject = Convert.ToString(properties.ListItem["Meeting_x0020_Subject"]); //set subject app.Start = (DateTime)properties.ListItem["EventDate"]; //set Start date app.End = (DateTime)properties.ListItem["EndDate"]; //set end date app.Body = "Set Meeting Body"; app.RequiredAttendees.Clear(); //clear all attendees. foreach (SPFieldUserValue itemAttendee in col) { if (!string.IsNullOrEmpty(itemAttendee.User.Email) && reg.IsMatch(itemAttendee.User.Email)) app.RequiredAttendees.Add(itemAttendee.User.Email); } app.Update(ConflictResolutionMode.AutoResolve); } } catch (Exception ex) { //Exception Handling } } /// <summary> /// /// </summary> public void SendMeetingRequestToNewAttendee(SPWeb web, string listName, int itemID, string organizerEmail, string title, MailAddressCollection macAttendeeList, string strSubject, string strSummary, int MeetingID, string MeetingURL, string meetingForwardBy, ExchangeServiceConnection conn, object appid) { try { string Title = string.Empty; string Subject = string.Empty; string stime = string.Empty; string ETime = string.Empty; string meetingAgenda = string.Empty; string MObjective = string.Empty; SPList spList = web.Lists.TryGetList("MeetingsListName"); SPListItem item = spList.GetItemById(Convert.ToInt32(MeetingID)); if (item != null) { Title = Convert.ToString(item["Title"]); Subject = Convert.ToString(item["Meeting_x0020_Subject"]); stime = Convert.ToString(item["EventDate"]); ETime = Convert.ToString(item["EndDate"]); meetingAgenda = Convert.ToString(item["Meeting_x0020_Agenda"]); MObjective = Convert.ToString(item["Meeting_x0020_Objective"]); } URL = MeetingURL; Appointment appointment = Appointment.Bind(conn.Service, new ItemId(appid.ToString())); List<EmailAddress> emails = new List<EmailAddress>(); foreach (MailAddress mails in macAttendeeList) { emails.Add(new EmailAddress(mails.Address)); } appointment.Subject = strSubject; String message = "Some Text"; MessageBody mb = new MessageBody(message); appointment.Forward(mb, emails); } catch (Exception ex) { //Exception Handling } } } }
0 comments:
Post a Comment