Skip to main content

The most efficient way to a lot of transaction in SharePoint is using of SPWeb.ProcessBatchData method. For deleting operations there are very helpfull postvery helpfull post. Code exaple from that post:

    // We prepare a String.Format with a String.Format, this is why we have a {{0}} 
   string command = String.Format("<Method><SetList Scope=\"Request\">{0}</SetList><SetVar Name=\"ID\">{{0}}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar><SetVar Name=\"owsfileref\">{{1}}</SetVar></Method>", list.ID);
   // We get everything but we limit the result to 100 rows 
   SPQuery q = new SPQuery();
   q.RowLimit = 100;

   // While there's something left 
   while (list.ItemCount > 0)
   {
    // We get the results 
    SPListItemCollection coll = list.GetItems(q);

    StringBuilder sbDelete = new StringBuilder();
    sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");

    Guid[] ids = new Guid[coll.Count];
    for (int i=0;i<coll.Count;i++)
    {
     SPListItem item = coll[i];
     sbDelete.Append(string.Format(command, item.ID.ToString(), item.File.ServerRelativeUrl));
     ids[i] = item.UniqueId;
    }
    sbDelete.Append("</Batch>");

    // We execute it 
    web.ProcessBatchData(sbDelete.ToString());

    //We remove items from recyclebin
    web.RecycleBin.Delete(ids);

    list.Update();
   }
  }

The most efficient way to a lot of transaction in SharePoint is using of SPWeb.ProcessBatchData method. For deleting operations there are very helpfull post. Code exaple from that post:

    // We prepare a String.Format with a String.Format, this is why we have a {{0}} 
   string command = String.Format("<Method><SetList Scope=\"Request\">{0}</SetList><SetVar Name=\"ID\">{{0}}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar><SetVar Name=\"owsfileref\">{{1}}</SetVar></Method>", list.ID);
   // We get everything but we limit the result to 100 rows 
   SPQuery q = new SPQuery();
   q.RowLimit = 100;

   // While there's something left 
   while (list.ItemCount > 0)
   {
    // We get the results 
    SPListItemCollection coll = list.GetItems(q);

    StringBuilder sbDelete = new StringBuilder();
    sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");

    Guid[] ids = new Guid[coll.Count];
    for (int i=0;i<coll.Count;i++)
    {
     SPListItem item = coll[i];
     sbDelete.Append(string.Format(command, item.ID.ToString(), item.File.ServerRelativeUrl));
     ids[i] = item.UniqueId;
    }
    sbDelete.Append("</Batch>");

    // We execute it 
    web.ProcessBatchData(sbDelete.ToString());

    //We remove items from recyclebin
    web.RecycleBin.Delete(ids);

    list.Update();
   }
  }

The most efficient way to a lot of transaction in SharePoint is using of SPWeb.ProcessBatchData method. For deleting operations there are very helpfull post. Code exaple from that post:

    // We prepare a String.Format with a String.Format, this is why we have a {{0}} 
   string command = String.Format("<Method><SetList Scope=\"Request\">{0}</SetList><SetVar Name=\"ID\">{{0}}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar><SetVar Name=\"owsfileref\">{{1}}</SetVar></Method>", list.ID);
   // We get everything but we limit the result to 100 rows 
   SPQuery q = new SPQuery();
   q.RowLimit = 100;

   // While there's something left 
   while (list.ItemCount > 0)
   {
    // We get the results 
    SPListItemCollection coll = list.GetItems(q);

    StringBuilder sbDelete = new StringBuilder();
    sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");

    Guid[] ids = new Guid[coll.Count];
    for (int i=0;i<coll.Count;i++)
    {
     SPListItem item = coll[i];
     sbDelete.Append(string.Format(command, item.ID.ToString(), item.File.ServerRelativeUrl));
     ids[i] = item.UniqueId;
    }
    sbDelete.Append("</Batch>");

    // We execute it 
    web.ProcessBatchData(sbDelete.ToString());

    //We remove items from recyclebin
    web.RecycleBin.Delete(ids);

    list.Update();
   }
  }
added 7 characters in body
Source Link
IAfanasov
  • 1.9k
  • 13
  • 20

The most efficient way to a lot of transaction in SharePoint is using of SPWeb.ProcessBatchData method. For deleting operations there are very helpfull post. Code exaple from that post:

private static String BuildBatchDeleteCommand( SPList list, SPListItemCollection coll ) 
{
  StringBuilder sbDelete = new StringBuilder();
  sbDelete.Append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>" );
 
  // We prepare a String.Format with a String.Format, this is why we have a {{0}} 
   string command = String.Format( "<Method><SetList Scope=\"Request\">{0}</SetList><SetVar Name=\"ID\">{{0}}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar><SetVar Name=\"owsfileref\">{{1}}</SetVar></Method>", list.ID );
  foreach (// SPListItemWe itemget ineverything collbut )we 
 limit the {
result to 100 rows sbDelete.Append( 
 string.Format( command, item.ID.ToString(),SPQuery item.File.ServerRelativeUrlq )= new SPQuery();
  }
  sbDeleteq.Append(RowLimit "</Batch>"= );100;

  return sbDelete.ToString();
}

// While there's something left 
   while ( list.ItemCount > 0 ) 
   {
    // We get everythingthe butresults we 
 limit the result toSPListItemCollection 100coll rows= list.GetItems(q);

  SPQuery q StringBuilder sbDelete = new SPQueryStringBuilder();
  q  sbDelete.RowLimitAppend("<?xml =version=\"1.0\" 100;encoding=\"UTF-8\"?><Batch>");

    Guid[] ids = new Guid[coll.Count];
  // We getfor the(int resultsi=0;i<coll.Count;i++)
  SPListItemCollection coll {
     SPListItem item = listcoll[i];
     sbDelete.GetItemsAppend(string.Format(command, qitem.ID.ToString(), item.File.ServerRelativeUrl));
 
   // We process aids[i] CAML= batchitem.UniqueId;
  String batch =}
 BuildBatchDeleteCommand( list, coll sbDelete.Append("</Batch>");
 
    // We execute it 
    web.ProcessBatchData(sbDelete.ToString());

 batch   //We remove items from recyclebin
    web.RecycleBin.Delete(ids);
 
    list.Update();
   }
  }

The most efficient way to a lot of transaction in SharePoint is using of SPWeb.ProcessBatchData method. For deleting operations there are very helpfull post. Code exaple from that post:

private static String BuildBatchDeleteCommand( SPList list, SPListItemCollection coll ) 
{
  StringBuilder sbDelete = new StringBuilder();
  sbDelete.Append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>" );
 
  // We prepare a String.Format with a String.Format, this is why we have a {{0}}
  string command = String.Format( "<Method><SetList Scope=\"Request\">{0}</SetList><SetVar Name=\"ID\">{{0}}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar><SetVar Name=\"owsfileref\">{{1}}</SetVar></Method>", list.ID );
  foreach ( SPListItem item in coll ) 
   {
    sbDelete.Append( string.Format( command, item.ID.ToString(), item.File.ServerRelativeUrl ) );
  }
  sbDelete.Append( "</Batch>" );
  return sbDelete.ToString();
}

// While there's something left
while ( list.ItemCount > 0 ) 
{
  // We get everything but we limit the result to 100 rows
  SPQuery q = new SPQuery();
  q.RowLimit = 100;
 
  // We get the results
  SPListItemCollection coll = list.GetItems( q );
 
   // We process a CAML batch
  String batch = BuildBatchDeleteCommand( list, coll );
 
  // We execute it
  web.ProcessBatchData( batch );
 
  list.Update();
}

The most efficient way to a lot of transaction in SharePoint is using of SPWeb.ProcessBatchData method. For deleting operations there are very helpfull post. Code exaple from that post:

    // We prepare a String.Format with a String.Format, this is why we have a {{0}} 
   string command = String.Format("<Method><SetList Scope=\"Request\">{0}</SetList><SetVar Name=\"ID\">{{0}}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar><SetVar Name=\"owsfileref\">{{1}}</SetVar></Method>", list.ID);
   // We get everything but we limit the result to 100 rows  
   SPQuery q = new SPQuery();
   q.RowLimit = 100;

   // While there's something left 
   while (list.ItemCount > 0)
   {
    // We get the results  
    SPListItemCollection coll = list.GetItems(q);

    StringBuilder sbDelete = new StringBuilder();
    sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");

    Guid[] ids = new Guid[coll.Count];
    for (int i=0;i<coll.Count;i++)
    {
     SPListItem item = coll[i];
     sbDelete.Append(string.Format(command, item.ID.ToString(), item.File.ServerRelativeUrl));
     ids[i] = item.UniqueId;
    }
    sbDelete.Append("</Batch>");

    // We execute it 
    web.ProcessBatchData(sbDelete.ToString());

    //We remove items from recyclebin
    web.RecycleBin.Delete(ids);

    list.Update();
   }
  }
Source Link
IAfanasov
  • 1.9k
  • 13
  • 20

The most efficient way to a lot of transaction in SharePoint is using of SPWeb.ProcessBatchData method. For deleting operations there are very helpfull post. Code exaple from that post:

private static String BuildBatchDeleteCommand( SPList list, SPListItemCollection coll ) 
{
  StringBuilder sbDelete = new StringBuilder();
  sbDelete.Append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>" );
 
  // We prepare a String.Format with a String.Format, this is why we have a {{0}}
  string command = String.Format( "<Method><SetList Scope=\"Request\">{0}</SetList><SetVar Name=\"ID\">{{0}}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar><SetVar Name=\"owsfileref\">{{1}}</SetVar></Method>", list.ID );
  foreach ( SPListItem item in coll ) 
  {
    sbDelete.Append( string.Format( command, item.ID.ToString(), item.File.ServerRelativeUrl ) );
  }
  sbDelete.Append( "</Batch>" );
  return sbDelete.ToString();
}

// While there's something left
while ( list.ItemCount > 0 ) 
{
  // We get everything but we limit the result to 100 rows
  SPQuery q = new SPQuery();
  q.RowLimit = 100;
 
  // We get the results
  SPListItemCollection coll = list.GetItems( q );
 
  // We process a CAML batch
  String batch = BuildBatchDeleteCommand( list, coll );
 
  // We execute it
  web.ProcessBatchData( batch );
 
  list.Update();
}