EWS Autodiscover not working, cycle through calendar items

I'm trying to create a program that cycles through all user calendars and records their calendar items.  However, I can't seem to do anything other than access my calendar.  I'm the Systems Administrator and have access to all admin accounts and admin network credentials.

How can I get a user's (other than mine) calendar items?  I get autodiscover errors so I am putting in the service URL directly.

JOe K.


ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);

service.UseDefaultCredentials = true;
service.Url = new Uri ("https://mydomain/EWS/Exchange.asmx");
                Appointment appointment = new Appointment(service);
                appointment.Subject = "Dentist Appointment";
                appointment.Body = "The appointment is with Dr. Smith.";
                appointment.Start = new DateTime(2012, 1, 3, 9, 0, 0);
                appointment.End = appointment.Start.AddHours(2);

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Luis PérezSoftware Architect in .NetCommented:
To access another user's calendar items (or emails, or anything else), you must "say" to EWS that you want to impersonate that user.

service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "myuser@mydomain.com")

You can use the mail e-mail address of the user to impersonate, as well as other data like the Principal name or the SID.

In the other hand, try this to get working your Autodiscover service:

using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

public static bool OnValidationCallback(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
      return true;

And, before calling Autodiscover:
ServicePointManager.ServerCertificateValidationCallback == new RemoteCertificateValidationCallback(OnValidationCallback);

This function is to bypass the certificate validation that EWS requires.

Hope that helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ClaudeWalkerAuthor Commented:
That definitely helps.  Now I need to Enable Impersonation on the account:

Per the documentation to enable it for all users:

New-ManagementRoleAssignment –Name:impersonationAssignmentName –Role:ApplicationImpersonation –User:serviceAccount

However, I don't know what the parameters are supposed to me :

impersonationAssignmentName (is this just an arbitrary named thing, could I keep it the same
ApplicationImpersonation (Is this meant to be as is)
serviceAccount (is this an email account, sharedTest@mydomain.com)

I'm close...

JOe K.
Luis PérezSoftware Architect in .NetCommented:
You must left all the data as the way you wrote it except for "serviceAccount" that must be your account (your user name into the domain, the one which you log in with).

Hope that helps.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

ClaudeWalkerAuthor Commented:
I'm still getting the same error even after adding that to EMS.

Here is my code:

Here is what I used to enable impersonation:  New-ManagementRoleAssignment –Name:impersonationAssignmentName –Role:ApplicationImpersonation –User:userLogin

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
                    service.Url = new Uri("https://myDomain.com/EWS/Exchange.asmx");
                    service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "userLogin@myDomain.com");
                    Folder folder = Folder.Bind(service, WellKnownFolderName.Calendar);

Open in new window

ClaudeWalkerAuthor Commented:
Sorry about the tabbing in the embedded code.
ClaudeWalkerAuthor Commented:
Nevermind, this was the issue:  Console.WriteLine(folder.DisplayName);

I can get Console.WriteLine(folder.Id); to work instead meaning its connecting.
ClaudeWalkerAuthor Commented:
Now I have access to the calendar folder for all users

how can I grab the calendar items using folder.???
Luis PérezSoftware Architect in .NetCommented:
Does Console.WriteLine(folder.Id) works?
ClaudeWalkerAuthor Commented:
Yes.  I got it!

foreach (Item item in calendar.FindItems(new ItemView(100)))

I was able to cycle through all of the items in a calendar.  Now I can put some date conditions in and loop through a number of email addresses.


Thank you soooo much!

JOe K.
Luis PérezSoftware Architect in .NetCommented:
Glad to help you!!

Greetings and happy new year!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.