Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Retrieve a list of roomlists from Exchange 2010 via SOAP.
// Retrieve a list of roomlists from Exchange 2010 via SOAP.
// This method then uses SMXMLDocument to retrieve the e-mail addresses for those roomlists,
// which can be used to query the individual rooms
// DEPENDENCIES: AFNetworking v1 (
// SMXMLDocument (
// NOTE: I used AFHTTPRequestOperation instead of AFXMLRequestOperation because I wanted the XML, not an XML parser
// (which is what AFXMLRequestOperation returns)
- (void) readRoomLists {
NSString *soapMessage = [NSString stringWithFormat:@""
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
@"<soap:Envelope xmlns:xsd=\"\" xmlns:soap=\"\" xmlns:t=\"\" xmlns:m=\"\">"
@"<t:RequestServerVersion Version =\"Exchange2010_SP2\"/>"
@"<m:GetRoomLists />"
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:[self makeExchangeRequestWithMessage:soapMessage]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError *error_ = nil;
SMXMLDocument *xml = [SMXMLDocument documentWithData:responseObject error:&error_];
if (!error_) {
SMXMLElement *Body = [xml.root childNamed:@"Body"];
SMXMLElement *GetRoomListsResponse = [Body childNamed:@"GetRoomListsResponse"];
SMXMLElement *RoomLists = [GetRoomListsResponse childNamed:@"RoomLists"];
for (SMXMLElement *room in [RoomLists childrenNamed:@"Address"]) {
SMXMLElement *element = [room childNamed:@"EmailAddress"];
NSString *email = [element value];
// do something with the roomlist e-mail address here
NSLog(@"finished reading roomLists");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", operation.responseString);
[operation setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
[self handleAuthChallenge:connection :challenge];
[operation start];
- (NSMutableURLRequest *)makeExchangeRequestWithMessage:(NSString *)soapMessage {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://OutlookWebAccessServer.domain.tld/ews/exchange.asmx"]];
[request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
[request setValue:@"" forHTTPHeaderField:@"SOAPAction"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
return request;
-(void) handleAuthChallenge:(NSURLConnection *) connection :(NSURLAuthenticationChallenge *) challenge {
if( [[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodNTLM ) {
NSLog(@"trying NTLM...");
if( [challenge previousFailureCount] > 0 ) {
// Avoid too many failed authentication attempts which could lock out the user
NSLog(@"NTLM backing out, too many failures");
[[challenge sender] cancelAuthenticationChallenge:challenge];
} else {
NSLog(@"NTLM success!");
[[challenge sender] useCredential:[NSURLCredential credentialWithUser:self.soapUsername password:self.soapPassword persistence:NSURLCredentialPersistenceForSession] forAuthenticationChallenge:challenge];
} else {
// Authenticate in other ways than NTLM if desired or cancel the auth like this:
[[challenge sender] cancelAuthenticationChallenge:challenge];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment