I am working on a angular web app which is a in house solution for developers to handle vacations (employees request vacations/sick days, admins can approve or deny request and so forth).
What I'm having trouble with is configuring a feature that handles sending calendar data to Outlook using vsto
In angular I set this up outlook-calendar.service.ts:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, BehaviorSubject } from 'rxjs';
import { ReturnHandler } from '../modules/vacation/Models/ReturnHandler';
import { OutlookCalendar } from '../modules/vacation/Models/outlookCalendar';
@Injectable({
providedIn: 'root'
})
export class OutlookCalendarService {
httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
private OutlookUrl = '/api/OutlookCalendar';
private outlookBH$: BehaviorSubject<number> = new BehaviorSubject<number>(0);
public outlook$: Observable<number> = this.outlookBH$.asObservable();
constructor(private http: HttpClient) { }
AddOutlookAppointment(outlookCalendar: OutlookCalendar) : Observable<ReturnHandler> {
const url = `${this.OutlookUrl}/CreateAppointment`;
return this.http.post<ReturnHandler>(url, outlookCalendar, this.httpOptions);
}
DeleteOutlookAppointment(outlookCalendar: OutlookCalendar) : Observable<ReturnHandler> {
const url = `${this.OutlookUrl}/DeleteAppointment`;
return this.http.post<ReturnHandler>(url, outlookCalendar, this.httpOptions);
}
OutlookYearCalendarGenerator(year: number) : Observable<ReturnHandler>{
const url = `http://localhost:50304/vac/api/OutlookCalendar/CreateNewOutlookCalendar?year=${year}`;
//const url = `${this.OutlookUrl}/CreateNewOutlookCalendar?year=${year}`;
console.log(year);
return this.http.get<ReturnHandler>(url, this.httpOptions);
}
// WriteOutlookNationalHolidays(year: string) : Observable<ReturnHandler>{
// const url = `${this.url}/WriteOutlookNationalHolidays?year=${year}`;
// return this.http.get<ReturnHandler>(url,this.httpOptions);
// }
}
In visual studio, ThisAddIn.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
using System.IO;
namespace OutlookCalendar
{
public partial class ThisAddIn
{
private OutlookCalendarCs outlookCalendar;
protected override object RequestComAddInAutomationService()
{
if(outlookCalendar == null)
outlookCalendar = new OutlookCalendarCs();
return outlookCalendar;
}
private void ThisAddIn_Startup(object sender, EventArgs e)
{
Outlook.NameSpace session = Globals.ThisAddIn.Application.Session;
Outlook.Accounts accounts = session.Accounts;
List<Outlook.MAPIFolder> calendars = new List<Outlook.MAPIFolder>();
foreach (Outlook.Account account in accounts)
{
Outlook.Recipient recipient = session.CreateRecipient(account.DisplayName);
Outlook.MAPIFolder calendar = session.GetSharedDefaultFolder(recipient, Outlook.OlDefaultFolders.olFolderCalendar);
if (calendar != null)
{
calendars.Add(calendar);
}
}
File.WriteAllLines("dd.txt", (IEnumerable<string>)calendars);
Console.WriteLine(calendars);
using (TextWriter tw = new StreamWriter("C:\\AxessCroTools\\IIS\\dd.txt"))
{
foreach (Outlook.MAPIFolder calendar in calendars)
{
tw.WriteLine(calendar);
}
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
// Note: Outlook no longer raises this event. If you have code that
// must run when Outlook shuts down, see https://go.microsoft.com/fwlink/?LinkId=506785
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup = new System.EventHandler(ThisAddIn_Startup);
this.Shutdown = new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
Now when pressing my generate outlook calendar button I encounter this error:
Http failure response for http://localhost:50304/vac/api/OutlookCalendar/CreateNewOutlookCalendar?year=undefined: 0 Unknown Error
CodePudding user response:
In the code of your add-in I've noticed the following code:
using (TextWriter tw = new StreamWriter("C:\\AxessCroTools\\IIS\\dd.txt"))
{
foreach (Outlook.MAPIFolder calendar in calendars)
{
tw.WriteLine(calendar);
}
}
So, you are trying to write data to the text stream, followed by a line terminator. But a COM object which represents a MAPIFolder
from the Outlook object model is passed. What output do you expect in that case? You may be interested in the Folder path, then check out the MAPIFolder.FolderPath property.
If you need to export the calendar take a look at the MAPIFolder.GetCalendarExporter method which creates a CalendarSharing
object for the specified Folder
. Then you can use the SaveAsICal(String) method to save calendar information in an iCalendar (.ics) file for sharing a calendar as a URL.