2

I'm attempting to write a script to communicate with sharepoint via SOAP using urllib2 in Python. My code is connecting successfully to a sharepoint list, but does not do anything once connected. Could my SOAP request be wrong? It seems to be returning nothing, despite 2 list items existing on the sharepoint site.

import urllib2
from ntlm import HTTPNtlmAuthHandler

user = r'DOMAIN\myusername'
password = "password"
url = "https://mysecuresite.com/site/_vti_bin/Lists.asmx"

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, user, password)
# create the NTLM authentication handler
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)

# create and install the opener
opener = urllib2.build_opener(auth_NTLM)
urllib2.install_opener(opener)

request="""<?xml version="1.0" encoding="utf-8"?>
    <SOAP:ENVELOPE xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SOAP:BODY>
        <?XML:NAMESPACE PREFIX = "[default] http://schemas.microsoft.com/sharepoint/soap/" NS = "http://schemas.microsoft.com/sharepoint/soap/" /><GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
            <listName>site</listName>
            <query></query>
            <rowLimit></rowLimit>
        </GetListItems>
    </SOAP:BODY>
</SOAP:ENVELOPE>
"""

headers={"SOAPAction":"\"http://schemas.microsoft.com/sharepoint/soap/GetListItems\"","Content-Type":"text/xml;charset=UTF-8"}

req = urllib2.Request(url, request, headers)

response=urllib2.urlopen(req)
data = response.read()
print data

I've tried using libraries like haufe.sharepoint, but they don't play nice with HTTPS and ntlm.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Ryan W
  • 98
  • 2
  • 12
  • Have you tried manually putting the same request in using e.g. POSTER in Firefox? – DisappointedByUnaccountableMod Dec 03 '15 at 17:07
  • Pfuh, good luck. The header value for SoapAction looks odd with literal " around the value (but not because I know it's wrong) – DisappointedByUnaccountableMod Dec 03 '15 at 17:22
  • All Chrome plugins are blocked on my work computer, and Firefox isn't an option. Any other ways to check this? – Ryan W Dec 03 '15 at 17:23
  • 1
    There are some other examples of soap requests if you goggle for _sharepoint soap getlistitems_. https://www.furunousa.com/_vti_bin/Lists.asmx?op=GetListItems http://stackoverflow.com/questions/4287384/using-sharepoint-soap-with-jquery-getlistitems-simple-but-cant-work-it-out http://stackoverflow.com/questions/5015625/sharepoint-soap-getlistitems-ignoring-query – DisappointedByUnaccountableMod Dec 03 '15 at 17:26
  • That third link did the trick! The problem likely either lied in my formatting, or in the fact that I was referring to the list by its name instead of it's GUID. – Ryan W Dec 03 '15 at 17:41

1 Answers1

0

See the code below if it gives you some clues. I am simply calling a SOAP service using only urllib2 with Python 2.6.6. This worked fine for me. Be careful because I've added some code to pass through a proxy that you may or not need. In my particular case I am invoking a SOAP service in an Oracle Data Integrator (ODI)

import urllib2

url = "http://alexmoleiro.com:20910/oraclediagent/OdiInvoke?wsdl"

post_data = """<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <OdiStartScenRequest xmlns="xmlns.oracle.com/odi/OdiInvoke/">
            <Credentials xmlns="">
                 <OdiUser>loquesea</OdiUser>
                <OdiPassword>aversiacierto</OdiPassword>
               <WorkRepository>repofacil</WorkRepository>
         </Credentials>            
        </OdiStartScenRequest>
    </Body>
</Envelope>
"""

http_headers = {
    "Accept": "application/soap+xml,multipart/related,text/*",
    "Cache-Control": "no-cache",
    "Pragma": "no-cache",
    "Content-Type": "text/xml; charset=utf-8"

}

request_object = urllib2.Request(url, post_data, http_headers)

#IF YOU ARE NOT BEHIND A PROXY, DELETE THIS BLOCK
http_proxy_server = "10.115.4.2"
http_proxy_port = "8080"
http_proxy_realm = http_proxy_server
http_proxy_full_auth_string = "http://%s:%s" % (http_proxy_server, http_proxy_port)
proxy = urllib2.ProxyHandler({'http': http_proxy_full_auth_string})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
#END OF --> IF YOU ARE NOT BEHIND A PROXY, DELETE THIS BLOCK


response = urllib2.urlopen(request_object)
html_string = response.read()
print html_string

Hope it helps!

Alex Moleiro
  • 1,166
  • 11
  • 13