2

I am looking for adding SPListItem using SPWeb.ProcessBatchData method. Could any one please help me on syntax for adding list item to my custom list.

1 Answer 1

1

if you want to use SPWeb.ProcessBatchData method then follow this tutorial,

Inserting New Items to List using ProcessBatchData

why you must have to use "ProcessBatchData" when you can easily get SPWeb.GetList(siteURL) method to get list and add items to it.

using System;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite site = new SPSite("http://localhost/sites/sitecollection"))
         {
            using (SPWeb web = site.OpenWeb("sitecollection/subsite"))
            {
               string listUrl = "/sites/sitecollection/subsite/Lists/Announcements";
               SPList list = web.GetList(listUrl);
               Console.WriteLine("List URL: {0}", list.RootFolder.ServerRelativeUrl);
               SPListItem oSPListItem = list.Items.Add();
               oSPListItem["Title"] = "Hello SharePoint";
               oSPListItem.Update();
            }
         }
         Console.ReadLine();
      }
   }
}

taken from MSDN GetList method

For More examples check this out

programmatically-add-items-list

2
  • First of thanks for taking time to answer. No doubt I can use the other approach. But in my case I am adding multiple list items inside a loop. And item.Update() inside the loop is violation of SharePoint coding rules.
    – V.B
    Sep 5, 2012 at 7:48
  • I tried that before and it worked for me, however I put a link up there where he is inserting multiple items using process batch method :) Sep 5, 2012 at 8:07

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.