SharePoint 2010 Lists Web Service: Using WebClient.UploadString to POST

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… ;/




static void Main(string[] args)

            string soapEnv = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                "<soap12:Envelope xmlns:xsi=\"\"" +
                " xmlns:xsd=\"\"" +
                " xmlns:soap12=\"\">" +
                "<soap12:Body>" +
                "<UpdateListItems xmlns=\"\">" +
                "<listName>TestList</listName>" +
                "<updates>" +
                "<Batch>" +
                  "<Method ID=\"1\" Cmd=\"New\">" +
                    "<Field Name=\"Title\">Added item</Field>" +
                  "</Method>" +
                "</Batch>" +
                "</updates>" +
                "</UpdateListItems>" + 
                "</soap12:Body>" + 

            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"] = ""; 
            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);

                string response = client.UploadString(new Uri(uri, UriKind.Absolute), "POST", soapEnv); 
            catch (Exception ex) 


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

About davros85

SharePoint / Azure Architect & Developer - Creator of ProvisionPoint, a site provisioning and change management solution for SharePoint Online