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

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

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

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

You May Also Like

About the Author: rnowik

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.