Programmatic access to Exchange 2010 using EWS, SOAP, and Python

I’ve previously blogged about accessing Exchange (2007) using suds and Python. Turns out that things have changed slightly in Exchange 2010, so here’s an update.

First, you’ll need to use Alex Koshelev’s EWS-specific fork of suds, which you can grab from BitBucket. Next, you’ll need code a little like this:

import urllib2

from suds.client import Client
from suds.sax.element import Element
from suds.transport.http import HttpTransport

class Transport(HttpTransport):
    def __init__(self, **kwargs):
        realm, uri = kwargs.pop('realm'), kwargs.pop('uri')
        HttpTransport.__init__(self, **kwargs)
        self.handler = urllib2.HTTPBasicAuthHandler()
        self.handler.add_password(realm=realm,
                                  user=self.options.username,
                                  passwd=self.options.password,
                                  uri=uri)
        self.urlopener = urllib2.build_opener(self.handler)

transport = Transport(realm='nexus.ox.ac.uk',
                      uri='https://nexus.ox.ac.uk/',
                      username='abcd0123',
                      password='secret')
client = Client("https://nexus.ox.ac.uk/EWS/Services.wsdl",
                transport=transport)

ns = ('t', 'http://schemas.microsoft.com/exchange/services/2006/types')
soap_headers = Element('RequestServerVersion', ns=ns)
soap_headers.attributes.append('Version="Exchange2010_SP1"')
client.set_options(soapheaders=soap_headers)

address = client.factory.create('t:EmailAddress')
address.Address = 'first.last@unit.ox.ac.uk'

client.service.GetUserOofSettings(address)

Differences from the previous post are:

  • Passing SOAP headers seems to be necessary in some circumstances. This post on StackOverflow came in handy in working out what to do. The MSDN documentation (e.g. this page about GetRoomLists) tells you which SOAP headers you can send.
  • Namespaces seem to be working better. We’ve created a t:EmailAddress element this time, not a ns1:EmailAddress.
  • The patch mentioned in my previous blog post has been applied, so there’s now no need to apply it yourself.
Posted in Exchange Web Services, SharePoint and Exchange | Leave a comment

Leave a Reply