C# Outlook get public Appointments of other users by using EWS - c#

I'm currently trying to retrieve public Appointments of a users Calendar in Outlook (with Exchange2013) to a DataSet to be displayed later on.
Here's the relevant code so far:
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
service.AutodiscoverUrl("mail#mycompany.com", RedirectionCallback);
DateTime reportDate = dateTimePicker1.Value;
DateTime startDate = new DateTime(reportDate.Year, reportDate.Month, reportDate.Day, 0, 0, 1);
DateTime endDate = startDate.AddHours(23);
endDate = endDate.AddMinutes(59);
CalendarFolder calendar = CalendarFolder.Bind(service, new FolderId(WellKnownFolderName.Calendar, "somemail#mycompany.com"));
CalendarView cView = new CalendarView(startDate, endDate);
cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End, AppointmentSchema.Location);
// get appointment collection
FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);
// transfer to DataSet
DataSet1.Tables[0].Rows.Clear();
int i = 0;
foreach (Appointment a in appointments) {
DataSet1.Tables[0].Rows.Add(
i++.ToString(),
a.Subject.ToString(),
a.Start.ToString(),
a.End.ToString(),
a.Location.ToString());
}
An exception is being shown in the Line
CalendarFolder calendar = CalendarFolder.Bind(service, new FolderId(WellKnownFolderName.Calendar, "somemail#mycompany.com"));
Any ideas what I'm doing wrong here? Or is there even a completely other way on how to approach my problem?

The account which you are using to connect tho the exchange server doesn't have permission to read the mailbox of somemail#mycompany.com. Try to get a super user privilege for the account. Hope this is useful.

Related

How to get group detail from Required Attendees using EWS

I'm trying to fetch detail of the email address from Attendees because of more addresses in the group, this is my code.
public List<Meeting> getAll(string email, string sDate, string eDate)
{
List<Meeting> res = new List<Meeting>();
ExchangeService es = new ExchangeService();
string username = Properties.Settings.Default.username;
string password = Properties.Settings.Default.password;
SecureString ssPassword = new SecureString();
foreach (char x in password)
ssPassword.AppendChar(x);
es.Credentials = new NetworkCredential(username, ssPassword);
es.Url = new Uri("https://outlook.office365.com/ews/exchange.asmx");
FolderId folderID = new FolderId(WellKnownFolderName.Calendar, "xxxxxx#xxxx.com");
DateTime startDate = DateTime.ParseExact(sDate + " 00:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
DateTime endDate = DateTime.ParseExact(eDate + " 23:59:59", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
CalendarView cView = new CalendarView(startDate, endDate);
//cView.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties);
FindItemsResults<Item> resultItem = es.FindItems(folderID, cView);
foreach (Item item in resultItem.Items)
{
ServiceResponseCollection<GetItemResponse> itemResponseCollection = es.BindToItems(new[] { new ItemId(item.Id.UniqueId) }, new PropertySet(BasePropertySet.FirstClassProperties));
foreach (GetItemResponse itemResponse in itemResponseCollection)
{
Appointment app = (Appointment)itemResponse.Item;
res.Add(GetClassFromAppointment(app));
}
}
return res;
}
obj.Attendees = {aaa#xxxx.com, bbb#xxxx.com, Group#xxxx.com}
"Group#xxxx.com" include more emaill address: ccc#xxxx.com, ddd#xxxx.com
How to fetch detail's addresses from the group?
You should be about just to use the EWS ExpandGroup operation https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-expand-distribution-groups-by-using-ews-in-exchange-2013. eg with your code something like
// Return the expanded group.
ExpandGroupResults myGroupMembers = es.ExpandGroup("Group#xxxx.com");
// Display the group members.
foreach (EmailAddress address in myGroupMembers.Members)
{
Console.WriteLine("Email Address: {0}", address);
}
Another approach is because you using Office365 is that you can get the Group members using the Microsoft Graph API https://learn.microsoft.com/en-us/graph/api/group-list-members?view=graph-rest-1.0&tabs=http

Deleting SharePoint calendar links In Exchange via EWS

Does anyone have examples of how to delete a user's SharePoint calendar link in Outlook/Exchange via EWS Managed API? I am able to successfully find the Calendar link item, however the item.Delete(DeleteMode.HardDelete) executes without actually removing the calendar link. Any help or guidance would be appreciated.
Edited to include code:
//Will target a specific user mailbox with parameter mbMailboxname
static Dictionary<string, Folder> GetSharedCalendarFolders(ExchangeService service, String mbMailboxname)
{
Dictionary<String, Folder> rtList = new System.Collections.Generic.Dictionary<string, Folder>();
DateTime startDate = new DateTime(2018, 8, 1);
DateTime endDate = new DateTime(2018, 8, 31);
CalendarView calView = new CalendarView(startDate, endDate);
Mailbox mb = new Mailbox(mbMailboxname);
FolderId rfRootFolderid = new FolderId(WellKnownFolderName.Root, mb);
FolderView fvFolderView = new FolderView(1000);
SearchFilter sfSearchFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Common Views");
FindFoldersResults ffoldres = service.FindFolders(rfRootFolderid, sfSearchFilter, fvFolderView);
if (ffoldres.Folders.Count == 1)
{
PropertySet psPropset = new PropertySet(BasePropertySet.FirstClassProperties);
ExtendedPropertyDefinition PidTagWlinkAddressBookEID = new ExtendedPropertyDefinition(0x6854, MapiPropertyType.Binary);
ExtendedPropertyDefinition PidTagWlinkGroupName = new ExtendedPropertyDefinition(0x6851, MapiPropertyType.String);
psPropset.Add(PidTagWlinkAddressBookEID);
ItemView iv = new ItemView(1000);
iv.PropertySet = psPropset;
iv.Traversal = ItemTraversal.Associated;
SearchFilter cntSearch = new SearchFilter.IsEqualTo(PidTagWlinkGroupName, "Other Calendars");
FindItemsResults<Item> fiResults = ffoldres.Folders[0].FindItems(cntSearch, iv);
foreach (Item itItem in fiResults.Items)
{
Microsoft.Exchange.WebServices.Data.Item foundItem = itItem;
if (foundItem.Subject.ToString().Trim().Contains("Company Group Calendar"))
{
Console.WriteLine("Deleting calendar..");
//Executing the delete command here does not delete the calendar
foundItem.Delete(DeleteMode.MoveToDeletedItems);
}
}
}
return rtList;
}

Response Exchange EWS

How can I get a response after I make a new appointment? An OK response or Not OK and the reason for it..?
After the authentication this is my code for creating the appointment
DateTime bookingStart = new DateTime(2017, 6, 14, 14, 0, 0);
DateTime bookingEnd = bookingStart.AddHours(2);
Booking booking = new Booking()
{
Subject = "BookingTest",
Start = bookingStart,
End = bookingEnd,
Room = "xxx.room#dummy.be",
Person = "person#dummy.be"
};
//Meetingrequest aanmaken
var request = new Appointment(service)
{
Subject = booking.Subject,
Start = booking.Start,
End = booking.End,
Location = booking.Room
};
//Aanhangen wie wilt meedoen
request.RequiredAttendees.Add(booking.Person);
request.RequiredAttendees.Add(booking.Room);
ServiceResponse response = service.CreateItems()
request.Save(SendInvitationsMode.SendOnlyToAll);
But this doesn't give me an object back.
After you save this appointment, the meeting item will get a unique identifier.
request.Save(SendInvitationsMode.SendOnlyToAll);
var id = request.Id.UniqueId;
Save the identifier, later on, you can use this identifier to track the meeting response like this
Appointment meeting = Appointment.Bind(service, new ItemId(id));
for (int i = 0; i < meeting.RequiredAttendees.Count; i++)
{
Console.WriteLine("Required attendee - " + meeting.RequiredAttendees[i].Address + ": " + meeting.RequiredAttendees[i].ResponseType.Value.ToString());
}
Reference: Tracking meeting responses by using the EWS Managed API 2.0

How to get all appointments including the private ones?

I want to list all Outlook appointment times in a specific time range and I am only interested in start and end time of the appointments.
I'm using this code so far:
DateTime startTime = DateTime.Now.AddDays(2);
DateTime endTime = DateTime.Now.AddDays(3);
var outlookApplication = new Outlook.Application();
Outlook.NameSpace outlookNamespace = outlookApplication.GetNamespace("MAPI");
var recip = outlookNamespace.CreateRecipient("<valid contact>");
if (recip.Resolve())
{
var calendarItems = outlookNamespace.GetSharedDefaultFolder(recip, OlDefaultFolders.olFolderCalendar).Items;
calendarItems.IncludeRecurrences = true;
var filter = String.Format("[Start] >= '{0}' And [End] < '{1}'", startTime.ToShortDateString(), endTime.ToShortDateString());
calendarItems.Sort("[Start]");
calendarItems = calendarItems.Restrict(filter);
var result = calendarItems.Cast<AppointmentItem>().Select(x => x);
}
The code retrieves nearly all appointments, but not the private ones. How can I get the private appointments too?
Outlook always filters out private appointments form the shared folders even if they are perfectly accessible using MAPI. If using Redemption (I am its author) is an option, you can use RDOFolder.GetActivitiesForTimeRange - it returns private appointments.

Output Dynamic List in C# ASP.net

I have a program which fetches appointments from Microsoft Exchange for a given calendar and I want to output these in an ASP.net file
The "code behind" I have is:
DateTime StartDate = DateTime.Today;
DateTime EndDate = DateTime.Today.AddDays(1);
CalendarView cv = new CalendarView(StartDate, EndDate);
String MailboxToAccess = "RES_03037#company.com";
FolderId CalendarFolderId = new FolderId(WellKnownFolderName.Calendar, MailboxToAccess);
FindItemsResults<Appointment> fapts = service.FindAppointments(CalendarFolderId, cv);
if (fapts.Items.Count > 0)
{
DataRow row = table2.NewRow();
foreach (Appointment Appoint in fapts)
{
//Output to ASPX file
}
}
I am fairly new to ASP and C#
Your advice will be much appreciated!

Categories