Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

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).

share|improve this answer
    
Thanks for the pointer Rich –  SteveC Nov 18 '10 at 9:29
    
Sorry can't vote up as only have 13 reputation :-( –  SteveC Nov 18 '10 at 9:30
add comment

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.

share|improve this answer
    
Sorry can't vote up as only have 13 reputation :-( –  SteveC Nov 18 '10 at 9:30
add comment

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.

share|improve this answer
    
Sorry can't vote up as only have 13 reputation :-( –  SteveC Nov 18 '10 at 9:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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