SharePoint: Adding list items remotely

To add an item remotely to a SharePoint list, you need to use the _vti_bin/list.asmx web service. Note that this must be called at the url of the site where the list resides, e.g. http://myserver/sites/mainsite/subsite/_vti_bin/lists.asmx

Node that your project needs to have a web reference to the list.asmx webservice. I normally set the web reference url property to http://localhost/_vti_bin/lists.asmx and set the url behaviour property to dynamic. In the code example below, i have given this web reference the id of UpdateListItemsWS. Note that the code below assumes that System.Xml and Microsoft.SharePoint is referenced.

The code below outlines how to add items remotely to a list.

webServiceUrl = "http://localhost/sites/testsite";
userName = "testuser";
password = "testpassword";
domain = "testdomain";
title = "Hello World";
testField = "Testing 123";

// Prepare connection to webservice
UpdateListItemsWS.Lists updateListItemsWS = new UpdateListItemsWS.Lists();
updateListItemsWS.Url = string.Format("{0}/_vti_bin/lists.asmx", webServiceUrl);
updateListItemsWS.Credentials = new System.Net.NetworkCredential(userName, password, domain);
// Create an XmlDocument object and construct a Batch element and its attributes.

XmlDocument doc = new System.Xml.XmlDocument();
// The name of this node is irrelevant
Xml.XmlElement batchElement = doc.CreateElement("Batch");
batchElement.SetAttribute("OnError","Continue");

// Construct field value xml
StringBuilder newFeedbackItemXml = new StringBuilder();
newFeedbackItemXml.Append("");

// Have to use title in place of build number as title is the internal name
newFeedbackItemXml.Append("");
newFeedbackItemXml.Append(title);
newFeedbackItemXml.Append("");
newFeedbackItemXml.Append("");
newFeedbackItemXml.Append(testField);
newFeedbackItemXml.Append("");
newFeedbackItemXml.Append("");
batchElement.InnerXml = newFeedbackItemXml.ToString();

// Add item to list
XmlNode addItemResults = updateListItemsWS.UpdateListItems("Build Log", batchElement);

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>