1

What's the best way to programatically add one or more items to a SharePoint list in WSS 3.0?

3 Answers 3

1

Microsoft Online requires that you use ProcessBatchData if you are adding multiple items within a loop:

Please use ProcessBatchData. It is a good method for processing a lot of commands against a SPList without having to open a SPListItemCollection and pay the penalty of slow performance if the list contains a substantial amount of items. For more details refer SPWeb.ProcessBatchData Method

This requirement changed my code adding multiple items to a Links list from this:

SPList list = web.Lists[listname];
string[] urls = property.Value.Split(";".ToCharArray());
foreach (string url in urls)
{
    SPListItem item = list.Items.Add();
    item[SPBuiltInFieldId.URL] = GetUrl(web, url);
    item.Update();
}

to this:

StringBuilder methodBuilder = new StringBuilder();
string batchFormat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
  "<ows:Batch OnError=\"Return\">{0}</ows:Batch>";

string methodFormat = "<Method ID=\"{0}\">" +
 "<SetList>{1}</SetList>" +
 "<SetVar Name=\"Cmd\">Save</SetVar>" +
 "<SetVar Name=\"ID\">New</SetVar>" +
 "<SetVar Name=\"urn:schemas-microsoft-com:office:office#URL\">{2}</SetVar>" +
 "</Method>";

SPList list = web.Lists[listname];
string listGuid = list.ID.ToString();
int i = 0;
string[] urls = property.Value.Split(";".ToCharArray());
foreach (string url in urls)
{
    methodBuilder.AppendFormat(methodFormat, ++i, listGuid, GetUrl(web, url));
}

string batch = string.Format(batchFormat, methodBuilder.ToString());
web.ProcessBatchData(batch);

Using ProcessBatchData obviously reduces readability, so (unless required) I would not use it for only a couple items. But it's good to know when wanting to add lots of items (especially in a post back or an -ing event receiver where the user is waiting).

1
  • Sorry can't vote up as only have 13 reputation :-(
    – SteveC
    Commented Nov 18, 2010 at 9:30
3

Well there are a couple of ways to do this, but again it depends on the type of the application runnig this code.

  1. Using the SharePoint Object model: Would work only on the server side. Suitable for web parts and any other components that would sit on the same box where the SharePoint is inatalled. This approach is easily proggramable but totaly dependent on the enviroment server.

  2. Lists.asmx web service: Suitable to applicatios running outside the SharePoint eg. Windows app etc. This is independet of the Server running but can be a bit complex because you need to deal with a lot CAML also there are limitations of using web services.

  3. JQuery library for SharePoint: Essentially the same as #2 but works only for web apps inside and outside SharePoint. Rest us Same as #2.

1
  • Sorry can't vote up as only have 13 reputation :-(
    – SteveC
    Commented Nov 18, 2010 at 9:30
1

This the way I did it in a .NET project written for WSS3.0

SPWeb oWeb = SPContext.Current.Web; // you'll need to set this according to how your own    project
SPList oList = oWeb.GetList("ListName");
SPListItem oListItem = oList.Items.Add();

oListItem[SPBuiltInFieldId.Title] = "My Title"; // using standard field/column names
oListItem["MyColumn"] = "My Column"; // using the column name

oListItem.Update(); // The first update is the one that actually adds the item and it doesn't have an ID until that point.

Hope this helps.

1
  • Sorry can't vote up as only have 13 reputation :-(
    – SteveC
    Commented Nov 18, 2010 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.