The built in Sharepoint webservices are a great way to add list items programatically. Here’s a code sample to get you started. This is a simple Windows application with a form that submits values into a Sharepoint 2007 list.
To get started, create a Windows application in Visual Studio 2008. Add a web reference to your Sharepoint list’s web service.
How to add the web reference:
Right click the "References" folder in the Solutions explorer. Add a "Services" reference. Find and click the "Advanced" button on the lower left. Click "Add web reference" on the lower left. Enter your Lists.asmx url.
Add a few text boxes and a button to your windows form. The form will take the 2 values, “Title,” and “Message,” and add them to the Sharepoint list. I created 3 text boxes on my form. txtTitle, txt Message, and txtStatus. The first 2 are mapped the the Title and Message fields on my Sharepoint list.
Make sure you do the following:
1. When adding your web service reference, make sure you add it at the site level where your list is located. For example http://SPServer/SubSite1/_vti_bin/Lists.asmx if your list is located in SubSite1. The web service at the root will appear to work, but you will get an error saying “The list that is referenced here no longer exists.”
2. Make sure you check the app.config. There’s a bug in Visual Studio in case you delete and re-add your web service reference that retains the first one you added.
3. Use either the GUID or the List name to access your list. The GUID can be found in the URL when you edit the list settings. Hint: Translate the the url-encoded symbols. The first and the last are brackets, the middle ones are dashes
a. %7b = {
b. %7d = }
c. %2D = -
Here’s the code to add the item to the list. Basically, you are sending in an xml batch to the webservice with the command and the values to insert.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace TestListsWebservice
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
AddListItem();
}
private void AddListItem()
{
string strBatch = "<Method ID='1' Cmd='New'>" +
"<Field Name='Title'>" + txtTitle.Text + "</Field>" +
"<Field Name='Message'>" + txtMessage.Text + "</Field></Method>";
ListsWS.Lists listService = new ListsWS.Lists();
// SharePoint Web Serices require authentication
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
//Either GUID or list name will work.
// String listName = "{209B7C81-6313-4A5D-B0A2-4EB5A58B8538}";
String listName = "MyListName";
XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");
elBatch.SetAttribute("OnError", "Continue");
elBatch.SetAttribute("ListVersion", "1");
elBatch.InnerXml = strBatch;
XmlNode ndReturn = listService.UpdateListItems(listName, elBatch);
txtStatus.Text = ndReturn.OuterXml.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}//
}
}