Reputation: 165
Some code, originally written by someone else, has suddenly stopped doing what it's supposed to.
The code is supposed to find a shared calendar from a different account and get the events within, but it no longer finds this shared calendar, or any shared calendars.
I don't have access to the account sharing the calendar, but I tried sharing a calendar from a different account and it would not show up either, however when creating a calendar in the account used by the script it appears fine. The shared calendars are visible when logged into the account though. And events can be created and seen.
I have limited knowledge of exchange, but I need this script to work.
I've looked around for already answered questions with similar issues and tried some solutions, but none have worked so far or been prevented by other problems and/or lack of knowledge.
Nothing has changed on this end that I'm aware, and the owner of the account claims that I have full access to the calendar and that nothing has changed on their end. So I have no idea why this would suddenly stop working.
For clarification, there are no errors. The try-catch doesn't trigger, and the script prints a list of folders to the page. The issue is that this list doesn't contain the shared calendars visible on the site.
const string EWSUrl = @"https://outlook.office365.com/ews/exchange.asmx";
const string EmailAdre = "myEmail";
const string EmailPass = "myPassword";
const string EmailDomi = "domi";
const string EmailCale = "targetAccount";
const int calFolderLimit = 200; // Limit on folder search
const int calMaxItemsReturned = 500; // Max limit cal fetch
const int calMonthLimit = 12; // How many months to fetch
const int calMonthBack = 1; // Update how many days back
// Tries to find one of this
const string calFolderName = "Ekonomi Årskalender";
const string calFolderID = "AQMkAGU5NTdjYTE5LTZiNWMtNDYzMi05MWQ4LWQ5MDcyMgA1YjMwMTYALgAAA5ukgsUdw+BFjn7iHZ02H3gBAM/bOclCwmFEsqKa2vRsw2EAAAIBWQAAAA==";
const string EWSUrl = @"https://outlook.office365.com/ews/exchange.asmx";
const string EmailAdre = "myEmail";
const string EmailPass = "myPassword";
const string EmailDomi = "domi";
const string EmailCale = "targetAccount";
const int calFolderLimit = 200; // Limit on folder search
const int calMaxItemsReturned = 500; // Max limit cal fetch
const int calMonthLimit = 12; // How many months to fetch
const int calMonthBack = 1; // Update how many days back
// Tries to find one of this
const string calFolderName = "Ekonomi Årskalender";
const string calFolderID = "AQMkAGU5NTdjYTE5LTZiNWMtNDYzMi05MWQ4LWQ5MDcyMgA1YjMwMTYALgAAA5ukgsUdw+BFjn7iHZ02H3gBAM/bOclCwmFEsqKa2vRsw2EAAAIBWQAAAA==";
protected string syncCalendar(HttpContext context)
{
string response = "";
try
{
// CONNECT TO EXCHANGE
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.Credentials = new WebCredentials(EmailAdre, EmailPass, EmailDomi);
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
service.EnableScpLookup = true;
service.AcceptGzipEncoding = true;
service.Url = new Uri(EWSUrl);
// Find Folder
FolderView folderView = new FolderView(calFolderLimit);
folderView.Traversal = FolderTraversal.Deep;
folderView.PropertySet = new PropertySet(FolderSchema.FolderClass, FolderSchema.DisplayName, FolderSchema.TotalCount, FolderSchema.ParentFolderId);
FindFoldersResults folders = service.FindFolders(new FolderId(WellKnownFolderName.Root), folderView);
foreach (var myFolder in folders.Folders)
{
if (myFolder is CalendarFolder && (myFolder.DisplayName == calFolderName || myFolder.Id.ToString() == calFolderID))
{
//Doing stuff with matched calendar folder
//Print name for debugging
response += "<div>" + myFolder.DisplayName + "</div>";
}
}
}
catch (Exception ex)
{
response += "[ERROR]<p>" + ex.Message + "<p/><p>" + ex.StackTrace + "</p>";
}
return response;
}
Upvotes: 0
Views: 88
Reputation: 22032
This code you posted doesn't make a lot of sense without talking to the original author its hard to determine why its been done that way. Let start by looking at
const string calFolderID = "AQMkAGU5NTdjYTE5LTZiNWMtNDYzMi05MWQ4LWQ5MDcyMgA1YjMwMTYALgAAA5ukgsUdw+BFjn7iHZ02H3gBAM/bOclCwmFEsqKa2vRsw2EAAAIBWQAAAA==";
Looks like the ews FolderId of the folder your trying to find hard coded ? Not such a great idea eg if the Mailbox was migrated from OnPrem to Office365 the FolderId would be different which would break the code (that would be my first guess as to why it doesn't work if you said it did work in the first place).
However if you already have the EWS FolderId of the folder then the rest of the code is redudant and you could have done the same thing by just binding to the folderId eg
FolderId fldId = new FolderId("AQMkAGU5NTdjYTE5LTZiNWMtNDYzMi05MWQ4LWQ5MDcyMgA1YjMwMTYALgAAA5ukgsUdw+BFjn7iHZ02H3gBAM/bOclCwmFEsqKa2vRsw2EAAAIBWQAAAA==");
Folder myFolder = Folder.Bind(service, fldId);
But most likely as the FolderId you want to access has probably changed the only way to fix it quickly would be open the Target Mailbox where the calendar you want to bind to is and grab the new folderId. You can use the EWSEditor https://ewseditor.codeplex.com/ to obtain that without needing to write any code. Once you have the quick fix working I would suggest you rewrite the whole thing to stop it breaking in the future.
Upvotes: 1