So I recently had to write a console application to post data to a SharePoint 2010 list, using the web services – but without using any ‘normal’ .NET web references. The server was set up with Basic Authentication.
This involved forming the SOAP envelope manually, and using a POST command. One thing that tripped me up (…and very nearly made me cry) was that most samples on the net use <soap:Envelope> – whereas it only worked for me when I used <soap12:Envelope> (with the correct namespace of course).
Anyway the code is below – so hopefully it might help some of you not waste so much time as I did… ;/
.davros.
static void Main(string[] args) { string soapEnv = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" + " xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" + "<soap12:Body>" + "<UpdateListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">" + "<listName>TestList</listName>" + "<updates>" + "<Batch>" + "<Method ID=\"1\" Cmd=\"New\">" + "<Field Name=\"Title\">Added item</Field>" + "</Method>" + "</Batch>" + "</updates>" + "</UpdateListItems>" + "</soap12:Body>" + "</soap12:Envelope>"; string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username@domain + ":" + "password")); string uri = "http://sharepoint-url-here/_vti_bin/lists.asmx"; WebClient client = new WebClient(); client.Headers["SOAPAction"] = "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems"; client.Headers["Content-Type"] = "application/soap+xml; charset=utf-8"; client.Headers["Authorization"] = "Basic " + user; client.Encoding = Encoding.UTF8; System.Text.ASCIIEncoding encode = new ASCIIEncoding(); Byte[] bytes = encode.GetBytes(soapEnv); try { string response = client.UploadString(new Uri(uri, UriKind.Absolute), "POST", soapEnv); } catch (Exception ex) { Console.WriteLine("Doh!"); } }