Hallo,
ich schreibe gerade für unsere Firma eine Workbench.
Hier sollen unter anderem auf dem Dashboard verschiedene Daten von Outlook aus dem Outlook-Kalender angezeigt werden. So soll er Daten aus (Termine, Feiertage in Deutschland, Urlaub und Geburtstage_MS) anzeigen.
Leider funktioniert das nicht, bzw. er findet die Daten nicht.
Hier mein Code zum Abgreifen der Outlook-Daten:
using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.Office.Interop.Outlook;
namespace LoginApp.Services
{
public class clsOutlookService
{
public DataTable GetAllCalendarItems()
{
Application oApp = null;
NameSpace mapiNamespace = null;
oApp = new Application();
mapiNamespace = oApp.GetNamespace("MAPI");
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Subject", typeof(string)));
dt.Columns.Add(new DataColumn("Start", typeof(DateTime)));
dt.Columns.Add(new DataColumn("End", typeof(DateTime)));
dt.Columns.Add(new DataColumn("Category", typeof(string)));
// Get default calendar
MAPIFolder calendarFolder = mapiNamespace.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
FetchCalendarItems(calendarFolder.Items, dt, "Termine");
// Get Feiertage in Deutschland
MAPIFolder holidaysFolder = GetFolderByName(mapiNamespace, "Feiertage in Deutschland");
if (holidaysFolder != null)
{
FetchCalendarItems(holidaysFolder.Items, dt, "Feiertage");
}
// Get Urlaub
MAPIFolder vacationFolder = GetFolderByName(mapiNamespace, "Urlaub");
if (vacationFolder != null)
{
FetchCalendarItems(vacationFolder.Items, dt, "Urlaub");
}
// Get Geburtstage_MS
MAPIFolder birthdaysFolder = GetFolderByName(mapiNamespace, "Geburtstage_MS");
if (birthdaysFolder != null)
{
FetchCalendarItems(birthdaysFolder.Items, dt, "Geburtstage");
}
// Debug-Ausgabe
foreach (DataRow row in dt.Rows)
{
Console.WriteLine($"{row["Category"]}: {row["Subject"]} von {row["Start"]} bis {row["End"]}");
}
return dt;
}
private MAPIFolder GetFolderByName(NameSpace mapiNamespace, string folderName)
{
foreach (MAPIFolder folder in mapiNamespace.Folders)
{
if (folder.Name == folderName)
{
return folder;
}
}
return null;
}
private void FetchCalendarItems(Items calendarItems, DataTable dt, string category)
{
calendarItems.IncludeRecurrences = true;
DateTime startOfWeek = DateTime.Now.AddDays(-(int)DateTime.Now.DayOfWeek);
DateTime endOfMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));
foreach (AppointmentItem item in calendarItems)
{
if (item.Start >= startOfWeek && item.End <= endOfMonth)
{
DataRow row = dt.NewRow();
row["Subject"] = item.Subject;
row["Start"] = item.Start;
row["End"] = item.End;
row["Category"] = category;
dt.Rows.Add(row);
}
}
}
}
}
Erkennt jemand den Fehler?