lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



I am attempting to write my first lua script with the aim of retrieving some data from a SharePoint list using the GetListItems operation from the build-in Lists.asmx web service.

Googleing led me to LuaSocket and I think, that the script below succedes in sending my request to the SharePoint web service but the response seems to indicate that I need NTLM authorization in order to get the data that I want.

Is it possible to use NTLM authorization with LuaSocket or are there other ways to to set up authentication. I have tried to add "user:pass@" in the ULR, but this did not change the response from the SharePoint site.

Is LuaSocket the correct choice for communicating with a SharePoint web service or should I be looking at something else entirely?

Here is my lua script:

-- Get data from SharePoint site
local http = require("socket.http")
local ltn12 = require("ltn12")
local io = require("io")

local body = [[
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"; xmlns:soap1="http://schemas.microsoft.com/sharepoint/soap/";>
   <soap:Header/>
   <soap:Body>
      <soap1:GetListItems>
         <soap1:listName>MyListName</soap1:listName>
         <soap1:viewName>MyViewName</soap1:viewName>
         <soap1:query>
<Query><Where><Eq><FieldRef Name='Field'/><Value Type='Number'> 1234 </Value></Eq></Where></Query>
         </soap1:query>
         <soap1:viewFields>
<ViewFields><FieldRef Name='ID' /><FieldRef Name='Field' /></ViewFields>
         </soap1:viewFields>
      </soap1:GetListItems>
   </soap:Body>
</soap:Envelope>
]]


r, c, h = http.request {
	url = "http://my/sharepoint/site/_vti_bin/lists.asmx";,
	method = "POST",
	sink = ltn12.sink.file(io.stdout),
	source = ltn12.source.string(body),
	headers = {
		["Content-Length"] = tostring(body:len()),
		["Content-Type"] = "application/soap+xml;charset=UTF-8",
["SOAPAction"] = "\"http://schemas.microsoft.com/sharepoint/soap/GetListItems\"";,
		["Connection"] = "Keep-Alive",
	}
}


-- Copied from http://lua-users.org/wiki/TableSerialization
-- Print anything - including nested tables
function table_print (tt, indent, done)
  done = done or {}
  indent = indent or 0
  if type(tt) == "table" then
    for key, value in pairs (tt) do
      io.write(string.rep (" ", indent)) -- indent it
      if type (value) == "table" and not done [value] then
        done [value] = true
        io.write(string.format("[%s] => table\n", tostring (key)));
        io.write(string.rep (" ", indent+4)) -- indent it
        io.write("(\n");
        table_print (value, indent + 7, done)
        io.write(string.rep (" ", indent+4)) -- indent it
        io.write(")\n");
      else
        io.write(string.format("[%s] => %s\n",
            tostring (key), tostring(value)))
      end
    end
  else
    io.write(tt .. "\n")
  end
end

print(r)
print(c)
table_print(h,2,false)

-- End of script

Running my script yields:

1
401
  [microsoftsharepointteamservices] => 12.0.0.6421
  [www-authenticate] => NTLM
  [date] => Mon, 17 Sep 2012 12:05:16 GMT
  [x-powered-by] => ASP.NET
  [content-length] => 0
  [server] => Microsoft-IIS/7.0


I can successfully send the same request to the SharePoint site using soapUI and in return I get the following headers:

MicrosoftSharePointTeamServices	12.0.0.6421
Content-Length	697
#status#	HTTP/1.1 200 OK
Set-Cookie	WSS_KeepSessionAuthenticated=80; path=/
Server	Microsoft-IIS/7.0
X-Powered-By	ASP.NET
Cache-Control	private, max-age=0
X-AspNet-Version	2.0.50727
Vary	Accept-Encoding
Date	Mon, 17 Sep 2012 11:52:24 GMT
Content-Encoding	gzip
Content-Type	application/soap+xml; charset=utf-8

and a soap:Envelope containing the response data.