EWS Autodiscover not working, cycle through calendar items

ClaudeWalker
ClaudeWalker used Ask the Experts™
on
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.

Thanks,
JOe K.


//THIS WORKS

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);
                appointment.Save(SendInvitationsMode.SendToNone);

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Architect in .Net
Commented:
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.

Author

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...

Thanks,
JOe K.
Luis PérezSoftware Architect in .Net

Commented:
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.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

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);
                               
                    Console.WriteLine(folder.DisplayName);

Open in new window

Author

Commented:
Sorry about the tabbing in the embedded code.

Author

Commented:
Nevermind, this was the issue:  Console.WriteLine(folder.DisplayName);

I can get Console.WriteLine(folder.Id); to work instead meaning its connecting.

Author

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 .Net

Commented:
Does Console.WriteLine(folder.Id) works?

Author

Commented:
Yes.  I got it!

foreach (Item item in calendar.FindItems(new ItemView(100)))
{
     Console.WriteLine(item.Subject);
}


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.

Perfect!

Thank you soooo much!

JOe K.
Luis PérezSoftware Architect in .Net

Commented:
Glad to help you!!

Greetings and happy new year!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial